600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > Idea中Spring整合MyBatis框架中配置文件中对象注入问题解决方案

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

时间:2021-01-14 23:35:53

相关推荐

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

运行环境:Spring框架整合MaBitis框架

问题叙述:

在Spring配置文件applicationContext-mybatis.xml中配置好mybatis之后

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance"xmlns:context="/schema/context"xsi:schemaLocation="/schema/beans/schema/beans/spring-beans.xsd/schema/context/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="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/schema/beans/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选项,进行标注,表明,此问题忽略,可以正常编译

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。