实现:从一个数据库中的某张表中获取数据,插入到另一个数据库中的另一张表,两张表的数据结构不一致
基于网上找了很多篇内容,最终借用了https://cloud.tencent.com/developer/article/1447714的内容了数据源的配置,数据调用的方法通过注解,最终在实现的时候通过注解@Autowired 一个controller来实现方法内同时并存两个数据源,TNewsController里实现的
由于使用的数据库实例不是私人的,实体代码请自行替换
以下是项目的具体,数据库是mysql和mssql,所以文件也跟着区分了
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
@EnableSwagger2
public class DatasApplication {
public static void main(String[] args) {
SpringApplication.run(DatasApplication.class, args);
}
}
以下是pom文件代码
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.7.RELEASE
com
datas
0.0.1-SNAPSHOT
datas
war
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-aop
com.microsoft.sqlserver
mssql-jdbc
7.0.0.jre8
runtime
mysql
mysql-connector-java
5.1.22
runtime
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
com.baomidou
mybatis-plus-boot-starter
3.1.1
io.springfox
springfox-swagger2
2.9.2
io.springfox
springfox-swagger-ui
2.9.2
com.github.xiaoymin
swagger-bootstrap-ui
1.9.1
io.springfox
springfox-bean-validators
2.9.2
io.swagger
swagger-annotations
1.5.21
io.swagger
swagger-models
1.5.21
com.alibaba
druid-spring-boot-starter
1.1.10
org.springframework.boot
spring-boot-maven-plugin
src/main/resources
true
src/main/java
**/*.xml
**/*.yml
**/*.ftl
application.yml文件 数据库相关数据替换成自己的
server:
port: 5000
spring:
datasource:
dynamic:
primary: master #设置默认的数据源或者数据源组,默认值即为master
datasource:
master:
jdbc-url: jdbc:mysql://
username:
password:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
slave:
jdbc-url: jdbc:sqlserver://
username:
password:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
#mybatis plus 设置
mybatis-plus:
mapper-locations: classpath*:com/datas/**/mapper/xml/*Mapper.xml
global-config:
db-config:
logic-delete-value: 1 # 逻辑已删除值(默认为 1)
logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
config文件相关代码
import java.lang.annotation.*;
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DataSource {
String value();
}
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import javax.sql.DataSource;
import java.util.Map;
public class DynamicDataSource extends AbstractRoutingDataSource {
/**
* 如果不希望数据源在启动配置时就加载好,可以定制这个方法,从任何你希望的地方读取并返回数据源
* 比如从数据库、文件、外部接口等读取数据源信息,并最终返回一个DataSource实现类对象即可
*/
@Override
protected DataSource determineTargetDataSource() {
return super.determineTargetDataSource();
}
/**
* 如果希望所有数据源在启动配置时就加载好,这里通过设置数据源Key值来切换数据,定制这个方法
*/
@Override
protected Object determineCurrentLookupKey() {
return DynamicDataSourceContextHolder.getDataSourceKey();
}
/**
* 设置默认数据源
* @param defaultDataSource
*/
public void setDefaultDataSource(Object defaultDataSource) {
super.setDefaultTargetDataSource(defaultDataSource);
}
/**
* 设置数据源
* @param dataSources
*/
public void setDataSources(Map
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Aspect
@Order(-1) // 该切面应当先于 @Transactional 执行
@Component
public class DynamicDataSourceAspect {
/**
* 切换数据源
* @param point
* @param dataSource
*/
@Before("@annotation(dataSource))")
public void switchDataSource(JoinPoint point, DataSource dataSource) {
if (!DynamicDataSourceContextHolder.containDataSourceKey(dataSource.value())) {
System.out.println("DataSource [{}] doesn't exist, use default DataSource [{}] " + dataSource.value());
} else {
// 切换数据源
DynamicDataSourceContextHolder.setDataSourceKey(dataSource.value());
System.out.println("Switch DataSource to [" + DynamicDataSourceContextHolder.getDataSourceKey()
+ "] in Method [" + point.getSignature() + "]");
}
}
/**
* 重置数据源
* @param point
* @param dataSource
*/
@After("@annotation(dataSource))")
public void restoreDataSource(JoinPoint point, DataSource dataSource) {
// 将数据源置为默认数据源
DynamicDataSourceContextHolder.clearDataSourceKey();
System.out.println("Restore DataSource to [" + DynamicDataSourceContextHolder.getDataSourceKey()
+ "] in Method [" + point.getSignature() + "]");
}
}
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class DynamicDataSourceContextHolder {
private static final ThreadLocal contextHolder = new ThreadLocal() {
/**
* 将 master 数据源的 key作为默认数据源的 key
*/
@Override
protected String initialValue() {
return "master";
}
};
/**
* 数据源的 key集合,用于切换时判断数据源是否存在
*/
public static List
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
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 org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
@Configuration
@MapperScan(basePackages = "com.datas.**.mapper")
public class MybatisConfig {
@Value("${spring.datasource.dynamic.datasource.master.jdbc-url}")
private String url;
@Value("${spring.datasource.dynamic.datasource.master.username}")
private String user;
@Value("${spring.datasource.dynamic.datasource.master.password}")
private String password;
@Value("${spring.datasource.dynamic.datasource.master.driver-class-name}")
private String driverClass;
@Value("${spring.datasource.dynamic.datasource.slave.jdbc-url}")
private String surl;
@Value("${spring.datasource.dynamic.datasource.slave.username}")
private String suser;
@Value("${spring.datasource.dynamic.datasource.slave.password}")
private String spassword;
@Value("${spring.datasource.dynamic.datasource.slave.driver-class-name}")
private String sdriverClass;
@Bean("master")
@Primary
@ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.master")
public DataSource master() {
return DataSourceBuilder.create()
.driverClassName(driverClass)
.url(url)
.username(user)
.password(password)
.build();
}
@Bean("slave")
@ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.slave")
public DataSource slave() {
return DataSourceBuilder.create()
.driverClassName(sdriverClass)
.url(surl)
.username(suser)
.password(spassword)
.build();
}
@Bean("dynamicDataSource")
public DataSource dynamicDataSource() {
DynamicDataSource dynamicDataSource = new DynamicDataSource();
Map
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
import java.util.List;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.build()
.globalOperationParameters(setHeaderToken())
.apiInfo(apiInfo());
}
private List setHeaderToken() {
ParameterBuilder tokenPar = new ParameterBuilder();
List pars = new ArrayList<>();
tokenPar.name("Authorization").description("token").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
pars.add(tokenPar.build());
return pars;
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("datas")
.description("datas文档")
.termsOfServiceUrl("http://www")
.version("1.0.0")
.build();
}
}
mssql相关代码,mssql作查询,简单写了下
import com.datas.config.DataSource;
import com.datas.mssql.entity.NewsEntity;
import com.datas.mssql.service.NewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Api(tags ="mssql news")
@RestController
@RequestMapping("/mssql")
public class NewsController {
public NewsController(){}
@Autowired
private NewsService newsService;
@DataSource(value ="slave")
@GetMapping(value ="/id")
@ApiOperation("通过ID获取数据")
public NewsEntity getById(Integer id){
//通过重写方法获得实体
NewsEntity newsEntity =newsService.getById(id);
return newsEntity;
}
@DataSource(value ="slave")
@GetMapping(value ="/getid")
@ApiOperation("通过ID获取数据")
public NewsEntity getId(Integer id){
try{
//mabatis框架封装的方法
NewsEntity newsEntity =newsService.getById(id);
return newsEntity;
}catch (Exception e){
System.out.print(e);
return null;
}
}
}
实体类的代码只扒了一点点
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
@Data
@TableName("CMS_News")
public class NewsEntity implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 主键
*/
@TableId
private Integer id;
private String guid;
private String title;
}
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.datas.mssql.entity.NewsEntity;
import org.apache.ibatis.annotations.Select;
public interface NewsMapper extends BaseMapper {
@Select("select * from CMS_News where ID =#{id}")
NewsEntity getById(Integer id);
}
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.datas.config.DataSource;
import com.datas.mssql.entity.NewsEntity;
import com.datas.mssql.mapper.NewsMapper;
import com.datas.mssql.service.NewsService;
import org.springframework.stereotype.Service;
@Service
public class NewsServiceImpl extends ServiceImpl implements NewsService {
@Override
@DataSource("slave")
public NewsEntity getById(Integer id){
return this.baseMapper.getById(id);
}
}
import com.baomidou.mybatisplus.extension.service.IService;
import com.datas.mssql.entity.NewsEntity;
public interface NewsService extends IService {
NewsEntity getById(Integer id);
}
mysql文件代码
import com.datas.config.DataSource;
import com.datas.mssql.controller.NewsController;
import com.datas.mssql.entity.NewsEntity;
import com.datas.mysql.entity.TNewsEntity;
import com.datas.mysql.service.TNewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@Api(tags ="mysql news")
@RestController
@RequestMapping("/mysql")
public class TNewsController {
@Autowired
private TNewsService tnewsService;
@Autowired
private NewsController newsController;
@PutMapping(value ="/save{fromid}")
@DataSource("master")//注入数据源
@ApiOperation("通过mssql表里的ID进行同步操作")
public String saveByFromId(@PathVariable Integer fromid){
//从mssql表里获取数据
NewsEntity newsEntity =newsController.getById(fromid);
//封装mysql表里需要的数据
TNewsEntity tNewsEntity =new TNewsEntity();
tNewsEntity.setFromId(fromid);
tNewsEntity.setGuid(newsEntity.getGuid());
tNewsEntity.setTitle(newsEntity.getTitle());
tNewsEntity.setDescriptions(newsEntity.getDescriptions());
tNewsEntity.setContents(newsEntity.getContents());
try{
tnewsService.add(tNewsEntity);
return "ok";
}catch (Exception e){
System.out.print(e);
return e.getMessage();
}
}
}
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.io.Serializable;
@Data
@TableName("test_news")
public class TNewsEntity implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 主键
*/
@TableId
private Integer id;
private Integer fromId;
private String guid;
private String title;
private String descriptions;
private String contents;
}
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.datas.config.DataSource;
import com.datas.mysql.entity.TNewsEntity;
import org.apache.ibatis.annotations.Insert;
@DataSource("master")
public interface TNewsMapper extends BaseMapper {
@Insert("insert into test_news (From_Id,GUID,Title,Descriptions,Contents)" +
"values ( #{fromId},#{guid},#{title},#{descriptions},#{contents})")
void save(TNewsEntity tNewsEntity);
}
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.datas.config.DataSource;
import com.datas.mysql.entity.TNewsEntity;
import com.datas.mysql.mapper.TNewsMapper;
import com.datas.mysql.service.TNewsService;
import org.springframework.stereotype.Service;
@Service
public class TNewsServiceImpl extends ServiceImpl implements TNewsService {
@Override
@DataSource("master")
public void add(TNewsEntity entity){
baseMapper.save(entity);
}
}
import com.baomidou.mybatisplus.extension.service.IService;
import com.datas.mysql.entity.TNewsEntity;
public interface TNewsService extends IService {
void add(TNewsEntity entity);
}