當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring和Mybatis整合,配置文件
生活随笔
收集整理的這篇文章主要介紹了
Spring和Mybatis整合,配置文件
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
整合時除了未整合前spring、mybatis的必須包外還需要加入兩個包
spring-jdbc-4.2.5.RELEASE.jar
mybatis-spring-1.2.5.jar
spring-jdbc-4.2.5.RELEASE.jar 提供了Mybatis所用數(shù)據(jù)源的管理
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" /><!-- 數(shù)據(jù)源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><!-- 創(chuàng)建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提供的數(shù)據(jù)源對象,我們創(chuàng)建SqlSessionTemplate時用的是這個類的構造方法,傳入的參數(shù)的類型是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整合,配置文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ObjectOutputStream 和
- 下一篇: android 启动过程