最近做项目使用mybatis-generator生成mybatis的mapper.xml映射文件和mapper接口。
原来采用spring-bean的方式显示注入,代码如下:
<bean id="articleMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.kuanrf.gs.article.dao.ArticleMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
发现每个mybatis的mapper接口都这么配置的话,太麻烦。现优化采用spring统一扫描自动注入。
application_spring_db.xml
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!-- 配置dbcp数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="initialPoolSize" value="${connection_pools.initial_pool_size}" />
<property name="minPoolSize" value="${connection_pools.min_pool_size}" />
<property name="maxPoolSize" value="${connection_pools.max_pool_size}" />
<property name="maxIdleTime" value="${connection_pools.max_idle_time}" />
<property name="acquireIncrement" value="${connection_pools.acquire_increment}" />
<property name="checkoutTimeout" value="${connection_pools.checkout_timeout}" />
</bean>
<!-- 配置mybatisSqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis/mybatis.xml"/>
<property name="mapperLocations" value="classpath*:com/itclj/magpie/**/*Mapper.xml"/>
</bean>
<!-- 配置SqlSessionTemplate -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!-- 配置mybatis mapper接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.itclj.magpie.**.dao"/>
<property name="sqlSessionTemplateBeanName" value="sqlSessionTemplate"/>
</bean>
<!-- 事务配置 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 使用annotation注解方式配置事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
其中如下代码为spring自扫描所有dao包并把其下的所有mybatis接口文件装配入容器。然后可直接在Service层直接自动装配注入。
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.itclj.magpie.**.dao"/>
<property name="sqlSessionTemplateBeanName" value="sqlSessionTemplate"/>
</bean>
mybatis接口mapper举例如下:
public interface UserBeanMapper extends BaseMapper<UserBean,UserBeanExample,Long> {
}
Service层注入mybatis接口mapper的时候需要在构造方法中注入,这样注入mapper实例才不会空。
@Service(value = "userService")
public class UserServiceImpl extends BaseServiceImpl<UserBean, UserBeanExample, Long> implements UserService {
@Autowired()
public UserServiceImpl(UserBeanMapper userBeanMapper) {
this.setMapper(userBeanMapper);
}
}
工程目录结构如下图