在學習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>
- 檢查掃描的包名,是否有寫錯或者少寫的。
- 確定數據源的配置正常,我的問題就出在這裏,修改數據庫配置信息(密碼等),看是否會報不一樣的錯,當還是原來的錯,說明配置文件沒有加載或者數據源錯誤。我用的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>
- 檢查對應的依賴類,配置文件路徑能否Ctrl進去。MyBatis的核心文件和映射文件路徑是否正確。以下是我的代碼:
-
<?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
-
<?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 >
類似問題的博客:
https://blog.csdn.net/h363659487/article/details/74275972
https://blog.csdn.net/u012385190/article/details/53186552
嗯嗯,第一次寫這樣的博客,希望會對大家有幫助!!,願我們都被溫柔以待!2019.4.21。
原文出處:https://www.cnblogs.com/liruilong/p/10744962.html