日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring整合mybatis接口无法注入问题

發布時間:2023/12/10 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring整合mybatis接口无法注入问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

??? ? ? ? 在學習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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/txhttp://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 configurationPUBLIC "-//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 mapperPUBLIC "-//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 >

    ?

    看Dao層的接口和Mapped的映射文件是否是在同一包下。
  • 類似問題的博客:

    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

    總結

    以上是生活随笔為你收集整理的spring整合mybatis接口无法注入问题的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。