zoukankan      html  css  js  c++  java
  • Idea中Spring整合MyBatis框架中配置文件中对象注入问题解决方案

    运行环境: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>
    

     出现:

    原因分析:

    这里的错误,是因为系统找不到userMapper,因为没有定义,在没有用Spring的时候,userMapper是通过sqlSession的getMapper方法获得的,

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

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

    问题解决:

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

     

  • 相关阅读:
    ios设备new Date('2019-07-26 11:00:00')报错
    vue图片压缩(不失真)
    ios微信端网页点击右上角复制链接或在浏览器打开,得不到当前页地址(动态路由)
    ios打开网页,输入框获取焦点键盘弹起,关闭键盘,界面下方空白不回弹
    vue-cli打包后vendor.js文件太大怎么办
    ios点击有300毫秒延迟,输入框必须重压或长按才能获取焦点唤起软键盘
    ios微信端上下滑动页面,若触摸的是input区域,页面内不滚动,整个页面被拖动了
    vue-cli打包优化之分析工具webpack-bundle-analyzer
    vue引入fastclick设置输入框type="number"报错Failed to execute 'setSelectionRange' on 'HTMLInputElement': The input element's type ('number') does not support selection.的解决办法
    js/vue图片压缩
  • 原文地址:https://www.cnblogs.com/ncl-960301-success/p/11000358.html
Copyright © 2011-2022 走看看