spring整合mybatis接口无法注入问题

Posted 意必固我

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring整合mybatis接口无法注入问题相关的知识,希望对你有一定的参考价值。

 

          在学习Spring完之后简单的了解了MyBatis。然后进行简单的整合,遇到MyBatista接口映射的Bean无法自动注入的问题;

代码异常:

线程“main”org.springframe .bean .factory中的异常。创建名为“UserController”的bean时出错:通过字段“userdao”表示的不满足的依赖关系;嵌套异常是org.springframe .bean .factory。BeanCreationException:在文件[C:Usersli rui longeclipse-workspaceMyBatis_Demouildclassescommybatisdao 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 . [email protected]]内检类[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/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://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>

 

  1. 检查扫描的包名,是否有写错或者少写的。
  2. 确定数据源的配置正常,我的问题就出在这里,修改数据库配置信息(密码等),看是否会报不一样的错,当还是原来的错,说明配置文件没有加载或者数据源错误。我用的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>
  3. 检查对应的依赖类,配置文件路径能否Ctrl进去。MyBatis的核心文件和映射文件路径是否正确。以下是我的代码:
  4. <?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 = "com/mybatis/dao/UserMapper.xml"/>
        </mappers>
    </configuration
     
  5. <?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 = "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。

以上是关于spring整合mybatis接口无法注入问题的主要内容,如果未能解决你的问题,请参考以下文章

解决spring mybatis 整合后mapper接口注入失败

MyBatis Spring整合配置映射接口类与映射xml文件

mybatis常见错误

Spring整合Mybatis底层原理

springboot 整合 mybatis dao一直自动注入失败

Mybatis学习--spring和Mybatis整合