spring與mybatis的整合

<?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"

       xmlns:aop="http://www.springframework.org/schema/aop"

       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

           http://www.springframework.org/schema/aop

           http://www.springframework.org/schema/aop/spring-aop.xsd

           ">

<!--引入配置屬性文件 -->

<context:property-placeholder ignore-unresolvable="true" location="classpath:jdbc.properties" />

<!-- 配置數據源 使用的是Druid數據源 -->

<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource"

init-method="init" destroy-method="close">

<property name="url" value="${mysql.jdbc.url}" />

<property name="username" value="${mysql.jdbc.username}" />

<property name="password" value="${mysql.jdbc.password}" />


<!-- 初始化連接大小 -->

<property name="initialSize" value="0" />

<!-- 連接池最大使用連接數量 -->

<property name="maxActive" value="20" />


<!-- 連接池最小空閒 -->

<property name="minIdle" value="0" />

<!-- 獲取連接最大等待時間 -->

<property name="maxWait" value="60000" />

<property name="poolPreparedStatements" value="true" />

<property name="maxPoolPreparedStatementPerConnectionSize" value="33" />

<!-- 用來檢測有效sql -->

<property name="validationQuery" value="${validationQuery}" />

<property name="testOnBorrow" value="false" />

<property name="testOnReturn" value="false" />

<property name="testWhileIdle" value="true" />

<!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連接,單位是毫秒 -->

<property name="timeBetweenEvictionRunsMillis" value="60000" />

<!-- 配置一個連接在池中最小生存的時間,單位是毫秒 -->

<property name="minEvictableIdleTimeMillis" value="25200000" />

<!-- 打開removeAbandoned功能 -->

<property name="removeAbandoned" value="true" />

<!-- 1800秒,也就是30分鐘 -->

<property name="removeAbandonedTimeout" value="1800" />

<!-- 關閉abanded連接時輸出錯誤日誌 -->

<property name="logAbandoned" value="true" />

<!-- 監控數據庫 

<property name="filters" value="mergeStat" />

-->

</bean>


<!-- 創建SqlSessionFactory -->

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<!-- 數據源:必須屬性 -->

<property name="dataSource" ref="dataSource" />

<!-- 指定mybatis配置文件,如settings和typeAliases  -->

<property name="configLocation" value="classpath*:mybatis-config.xml" />

<!-- 指定mybatis的 XML映射器文件地址 -->

<property name="mapperLocations" value="classpath*:mybatis/**/*.xml" />

<!-- 可在mybatis配置文件中設置 -->

<property name="configuration">

   <bean class="org.apache.ibatis.session.Configuration">

     <property name="mapUnderscoreToCamelCase" value="true"/>

   </bean>

</property>

<!-- 使用mybatis的事務管理器,而不是spring事務管理器(不推薦) -->

<property name="transactionFactory">

   <bean class="org.apache.ibatis.transaction.managed.ManagedTransactionFactory" />

</property> 

</bean>


<!-- 創建sqlSession: SqlSessionTemplate

   1.需要自己編寫dao層接口及其實現類

   2.在實現類中注入SqlSessionTemplate或繼承SqlSessionDaoSupport

-->

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">

 <constructor-arg index="0" ref="sqlSessionFactory" />

 <constructor-arg index="1" value="BATCH" />

</bean>


<!-- 不需要使用SqlSessionTemplate和SqlSessionDaoSupport,直接通過映射器接口,由spring創建代理來實現

   注:

    1.必須是接口而且必須與映射xml文件命名空間對應

    2.需要一個一個地的創建       

-->    

<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">

 <property name="mapperInterface" value="com.demo.dao.UserDao" />

 <property name="sqlSessionFactory" ref="sqlSessionFactory" />

</bean>


<!-- MapperScannerConfigurer的使用與MapperFactoryBean相似,

   只不過MapperScannerConfigurer會根據package來創建MapperFactoryBean  

-->

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

  <property name="basePackage" value="com.demo.dao" />

  <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />

</bean>


<!-- 配置事務管理器 -->

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

  <property name="dataSource" ref="dataSource" />

</bean>


<!-- 註解方式配置事物 -->

<tx:annotation-driven transaction-manager="transactionManager" />


</beans>


說明:

1、mybatis-spring的整合也主要是創建sqlSessionFactory和sqlSession

2、創建sqlSessionFactory是必須的且統一的。

3、sqlSession有多種配置

 3.1 SqlSessionTemplate


<!--創建sqlSession: SqlSessionTemplate
  1.需要自己編寫dao層接口及其實現類
  2.在實現類中注入SqlSessionTemplate或繼承SqlSessionDaoSupport
-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
  <constructor-arg index="0" ref="sqlSessionFactory"/>
  <constructor-arg index="1" value="BATCH" />
</bean>


<!-- 不需要使用SqlSessionTemplate和SqlSessionDaoSupport,直接通過映射器接口,由spring創建代理來實現

    注:1.必須是接口而且必須與映射xml文件命名空間對應; 

        2.需要一個一個地的創建

-->    
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
   <property name="mapperInterface" value="com.demo.dao.UserDao" />
   <property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>


<!-- MapperScannerConfigurer的使用與MapperFactoryBean相似,只不過MapperScannerConfigurer會根據package來創建MapperFactoryBean -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 <property name="basePackage" value="com.demo.dao" />
 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />

</bean>



參考地址:http://www.mybatis.org/spring/zh/batch.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章