運行環境: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選項,進行標注,表明,此問題忽略,可以正常編譯