博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring boot + mybatis 同时访问多数据源
阅读量:6416 次
发布时间:2019-06-23

本文共 5496 字,大约阅读时间需要 18 分钟。

hot3.png

数据迁移需要同时访问两个数据库,数据从jx迁移到my。

jdbc配置在yml配置文件添加:

application.yml  

jdbc:  jx:    jdbc-url: jdbc:mysql://localhost:3306/jxjr?useSSL=false    username: root    password: 123456    driver-class-name: com.mysql.jdbc.Driver  my:    jdbc-url: jdbc:mysql://localhost:3306/mylc?useSSL=false    username: root    password: 123456    driver-class-name: com.mysql.jdbc.Driver

第一个数据源配置:

gm.mvdt.dataSources.JxDataSourceConfig

package gm.mvdt.dataSources;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;import org.mybatis.spring.SqlSessionTemplate;import org.mybatis.spring.annotation.MapperScan;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.boot.jdbc.DataSourceBuilder;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import javax.sql.DataSource;/** * @Description: jx数据源配置 * @Author: 张颖辉(yh) * @CreateDate: 2018/11/8 20:19 * @UpdateUser: 张颖辉(yh) * @UpdateDate: 2018/11/8 20:19 * @UpdateRemark: The modified content * @Version: 1.0 */@Configuration@MapperScan(basePackages = "gm.mvdt.mapper.jx", sqlSessionTemplateRef  = "jxSqlSessionTemplate")public class JxDataSourceConfig {    @Bean(name = "jxDataSource")    @ConfigurationProperties(prefix = "jdbc.jx")    //@Primary    public DataSource jxDataSource() {        return DataSourceBuilder.create().build();    }    @Bean(name = "jxSqlSessionFactory")    //@Primary    public SqlSessionFactory jxSqlSessionFactory(@Qualifier("jxDataSource") DataSource dataSource) throws Exception {        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();        bean.setDataSource(dataSource);        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/jx/*.xml"));        //开启驼峰        bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);        return bean.getObject();    }    @Bean(name = "jxTransactionManager")    //@Primary    public DataSourceTransactionManager jxTransactionManager(@Qualifier("jxDataSource") DataSource dataSource) {        return new DataSourceTransactionManager(dataSource);    }    @Bean(name = "jxSqlSessionTemplate")    //@Primary    public SqlSessionTemplate jxSqlSessionTemplate(@Qualifier("jxSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {        return new SqlSessionTemplate(sqlSessionFactory);    }}

第二个数据源配置:

gm.mvdt.dataSources.MyDataSourceConfig  

package gm.mvdt.dataSources;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;import org.mybatis.spring.SqlSessionTemplate;import org.mybatis.spring.annotation.MapperScan;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.boot.jdbc.DataSourceBuilder;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import javax.sql.DataSource;/** * @Description: my数据源配置 * @Author: 张颖辉(yh) * @CreateDate: 2018/11/8 20:19 * @UpdateUser: 张颖辉(yh) * @UpdateDate: 2018/11/8 20:19 * @UpdateRemark: The modified content * @Version: 1.0 */@Configuration@MapperScan(basePackages = "gm.mvdt.mapper.my", sqlSessionTemplateRef  = "mySqlSessionTemplate")public class MyDataSourceConfig {    @Bean(name = "myDataSource")    @ConfigurationProperties(prefix = "jdbc.my")    //@Primary    public DataSource myDataSource() {        return DataSourceBuilder.create().build();    }    @Bean(name = "mySqlSessionFactory")    //@Primary    public SqlSessionFactory mySqlSessionFactory(@Qualifier("myDataSource") DataSource dataSource) throws Exception {        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();        bean.setDataSource(dataSource);        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/my/*.xml"));        //开启驼峰        bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);        return bean.getObject();    }    @Bean(name = "myTransactionManager")    //@Primary    public DataSourceTransactionManager myTransactionManager(@Qualifier("myDataSource") DataSource dataSource) {        return new DataSourceTransactionManager(dataSource);    }    @Bean(name = "mySqlSessionTemplate")    //@Primary    public SqlSessionTemplate mySqlSessionTemplate(@Qualifier("mySqlSessionFactory") SqlSessionFactory sqlSessionFactory){        return new SqlSessionTemplate(sqlSessionFactory);    }}

 

/**     * @Description: 数据迁移     * @Author: 张颖辉(yh)     * @Date: 2018/11/12 15:12     * @param: [userId]     * @return: java.util.List
* @Version: 1.0 */ @Override @Transactional(value = "myTransactionManager", rollbackFor = Exception.class) public void mvdt() {//迁移代码}

注意:

 @Transactional(value = "myTransactionManager", rollbackFor = Exception.class)

这里可以使用事务,如果迁移过程中出错,数据会自动回滚,但是只能指定一个事务管理器。因为jx库我只是读取不需要开始事务,这里我设置的事my的事务管理器。 

转载于:https://my.oschina.net/iyinghui/blog/2988579

你可能感兴趣的文章
Disruptor-NET和内存栅栏
查看>>
Windows平台ipod touch/iphone等共享笔记本无线上网设置大全
查看>>
播放加密DVD
查看>>
产品设计体会(3013)项目的“敏捷沟通”实践
查看>>
RHEL6.3基本网络配置(1)ifconfig命令
查看>>
网络诊断工具之—路由追踪tracert命令
查看>>
Java模拟HTTP的Get和Post请求(增强)
查看>>
php 环境搭建(windows php+apache)
查看>>
让虚拟机的软盘盘符不显示(适用于所有windows系统包括Windows Server)
查看>>
Cygwin不好用
查看>>
jQuery插件之验证控件jquery.validate.js
查看>>
[经验]无线鼠标和无线键盘真的不能用了?——雷柏的重生之路~
查看>>
【转】plist涉及到沙盒的一个问题
查看>>
GNU make manual 翻译( 一百四十五)
查看>>
重构之美-走在Web标准化设计的路上[复杂表单]3 9 Update
查看>>
linux中的优先搜索树的实现--prio_tree【转】
查看>>
重构之美-跨越Web标准,触碰语义网[开门见山:Microformat]
查看>>
git入门与实践【转】
查看>>
WPF 虚拟键盘
查看>>
储存卡无法打开专家教您怎么数据恢复
查看>>