首页 > 代码库 > Spring-Mybatis 异常记录(1)
Spring-Mybatis 异常记录(1)
Spring applicationconfig.xml如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 配置数据源 ,连接池用的阿里druid-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<!--
<property name="url" value="http://www.mamicode.com/jdbc:mysql://IP+数据库"/>
<property name="username" value="http://www.mamicode.com/用户名"/>
<property name="password" value="http://www.mamicode.com/密码"/>
-->
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- 配置mybatis的sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mappers.xml文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
<!-- mybatis配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<!-- DAO -->
<bean id="infoDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="net.xjdsz.dao.InfoDao" />
<property name="sqlSessionFactory" value="sqlSessionFactory"></property>
</bean>
</beans>
info.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="net.xjdsz.dao.InfoDao">
<!-- 查询条件:账号密码用户类型. 0第一个参数,1第二个参数,对应dao接口参数 -->
<select id="FindAllInfos" resultType="net.xjdsz.model.Info">
SELECT * FROM info limit 1
</select>
<!--
<select id="getAllUsers" resultMap="userResult">
SELECT USER_CODE,USER_NAME,USER_PWD,CREATE_DATE
FROM BLOG_USER
</select>
-->
</mapper>
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC
"-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<mappers>
<mapper resource="mapper/info.xml"/>
</mappers>
</configuration>
会报错:
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘sqlSessionFactory‘ defined in class path resource [spring-config/ApplicationContext.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: ‘file [E:\个人资料\个人代码\spring-batis\target\classes\mapper\info.xml]‘; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for net.xjdsz.dao.InfoDao.FindAllInfos
原因是,加载了两次mapper,注释掉红框即可
修改后依然会报错:
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘infoDao‘ defined in class path resource [spring-config/ApplicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type ‘java.lang.String‘ to required type ‘org.apache.ibatis.session.SqlSessionFactory‘ for property ‘sqlSessionFactory‘; nested exception is java.lang.IllegalStateException: Cannot convert value of type ‘java.lang.String‘ to required type ‘org.apache.ibatis.session.SqlSessionFactory‘ for property ‘sqlSessionFactory‘: no matching editors or conversion strategy found
原因是,sqlSessionFactory注入的时候,关键字用错了,value应该改成ref???
运行代码,运行成功
package net.xjdsz.service.impl;
import net.xjdsz.dao.InfoDao;
import net.xjdsz.model.Info;
import net.xjdsz.service.InfoService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by dingshuo on 2017/1/3.
*/
public class InfoServiceImpl implements InfoService {
private InfoDao infoDao;
@Override
public Info DoWork() {
Info info=infoDao.FindAllInfos();
return info;
}
public static void main(String[] args){
ApplicationContext ctx = null;
ctx = new ClassPathXmlApplicationContext("spring-config/ApplicationContext.xml");
InfoDao infoDao=(InfoDao)ctx.getBean("infoDao");
Info aaa=infoDao.FindAllInfos();
System.out.println(aaa.toString());
}
}
运行结果
Connected to the target VM, address: ‘127.0.0.1:58958‘, transport: ‘socket‘
一月 03, 2017 5:31:51 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7a0ac6e3: startup date [Tue Jan 03 17:31:51 CST 2017]; root of context hierarchy
一月 03, 2017 5:31:51 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-config/ApplicationContext.xml]
一月 03, 2017 5:31:52 下午 com.alibaba.druid.pool.DruidDataSource info
信息: {dataSource-1} inited
Disconnected from the target VM, address: ‘127.0.0.1:58958‘, transport: ‘socket‘
ID:1,TM:Thu Nov 10 00:00:00 CST 2016,DESC:nihao ,FLAG:0
Process finished with exit code 0
Spring-Mybatis 异常记录(1)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。