日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Spring和Mybatis整合,配置文件

發布時間:2025/6/15 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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整合,配置文件的全部內容,希望文章能夠幫你解決所遇到的問題。

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