spring整合mybatis接口无法注入问题
在学习Spring完之后简单的了解了MyBatis。然后进行简单的整合,遇到MyBatista接口映射的Bean无法自动注入的问题;
代码异常:
线程“main”org.springframe .bean .factory中的异常。创建名为“UserController”的bean时出错:通过字段“userdao”表示的不满足的依赖关系;嵌套异常是org.springframe .bean .factory。BeanCreationException:在文件[C:\Users\li rui long\eclipse-workspace\MyBatis_Demo\build\classes\com\mybatis\dao\ userdao]中创建名为“userdao”的bean时出错。类]:在设置bean属性“sqlSessionFactory”时无法解析对bean“sqlSessionFactory”的引用;嵌套异常是org.springframe .bean .factory。BeanCreationException:在类路径资源[ApplicationContext]中定义名称为“sqlSessionFactory”的bean创建错误。:设置bean属性“dataSource”时不能解析对bean“dataSource”的引用;嵌套异常是org.springframe .bean .factory。BeanCreationException:创建名为“dataSource”的bean时出错:查找方法解析失败;嵌套异常是java.lang。IllegalStateException:未能从ClassLoader [jdk.internal.loader.ClassLoader . $AppClassLoader@77a567e1]内检类[org.apache.commons.dbcp2.BasicDataSource]
异常提示,控制器层的Bean无法创建,原因是MyBatis对应的映射接口无法完成映射,无法生成DAO层的Bean;
所以问题应该出现在XML文件里,
测试类,13行报错:
1 package com.mybatis.test; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 import com.mybatis.controller.UserController; 7 8 9 public class Test_Controller { 10 11 public static void main(String[] args) { 12 // TODO Auto-generated method stub 13 ApplicationContext app = new ClassPathXmlApplicationContext("ApplicationContext.xml"); 14 UserController coll = (UserController) app.getBean("UserController"); 15 coll.test(); 16 } 17 18 }
对ApplicationContext.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:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd"><!-- 指定需要扫描的包,使注解生效 --><context:component-scan base-package="com.mybatis.po"/><context:component-scan base-package="com.mybatis.dao"/><context:component-scan base-package="com.mybatis.Controller"/><!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"><property name="driverClassName" value = "com.mysql.jdbc.Driver"/><property name="url" value ="jdbc:mysql://localhost:3306/Springtest?characterEncoding=utf8"/><property name="username" value = "root"/><property name="password" value ="mysql" /><!-- 可同时连接的最大的连接数 --><property name="maxActive" value="60" /><!-- 最大的空闲的连接数 --><property name="maxTotal" value="60" /><!-- 最小的空闲的连接数,低于这个数量会被创建新的连接,默认为0 --><property name="maxIdle" value="5" /> <!-- 连接池启动时创建的初始化连接数量,默认值为0 --> <property name="initialSize" value="5" /> </bean> <!-- 添加事务支持 --><bean id = "txManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name = "dataSource" ref = "dataSource"/></bean><!-- 开启事务注解 --><tx:annotation-driven transaction-manager ="txManager"/><!-- 配置Mybatis工厂,同时指定数据源,并与MyBatista完美结合 --><bean id="sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref ="dataSource"/><!-- configLocation 的属性为Mybatis 的核心配置文件 --><property name = "configLocation" value = "classpath:mybatis-config.xml"></property></bean><!-- Mapper 代理开发,使用Spring自动扫描MyBatista的接口并装配 --><!-- Spring 将指定包中所有的被@Mapper注解标注的接口自动装配为MyBatatis的映射接口 --><bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- MyBatis-spring组件的扫描器 --><property name="basePackage" value = "com.mybatis.dao"/><property name="sqlSessionFactoryBeanName" value = "sqlSessionFactory"/> </bean></beans>
- 检查扫描的包名,是否有写错或者少写的。
- 确定数据源的配置正常,我的问题就出在这里,修改数据库配置信息(密码等),看是否会报不一样的错,当还是原来的错,说明配置文件没有加载或者数据源错误。我用的DBCP数据源(需要导入两个包DBCP+连接池包),修改密码后还是报同样的错误所以我尝试着用Spring自带的数据源,解决了问题,正确代码:
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:context="http://www.springframework.org/schema/context" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context.xsd 11 http://www.springframework.org/schema/tx 12 http://www.springframework.org/schema/tx/spring-tx.xsd"> 13 <!-- 指定需要扫描的包,使注解生效 --> 14 15 <context:component-scan base-package="com.mybatis.po"/> 16 <context:component-scan base-package="com.mybatis.dao"/> 17 <context:component-scan base-package="com.mybatis.Controller"/> 18 <!-- 配置数据源 --> 19 <bean id = "dataSource" class = "org.springframework.jdbc.datasource.DriverManagerDataSource"> 20 <property name="driverClassName" value = "com.mysql.jdbc.Driver"/> 21 <property name="url" value ="jdbc:mysql://localhost:3306/Springtest?characterEncoding=utf8"/> 22 <property name="username" value = "root"/> 23 <property name="password" value ="mysql" /> 24 </bean> 25 26 <!-- 添加事务支持 --> 27 <bean id = "txManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager"> 28 <property name = "dataSource" ref = "dataSource"/> 29 </bean> 30 <!-- 开启事务注解 --> 31 <tx:annotation-driven transaction-manager ="txManager"/> 32 <!-- 配置Mybatis工厂,同时指定数据源,并与MyBatista完美结合 --> 33 <bean id="sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean"> 34 <property name="dataSource" ref ="dataSource"/> 35 <!-- configLocation 的属性为Mybatis 的核心配置文件 --> 36 <property name = "configLocation" value = "classpath:mybatis-config.xml"></property> 37 </bean> 38 <!-- Mapper 代理开发,使用Spring自动扫描MyBatista的接口并装配 --> 39 <!-- Spring 将指定包中所有的被@Mapper注解标注的接口自动装配为MyBatatis的映射接口 --> 40 <bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer"> 41 <!-- MyBatis-spring组件的扫描器 --> 42 <property name="basePackage" value = "com.mybatis.dao"/> 43 <property name="sqlSessionFactoryBeanName" value = "sqlSessionFactory"/> 44 </bean> 45 46 </beans>
- 检查对应的依赖类,配置文件路径能否Ctrl进去。MyBatis的核心文件和映射文件路径是否正确。以下是我的代码:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration><mappers><!-- 映射文件--><mapper resource = "com/mybatis/dao/UserMapper.xml"/></mappers> </configuration
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace = "com.mybatis.dao.UserDao"><!-- 根据ID查询用户信息 --><select id="selectUserById" parameterType = "Integer" resultType = "com.mybatis.po.Myuser">SELECT * FROM user WHERE uid = #{uid}</select><!-- <select id="selectAllUser" resultType = "com.mybatis.po.Myuser">SELECT * FROM user</select>添加一个用户,#{uname}为com.mybatis.po.MyUser属性值<insert id ="addUser" parameterType = "com.mybatis.po.Myuser">INSERT INTO user (uname,usex) VALUES (#{uname},#{usex})</insert>修改一个用户<update id="updateUser" parameterType = "com.mybatis.po.Myuser">UPDATE user SET uname = #{unmae},usex = #{user} where uid = #{uid}</update>删除一个用户<delete id = "delectUser" parameterType = "Integer">DELECT from user WHERE uid = #{uid}</delete> --> </mapper >
看Dao层的接口和Mapped的映射文件是否是在同一包下。
类似问题的博客:
https://blog.csdn.net/h363659487/article/details/74275972
https://blog.csdn.net/u012385190/article/details/53186552
嗯嗯,第一次写这样的博客,希望会对大家有帮助!!,愿我们都被温柔以待!2019.4.21。
转载于:https://www.cnblogs.com/liruilong/p/10744962.html
spring整合mybatis接口无法注入问题相关推荐
- Spring整合MyBatis原理之Mapper接口和xml文件的解析
目录 1. 前言 2. 类 `SqlSessionFactoryBean` 2.1. 实现了 `FactoryBean` 接口的 `getObject()` 2.2. `buildSqlSession ...
- Spring整合Mybatis之注解方式,(注解整合Junit)
Spring整合Mybatis之注解方式 我有一篇博客详细写了我自己使用xml的方法Spring整合MyBatis,现在我就把核心配置文件中的每个bean的配置使用注解的方式实现 注解整合MyBati ...
- spring整合mybatis基于xml配置
数据库 /* Navicat MySQL Data Transfer Source Server : mysql Source Server Version : 50549 Sourc ...
- spring 整合 mybatis 中数据源的几种配置方式
因为spring 整合mybatis的过程中, 有好几种整合方式,尤其是数据源那块,经常看到不一样的配置方式,总感觉有点乱,所以今天有空总结下. 一.采用org.mybatis.spring.mapp ...
- Spring 整合 Mybatis
数据库环境 // 创建mybatis数据库 create database mybatis;use mybatis // 创建teacher表 create table teacher(id int ...
- Spring整合Mybatis之DAO层、Service层开发
3. Spring整合Mybatis编程DAO层开发 1. 项目引入相关依赖spring mybatis mysql mybatis-spring druid2. 编写spring.xml整合:spr ...
- Spring——Spring整合MyBatis
文章目录: 1.写在前面 2.实现步骤 2.1 项目的大体框架 2.2 使用Navicat在数据库中创建一张表student2 2.3 在pom.xml文件中加入maven依赖 2.4 编写实体类St ...
- Spring源码深度解析(郝佳)-学习-源码解析-Spring整合MyBatis
了解了MyBatis的单独使用过程之后,我们再来看看它也Spring整合的使用方式,比对之前的示例来找出Spring究竟为我们做了什么操作,哪些操作简化了程序开发. 准备spring71.xml &l ...
- Spring 整合 Mybatis 原理
目录 Mybatis的基本工作原理 分析需要解决的问题 Spring中Bean的产生过程 解决问题 解决方案 FactoryBean Import 总结 优化 Mybatis的基本工作原理 在 Myb ...
最新文章
- PNAS:土壤氮循环微生物功能特征的全球生物地理学
- python 排序函数 sort sorted 简介
- 再见,2014;您好,2015!
- eclipse 查找
- linux重定向文件被修改后,Linux服务器修改.htaccess文件实现301重定向
- css3中animation动画、浏览器私有前缀、文字阴影
- EasyUI Datagrid 自定义列、Foolter及单元格编辑
- C# RSA在服务上使用出现拒绝方法错误的解决方法
- c语言坐标打印佛祖,C语言输入平面上两个点的坐标(double类型),计算两个点之间的距离。看”详细“里哪里写错了谢谢...
- 计算机科学导论_学长说专业 | 计算机科学与技术
- 分簇路由算法 LEACH算法
- 高通骁龙410e/APQ8016E嵌入式物联网应用处理器解决方案
- vs2019中出现PyTorch is not linked with support for cuda devices的解决方法
- 大数据人工智能应用场景
- 计算机管理中不显示u盘,优盘不显示,教您优盘不显示处理方法
- 如何使用中国知网查询文献,并自动生成参考文献格式引文?
- unity3d学习笔记-报错
- Fansblog HDU-6608(费马小定理、威尔逊定理)
- CentOS7安装json格式化工具jq
- bpmn-process-designer基础上进行自定义样式(工具、元素、菜单)