#数据迁移#
一、创建一个demo项目(spring boot +mybatis-plus)
二、创建相关目录结构
以下是完整目录截图
三、配置application.properties
server.port=8080
spring.datasource1.driver-class-name=com.mysql.jdbc.Driver
spring.datasource1.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource1.jdbc-url=jdbc:mysql://127.0.0.1:3306/test1?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
spring.datasource1.username=root
spring.datasource1.password=root
spring.datasource2.driver-class-name=com.mysql.jdbc.Driver
spring.datasource2.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource2.jdbc-url=jdbc:mysql://127.0.0.1:3306/test2?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
spring.datasource2.username=root
spring.datasource2.password=root
mybatis-plus.mapper-locations=classpath:mapper/*.xml
mybatis-plus.type-aliases-package=com.example.demo.entity
mybatis-plus.configuration.map-underscore-to-camel-case=true
四、配置DataSource
配置 DataSource1Config
package com.example.demo.config;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.zaxxer.hikari.HikariDataSource;
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;
@Configuration
@MapperScan(basePackages = "com.example.demo.mapper1", sqlSessionFactoryRef = "schoolSqlSessionFactory")
public class DataSource1Config {
@Bean
@ConfigurationProperties("spring.datasource1")
public HikariDataSource dataSource1() {
return new HikariDataSource();
}
/**
* 创建SessionFactory
*/
@Bean
@Primary
public SqlSessionFactory schoolSqlSessionFactory(@Qualifier("dataSource1") DataSource dataSource1) throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource1);
//设置mapper配置文件
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper1/*.xml"));
return bean.getObject();
}
/**
* 创建事务管理器
*/
@Bean
@Primary
public DataSourceTransactionManager schoolTransactionManger(@Qualifier("dataSource1") DataSource dataSource1) {
return new DataSourceTransactionManager(dataSource1);
}
/**
* 创建SqlSessionTemplate
*/
@Bean
@Primary
public SqlSessionTemplate sqlSessionTemplate1(@Qualifier("schoolSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
配置 DataSource2Config
package com.example.demo.config;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
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.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;
@Configuration
@MapperScan(basePackages = "com.example.demo.mapper2", sqlSessionFactoryRef = "tradeSqlSessionFactory")
public class DataSource2Config {
@Bean
@ConfigurationProperties("spring.datasource2")
public HikariDataSource dataSource2() {
return new HikariDataSource();
}
/**
* 创建SessionFactory
*/
@Bean
@Primary
public SqlSessionFactory tradeSqlSessionFactory(@Qualifier("dataSource2") DataSource dataSource2) throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource2);
//设置mapper配置文件
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper2/*.xml"));
return bean.getObject();
}
/**
* 创建事务管理器
*/
@Bean
@Primary
public DataSourceTransactionManager tradeTransactionManger(@Qualifier("dataSource2") DataSource dataSource2) {
return new DataSourceTransactionManager(dataSource2);
}
/**
* 创建SqlSessionTemplate
*/
@Bean
@Primary
public SqlSessionTemplate sqlSessionTemplate2(@Qualifier("tradeSqlSessionFactory") SqlSessionFactory sqlSessionFactory2) {
return new SqlSessionTemplate(sqlSessionFactory2);
}
}
五、创建实体对象
LiveSettleEntity
package com.example.demo.pojo.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
@Getter
@Setter
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
@TableName("bg_trade_live_settle")
public class LiveSettleEntity{
@TableField("live_id")
private Long liveId;
}
ServicerInfoEntity
package com.example.demo.pojo.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
@Getter
@Setter
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
@TableName("bg_school_servicer_info")
public class ServicerInfoEntity {
/** 客服的显示名称 **/
@TableField("servicer_name")
private String servicerName;
}
六、创建mapper1\mapper2对象
mapper1
package com.example.demo.mapper1;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.pojo.entity.ServicerInfoEntity;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface SchoolMapper extends BaseMapper<ServicerInfoEntity> {
}
mapper2
package com.example.demo.mapper2;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.pojo.entity.LiveSettleEntity;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface TradeMapper extends BaseMapper<LiveSettleEntity> {
}
七、创建service对象
SchoolServiceImpl
package com.example.demo.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.pojo.entity.ServicerInfoEntity;
import com.example.demo.mapper1.SchoolMapper;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class SchoolServiceImpl extends ServiceImpl<SchoolMapper, ServicerInfoEntity> {
public List<ServicerInfoEntity> getList(){
QueryWrapper<ServicerInfoEntity> queryWrapper=new QueryWrapper<>();
return baseMapper.selectList(queryWrapper);
}
}
TradeServiceImpl
package com.example.demo.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.pojo.entity.LiveSettleEntity;
import com.example.demo.mapper2.TradeMapper;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class TradeServiceImpl extends ServiceImpl<TradeMapper, LiveSettleEntity> {
public List<LiveSettleEntity> getList(){
QueryWrapper<LiveSettleEntity> queryWrapper=new QueryWrapper<>();
return baseMapper.selectList(queryWrapper);
}
}
八、创建TestController
package com.example.demo.controller;
import com.example.demo.pojo.entity.LiveSettleEntity;
import com.example.demo.pojo.entity.ServicerInfoEntity;
import com.example.demo.pojo.vo.TestVo;
import com.example.demo.service.SchoolServiceImpl;
import com.example.demo.service.TradeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Objects;
@RestController
@RequestMapping("test")
public class TestController {
@Autowired
private SchoolServiceImpl schoolService;
@Autowired
private TradeServiceImpl tradeService;
@RequestMapping("/list")
public TestVo getTests(){
TestVo vo=new TestVo();
List<ServicerInfoEntity> serviceInfo=schoolService.getList();
List<LiveSettleEntity> live=tradeService.getList();
if(Objects.nonNull(serviceInfo) && serviceInfo.size()>0){
vo.setServicerName(serviceInfo.get(0).getServicerName());
}
if(Objects.nonNull(live) && live.size()>0){
vo.setLiveId(live.get(0).getLiveId());
}
return vo;
}
}