数据迁移需要同时访问两个数据库,数据从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的事务管理器。