1. 程式人生 > >spring+mybatis整合(二)

spring+mybatis整合(二)

一、applicationContext.xml配置

<!-- 設定掃描類 -->
<context:component-scan base-package="com.xxx.xxx"></context:component-scan>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="connectionProperties"><value>useUnicode=true;characterEncoding=utf-8;autoReconnect=true</value></property>
<!-- 基本屬性 url、user、password -->
<property name="url" value=" jdbc:mysql://xxx.xxx.xxx.xxx:3306/xxx?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true" />
<property name="username" value="xxxxxx" />
<property name="password" value="xxxxxx" />

<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="1" />
<property name="minIdle" value="1" />
<property name="maxActive" value="20" />

<!-- 配置獲取連線等待超時的時間 -->
<property name="maxWait" value="60000" />

<!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連線,單位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />

<!-- 配置一個連線在池中最小生存的時間,單位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" />

<property name="validationQuery" value="SELECT 'x'" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />

<!-- 開啟PSCache,並且指定每個連線上PSCache的大小 -->
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="20" />

<!-- 配置監控統計攔截的filters -->
<property name="filters" value="stat" />
</bean>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自動掃描entity目錄, 省掉Configuration.xml裡的手工配置 -->
<property name="typeAliasesPackage" value="com.xxx.xxx.spoc.vo" />
<!-- 顯式指定Mapper檔案位置 -->
<property name="mapperLocations" value="classpath:/mybatis/*Mapper.xml" />
<!-- mybatis全域性配置 -->
<property name="configLocation" value="classpath:/META-INF/spring/mybatis-config.xml" />
<!-- MyBatis分頁外掛配置 -->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<value>
dialect=mysql
reasonable=true
pageSizeZero=true
</value>
</property>
</bean>
</array>
</property>
</bean>
<!-- 掃描basePackage下所有以@MyBatisRepository標識的 介面-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xxx.dao" />
<property name="annotationClass" value="com.xxx.dao.MyBatisRepository"/>
</bean>
<!-- 事務控制 -->
<bean name="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>

<!-- 配置事務傳播特性 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
<tx:method name="query*" propagation="SUPPORTS" read-only="true" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
<tx:method name="*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 配置參與事務的方法 -->
<aop:config proxy-target-class="true">
<aop:pointcut id="allServiceMethod"
expression="execution(* com.xxx.service.*.impl.*Impl.*(..))" />
<aop:advisor pointcut-ref="allServiceMethod" advice-ref="transactionAdvice"/>
</aop:config>

二、META-INF/spring/mybatis-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"mybatis-3-config.dtd">
<configuration>
<!-- 配置全域性屬性 -->
<settings>
<!-- 獲取自增主鍵 -->
<setting name="useGeneratedKeys" value="true" />
<!-- 使用列別名替換列名 -->
<setting name="useColumnLabel" value="true" />
<!-- 開啟駝峰命名轉換 -->
<setting name="mapUnderscoreToCamelCase" value="true" />
</settings>
</configuration>

三、註解MyBatisRepository

 1 import java.lang.annotation.Documented;
 2 import java.lang.annotation.ElementType;
 3 import java.lang.annotation.Retention;
 4 import java.lang.annotation.RetentionPolicy;
 5 import java.lang.annotation.Target;
 6 
 7 import org.springframework.stereotype.Component;
 8 
 9 /**
10  * 標識MyBatis的DAO,方便{@link org.mybatis.spring.mapper.MapperScannerConfigurer}的掃描。
11  * 
12  */
13 @Retention(RetentionPolicy.RUNTIME)
14 @Target(ElementType.TYPE)
15 @Documented
16 @Component
17 public @interface MyBatisRepository {
18    String value() default "";
19 }

四、Dao類配置

@MyBatisRepository
public interface IFamousTeacherDao {
      //  介面……

}