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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring和Mybatis整合,配置文件

發布時間:2025/6/15 javascript 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring和Mybatis整合,配置文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

整合時除了未整合前spring、mybatis的必須包外還需要加入兩個包

spring-jdbc-4.2.5.RELEASE.jar

mybatis-spring-1.2.5.jar

spring-jdbc-4.2.5.RELEASE.jar 提供了Mybatis所用數據源的管理

mybatis-spring-1.2.5.jar 提供了myBatis所用的sqlSessionFactionBean 和SqlSessionTemplate

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 自動裝配配置 --><beanclass="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /><!-- 數據源Bean配置 --><bean id="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/appproject" /><property name="username" value="root" /><property name="password" value="12345678" /></bean><!-- MyBatis session工廠bean配置 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"></property><property name="mapperLocations" value="classpath*:mappers/mapper-*.xml"></property></bean><!-- 創建session bean,自動裝配 --><bean class="org.mybatis.spring.SqlSessionTemplate" autowire="byType"><constructor-arg ref="sqlSessionFactory"></constructor-arg></bean><import resource="beans-dao.xml" /><import resource="beans-service.xml" /></beans> org.springframework.jdbc.datasource.DriverManagerDataSource類是spring-jdbc提供的數據源對象,我們創建SqlSessionTemplate時用的是這個類的構造方法,傳入的參數的類型是SqlSessionFactionBean,這個Bean是由mybatis-spring提供的。
SqlSessionTemplate使用了自動裝配的方式(代碼如下),我的Dao層有一個基類,基類中的SqlSessionTemplate 已經用@Autowired。Spring啟動時,會自動注入,想要了解自動裝配的方式請點擊我的另外一篇博客@Autowired的使用 public class BaseDao {@Autowiredprotected SqlSessionTemplate session; }

?當然,整合的方式有很多種,此處是其中之一。

轉載于:https://www.cnblogs.com/doublejun/p/5387732.html

總結

以上是生活随笔為你收集整理的Spring和Mybatis整合,配置文件的全部內容,希望文章能夠幫你解決所遇到的問題。

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