欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-11-27 17:23:00
在学习Spring完之后简单的了解了MyBatis。然后进行简单的整合,遇到MyBatista接口映射的Bean无法自动注入的问题; 代码异常: 线程“main”org.springframe .bean .factory中的异常。创建名为“UserController”的bean时出错:通过字段“ ......

 

          在学习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/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。