Spring下mybatis多数据源配置

Spring下mybatis多数据源配置
介绍
本文描述了以mybatis作为持久层框架使用Spring的声明式事务时,如何配置多个数据源(即连接多个数据库),使用Spring的注解方式迚行依赖的注入和事务的管理。并且利用mybatis的spring插件自动扫描和装配Mapper接口。
先来看一个mybatis的单数据源配置

<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
<property name="alias" value="${alias}" />
<property name="driver" value="${driver}" />
<property name="driverUrl" value="${driverUrl}" />
<property name="user" value="${user}" />
<property name="password" value="${password}" />
<property name="houseKeepingTestSql" value="${house-keeping-test-sql}" />
<property name="maximumConnectionCount" value="${maximum-connection-count}" />
<property name="minimumConnectionCount" value="${minimum-connection-count}" />
<property name="prototypeCount" value="${prototype-count}" />
<property name="simultaneousBuildThrottle" value="${simultaneous-build-throttle}" />
<property name="trace" value="${trace}" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 自动扫描mapper接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="rhinoceros.persistence.dao" />
</bean>

说明
1. 数据库连接池使用Proxool,首先配置的就是Proxool的数据源
2. 接下来配置Spring的声明式事务管理,这里我们使用全注解+自动装配的方式迚行Bena和事务声明。
3. mybatis配置为自动扫描Maper接口,只要指定Mapper接口所在的包,需要注意的是,Mapper接口对应的映射文件(.xml)也要在这个包里,且名字和接口的名字一致。
使用多个数据源时,mybatis的文档表示丌能使用自动扫描和自动装配,但实际使用时发现还是可以的。看一个配置文件先:

<context:property-placeholder location="classpath:jdbc.properties" />
<!-- proxool连接池 -->
<bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
<property name="alias" value="${alias}" />
<property name="driver" value="${driver}" />
<property name="driverUrl" value="${driverUrl}" />
<property name="user" value="${user}" />
<property name="password" value="${password}" />
<property name="houseKeepingTestSql" value="${house-keeping-test-sql}" />
<property name="maximumConnectionCount" value="${maximum-connection-count}" />
<property name="minimumConnectionCount" value="${minimum-connection-count}" />
<property name="prototypeCount" value="${prototype-count}" />
<property name="simultaneousBuildThrottle" value="${simultaneous-build-throttle}" />
<property name="trace" value="${trace}" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 注解式事务管理 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 自动扫描 mybatis mapper接口 -->
<bean name="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="rhino.persistence.mapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!-- 配置新的的数据源 -->
<bean id="dataSource_1" class="org.logicalcobwebs.proxool.ProxoolDataSource">
<property name="alias" value="${db1.alias}" />
<property name="driver" value="${driver}" />
<property name="driverUrl" value="${db1.driverUrl}" />
<property name="user" value="${db1.user}" />
<property name="password" value="${db1.password}" />
<property name="houseKeepingTestSql" value="${house-keeping-test-sql}" />
<property name="maximumConnectionCount" value="${maximum-connection-count}" />
<property name="minimumConnectionCount" value="${minimum-connection-count}" />
<property name="prototypeCount" value="${prototype-count}" />
<property name="simultaneousBuildThrottle" value="${simultaneous-build-throttle}" />
<property name="trace" value="${trace}" />
</bean>
<bean id="sqlSessionFactory_1" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource_1" />
<property name="configLocation" value="classpath:mybatis-config-db1.xml"/>
</bean>
<bean id="transactionManager_1"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource_1" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager_1" />
<!-- 自动扫描 mybatis mapper接口 -->
<bean name="mapperScannerConfigurer_1" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value=rhino.persistence.db1" />
<property name="sqlSessionFactory" ref="sqlSessionFactory_1" />
</bean>
<!-- spring beans 组件扫描 -->
<context:component-scan base-package="rhino.domain" />

说明
1. 需要配置新的数据源、事务管理器、sqlSession工厂
2. mybatis配置文件也要独立出来,即2个数据源应该有2个mybatis配置文件,互相之间是独立的,注意配置sqlSessionFactory_1时注入的mybatis-config-db1.xml
3. Mapper接口要放在一个新的包里
4. 2个以上的数据源同样配置

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中使用MyBatis实现多数据的方式有很多种,下面介绍两种常用的方法。 方法一:使用Spring Boot的自动配置 1.在application.yml或application.properties中定义多个数据配置信息,如下所示: ``` # 数据1 spring.datasource.master.jdbc-url=jdbc:mysql://localhost:3306/db_master spring.datasource.master.username=root spring.datasource.master.password=123456 spring.datasource.master.driver-class-name=com.mysql.jdbc.Driver # 数据2 spring.datasource.slave.jdbc-url=jdbc:mysql://localhost:3306/db_slave spring.datasource.slave.username=root spring.datasource.slave.password=123456 spring.datasource.slave.driver-class-name=com.mysql.jdbc.Driver ``` 2.创建多个数据的连接池和SqlSessionFactory,可以使用Spring Boot自动配置的方式来实现。只需要在配置类上添加@MapperScan注解即可,如下所示: ``` @Configuration @MapperScan(basePackages = "com.example.mapper") public class MybatisConfig { @Bean @ConfigurationProperties(prefix="spring.datasource.master") public DataSource masterDataSource() { return DataSourceBuilder.create().build(); } @Bean @ConfigurationProperties(prefix="spring.datasource.slave") public DataSource slaveDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "masterSqlSessionFactory") public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(masterDataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/master/*.xml")); return bean.getObject(); } @Bean(name = "slaveSqlSessionFactory") public SqlSessionFactory slaveSqlSessionFactory(@Qualifier("slaveDataSource") DataSource slaveDataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(slaveDataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/slave/*.xml")); return bean.getObject(); } @Bean(name = "masterSqlSessionTemplate") public SqlSessionTemplate masterSqlSessionTemplate(@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } @Bean(name = "slaveSqlSessionTemplate") public SqlSessionTemplate slaveSqlSessionTemplate(@Qualifier("slaveSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } } ``` 3.在Mapper接口中使用@Qualifier注解来指定使用哪个数据,如下所示: ``` public interface UserMapper { @Select("select * from user") @Qualifier("masterSqlSessionFactory") List<User> selectAllMaster(); @Select("select * from user") @Qualifier("slaveSqlSessionFactory") List<User> selectAllSlave(); } ``` 方法二:手动配置多个数据 1.定义多个数据配置信息,如下所示: ``` @Configuration public class DataSourceConfig { @Bean(name = "masterDataSource") @ConfigurationProperties(prefix="spring.datasource.master") public DataSource masterDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "slaveDataSource") @ConfigurationProperties(prefix="spring.datasource.slave") public DataSource slaveDataSource() { return DataSourceBuilder.create().build(); } } ``` 2.创建多个SqlSessionFactory,如下所示: ``` @Configuration public class MybatisConfig { @Autowired @Qualifier("masterDataSource") private DataSource masterDataSource; @Autowired @Qualifier("slaveDataSource") private DataSource slaveDataSource; @Bean(name = "masterSqlSessionFactory") public SqlSessionFactory masterSqlSessionFactory() throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(masterDataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/master/*.xml")); return bean.getObject(); } @Bean(name = "slaveSqlSessionFactory") public SqlSessionFactory slaveSqlSessionFactory() throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(slaveDataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/slave/*.xml")); return bean.getObject(); } @Bean(name = "masterSqlSessionTemplate") public SqlSessionTemplate masterSqlSessionTemplate(@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } @Bean(name = "slaveSqlSessionTemplate") public SqlSessionTemplate slaveSqlSessionTemplate(@Qualifier("slaveSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } } ``` 3.在Mapper接口中使用@Qualifier注解来指定使用哪个数据,如下所示: ``` public interface UserMapper { @Select("select * from user") @Qualifier("masterSqlSessionFactory") List<User> selectAllMaster(); @Select("select * from user") @Qualifier("slaveSqlSessionFactory") List<User> selectAllSlave(); } ``` 以上就是Spring Boot中使用MyBatis实现多数据的两种常用方法,具体使用哪种方法,可以根据自己的实际情况来选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值