首页 > 代码库 > Spring-Mybatis 异常记录(1)

Spring-Mybatis 异常记录(1)

Spring  applicationconfig.xml如下
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xmlns:jee="http://www.springframework.org/schema/jee"
  8. xmlns:tx="http://www.springframework.org/schema/tx"
  9. xsi:schemaLocation="
  10. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  11. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  12. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
  13. http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
  14. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
  15. <!-- 配置数据源 ,连接池用的阿里druid-->
  16. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
  17. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  18. <!--
  19. <property name="url" value="http://www.mamicode.com/jdbc:mysql://IP+数据库"/>
  20. <property name="username" value="http://www.mamicode.com/用户名"/>
  21. <property name="password" value="http://www.mamicode.com/密码"/>
  22. -->
  23. <property name="url" value="jdbc:mysql://127.0.0.1:3306/test"/>
  24. <property name="username" value="root"/>
  25. <property name="password" value="root"/>
  26. </bean>
  27. <!-- 配置mybatis的sqlSessionFactory -->
  28. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  29. <property name="dataSource" ref="dataSource" />
  30. <!-- 自动扫描mappers.xml文件 -->
  31. <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
  32. <!-- mybatis配置文件 -->
  33. <property name="configLocation" value="classpath:mybatis-config.xml"></property>
  34. </bean>
  35. <!-- DAO -->
  36. <bean id="infoDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
  37. <property name="mapperInterface" value="net.xjdsz.dao.InfoDao" />
  38. <property name="sqlSessionFactory" value="sqlSessionFactory"></property>
  39. </bean>
  40. </beans>

info.xml
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3. <mapper namespace="net.xjdsz.dao.InfoDao">
  4. <!-- 查询条件:账号密码用户类型. 0第一个参数,1第二个参数,对应dao接口参数 -->
  5. <select id="FindAllInfos" resultType="net.xjdsz.model.Info">
  6. SELECT * FROM info limit 1
  7. </select>
  8. <!--
  9. <select id="getAllUsers" resultMap="userResult">
  10. SELECT USER_CODE,USER_NAME,USER_PWD,CREATE_DATE
  11. FROM BLOG_USER
  12. </select>
  13. -->
  14. </mapper>

mybatis-config.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE configuration PUBLIC
  3. "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <mappers>
  7. <mapper resource="mapper/info.xml"/>
  8. </mappers>
  9. </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???
技术分享
 
 运行代码,运行成功
  1. package net.xjdsz.service.impl;
  2. import net.xjdsz.dao.InfoDao;
  3. import net.xjdsz.model.Info;
  4. import net.xjdsz.service.InfoService;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;
  7. /**
  8. * Created by dingshuo on 2017/1/3.
  9. */
  10. public class InfoServiceImpl implements InfoService {
  11. private InfoDao infoDao;
  12. @Override
  13. public Info DoWork() {
  14. Info info=infoDao.FindAllInfos();
  15. return info;
  16. }
  17. public static void main(String[] args){
  18. ApplicationContext ctx = null;
  19. ctx = new ClassPathXmlApplicationContext("spring-config/ApplicationContext.xml");
  20. InfoDao infoDao=(InfoDao)ctx.getBean("infoDao");
  21. Info aaa=infoDao.FindAllInfos();
  22. System.out.println(aaa.toString());
  23. }
  24. }
运行结果
  1. Connected to the target VM, address: ‘127.0.0.1:58958‘, transport: ‘socket‘
  2. 一月 03, 2017 5:31:51 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
  3. 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7a0ac6e3: startup date [Tue Jan 03 17:31:51 CST 2017]; root of context hierarchy
  4. 一月 03, 2017 5:31:51 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
  5. 信息: Loading XML bean definitions from class path resource [spring-config/ApplicationContext.xml]
  6. 一月 03, 2017 5:31:52 下午 com.alibaba.druid.pool.DruidDataSource info
  7. 信息: {dataSource-1} inited
  8. Disconnected from the target VM, address: ‘127.0.0.1:58958‘, transport: ‘socket‘
  9. ID:1,TM:Thu Nov 10 00:00:00 CST 2016,DESC:nihao ,FLAG:0
  10. Process finished with exit code 0

Spring-Mybatis 异常记录(1)