Spring-Mybatis 异常记录
Posted 二刀
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring-Mybatis 异常记录相关的知识,希望对你有一定的参考价值。
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="jdbc:mysql://IP+数据库"/>
<property name="username" value="用户名"/>
<property name="password" value="密码"/>
-->
<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 异常记录的主要内容,如果未能解决你的问题,请参考以下文章
最新最全面的Spring详解——Spring-Mybatis整合
Intellij16创建Spring-Mybatis项目创(填)建(坑)记录,解决IDEA下找不到xml文件的问题
mybatis快速入门-spring-mybatis动态代理整合