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

Idea中Spring整合MyBatis框架中配置文件中对象注入问题解决方案

程序员文章站 2022-06-28 21:18:16
运行环境:Spring框架整合MaBitis框架 问题叙述: 在Spring配置文件applicationContext-mybatis.xml中配置好mybatis之后 接下来配置applicationContext-service.xml,其中需要注入userMapper 出现: 原因分析: 这 ......

运行环境:spring框架整合mabitis框架

问题叙述:

  在spring配置文件applicationcontext-mybatis.xml中配置好mybatis之后

  

<?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"
       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">
    <!--当前配置文件用于管理mybatis-->
    <!--加载资源文件,需要用到context命名空间-->
    <context:property-placeholder location="classpath:com/bjsxt/config/commons/db.properties"/>
    <!--配置数据源,在spring-jdbc.jar中提供了一个测试用的数据源-->
    <bean id="datasource" class="org.springframework.jdbc.datasource.drivermanagerdatasource">
        <property name="driverclassname" value="${db.driver}"/>
        <property name="url" value="${db.url}"/>
        <property name="username" value="${db.username}"/>
        <property name="password" value="${db.password}"/>
    </bean>
    <!--配置sqlsessionfactory对象,在mybatis-spring.jar中提供-->
    <bean id="factory" class="org.mybatis.spring.sqlsessionfactorybean">
        <!--注入数据源-->
        <property name="datasource" value="datasource"/>
        <!--配置别名-->
        <property name="typealiases" value="com.bjsxt.pojo"/>
    </bean>
    <!--配置映射扫描,在mybatis-spring.xml中提供-->
    <bean id="msc" class="org.mybatis.spring.mapper.mapperscannerconfigurer">
        <!--扫描位置-->
        <property name="basepackage" value="com.bjsxt.mapper"/>
        <!--注入工厂对象-->
        <property name="sqlsessionfactorybeanname" value="factory"/>
    </bean>

</beans>

  接下来配置applicationcontext-service.xml,其中需要注入usermapper

  

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
       xsi:schemalocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--此配置文件用于管理service对象-->
    <bean id="userservice" class="com.bjsxt.service.impl.userserviceimpl">
        <!--注入usermapper对象-->
        <property name="usermapper" ref="usermapper"/>
    </bean>
</beans>

 出现:

Idea中Spring整合MyBatis框架中配置文件中对象注入问题解决方案

原因分析:

这里的错误,是因为系统找不到usermapper,因为没有定义,在没有用spring的时候,usermapper是通过sqlsession的getmapper方法获得的,

当使用spring进行配置mybstis时,sqlsession对象和usermapper已经通过配置文件进行生成,但是这个过程是在程序正式启动运行过程中才会

产生的,此处虽然会报错,但是不影响程序运行,但是不解决总让人感觉不舒服,下面是解决方案:

问题解决:

Idea中Spring整合MyBatis框架中配置文件中对象注入问题解决方案

鼠标放在有错误的这一行,前面会有一个类似灯泡的图标,点击下三角,按照图上选择disable  inspextion选项,进行标注,表明,此问题忽略,可以正常编译