多数据源配置spting-mybatis
来源:互联网 发布:飞秋for mac 编辑:程序博客网 时间:2024/06/11 05:57
在一个项目中,如果所需的数据来自于不同的数据库,这时候就需要多数据源的配置,本文用的mysql数据库,整合了mybatis,具体配置如下:
spring配置文件
需要配置两个dataSource,两个SqlSessionFactoryBean,两个扫描mapper的MapperScannerConfigurer
<bean id="dataSource1" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="driverClassName" value="${jdbc.driver}" />
<property name="maxActive" value="10" />
<property name="minIdle" value="5" />
</bean>
<bean id="dataSource2" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="url" value="${jdbcbi.urlbi}" />
<property name="username" value="${jdbcbi.usernamebi}" />
<property name="password" value="${jdbcbi.passwordbi}" />
<property name="driverClassName" value="${jdbcbi.driverbi}" />
<property name="maxActive" value="10" />
<property name="minIdle" value="5" />
</bean>
<bean id="sqlSessionFactory1" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource1" />
</bean>
<bean id="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource2" />
</bean>
<!--两个MapperScannerConfigurer注入不同的factory-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描同一个包-->
<property name="basePackage" value="com.suneee.business.mapper" />
<!-- 注入不同的SessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory1"/>
<!-- 使用自定义注解,携带数据源,需要在Mapper层加上注解@DataSource1 -->
<property name="annotationClass" value="com.suneee.business.annotation.DataSource1"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.suneee.business.mapper" />
<!-- 注入不同的SessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory2"/>
<!-- 使用自定义注解,携带数据源,需要在Mapper层加上注解@DataSource2 -->
<property name="annotationClass" value="com.suneee.business.annotation.DataSource2"/>
</bean>
自定义注解
新生成两个自定义注解
package com.suneee.business.annotation;
public @interface DataSource1 {
}
package com.suneee.business.annotation;
public @interface DataSource2{
}
注入数据源
在不同的mapper中使用不同的注解,就可以实现注入不同的数据源
@DataSource1
public interface UserMapper(){
}
@DataSource2
public interface ProductMapper(){
}
阅读全文
0 0
- 多数据源配置spting-mybatis
- mybatis多数据源配置
- mybatis配置多数据源
- MyBatis 多数据源配置
- mybatis 多数据源配置
- mybatis多数据源配置
- MyBatis多数据源配置
- spring + mybatis多数据源配置
- spring MVC+MyBatis 多数据源配置
- Spring下mybatis 多数据源配置
- springmvc mybatis多数据源配置
- Spring下mybatis配置多数据源
- Spring+Mybatis 多数据源配置
- MyBatis多数据源配置(读写分离)
- Spring+MyBatis多数据源配置实现
- spring+mybatis多数据源配置
- SpringMvc+Mybatis多数据源配置
- Spring+MyBatis多数据源配置实现
- Oc GDataXML(DOM)解析分区表格
- ARM9 S3C2451 UART 理解
- Oc NSXMLParse(SAX)解析表格
- 编译caffe和matlab接口问题
- An_MVP_模式(okhttp)解析RecyclerView/XRecyclerView
- 多数据源配置spting-mybatis
- Oc NSXMLParse(SAX)解析分区表格
- echarts相关设置,看注释
- LeetCode 679: 24 Game 解题与思考
- 5
- Oc Json解析 表格
- 第八周项目二——顺序串算法
- 链表问题 删除链表的中间节点和a/b处的节点
- Eigen之矩阵、向量、标量的操作运算