1、单接口集成(test)
<!-- MyBatis 配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath*:org/activiti/dao/maper/*Mapper.xml" />
</bean>
<!-- mapper管理 -->
<!--创建dao bean(只需提供接口不需提供实现类 )-->
<bean id="userdao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="org.activiti.dao.IUserDao" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
2、批量接口集成的两种方法
1、基于接口的过滤器:所有接口继承一个父接口;
2、基于注解的过滤器:配置了特定注解的接口才会被集成;
方法一:
<!-- MyBatis 配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath*:org/activiti/dao/maper/*Mapper.xml" />
</bean>
<!-- mapper管理 -->
<!--创建dao bean(只需提供接口不需提供实现类 )-->
<!--通过模板定制mybatis的行为 -->
<bean id="sqlSessionTemplateBatch" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
<!--更新采用批量模式 -->
<constructor-arg index="1" value="BATCH"/>
</bean>
<!-- 采用自动扫描方式创建mapper bean(批量更新模式) -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="org.activiti.dao" />
<property name="sqlSessionTemplateBeanName" value="sqlSessionTemplateBatch" />
<property name="markerInterface" value="org.activiti.dao.BatchDao" />
</bean>
public interface IUserDao extends BatchDao {
int findUser();
User findUser2();
}
方法二(一般项目采用此方法):
首先新建一个注解:
package org.nick.mybatis.uitl;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MybatisMapper {
}
<!-- MyBatis 配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath*:org/activiti/dao/maper/*Mapper.xml" />
</bean>
<!-- mapper管理 -->
<!--创建dao bean(只需提供接口不需提供实现类 )-->
<!--通过模板定制mybatis的行为 -->
<bean id="sqlSessionTemplateBatch" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
<!--更新采用批量模式 -->
<constructor-arg index="1" value="BATCH"/>
</bean>
<!-- 采用自动扫描方式创建mapper bean(批量更新模式) -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="org.activiti.dao" />
<property name="sqlSessionTemplateBeanName" value="sqlSessionTemplateBatch" />
<property name="annotationClass" value="org.nick.mybatis.uitl.MybatisMapper" />
</bean>
import org.nick.mybatis.uitl.MybatisMapper;
@MybatisMapper
public interface IUserDao {
int findUser();
User findUser2();
}