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 {
      //  接口……

}    

猜你喜欢

转载自www.cnblogs.com/xcwang9/p/9960016.html