Springboot+Mybatis+通用Mapper多数据源实现数据同步

更新时间:2023-05-21 12:45:40 阅读: 评论:0

Springboot+Mybatis+通⽤Mapper多数据源实现数据同步有个需求需要查询远程数据库,然后将远程数据库中的数据抓取到本地,远程数据库中的数据是每天都增加的,所以就需要写个程序⾃动实现实时抓取。
这⾥我⽤到的框架是Springboot2.0+Mybatis+Mapper,涉及的数据库有SqlServer,Oracle,Mysql
添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
渔歌子古诗翻译<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId&batis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!--<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>usher in
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<!-- sqlrver -->
<dependency>
<groupId>com.microsoft.sqlrver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.4</version>
</dependency>
<!-- oracle -->
<dependency>
2010年六级考试时间
<dependency>
<groupId&acle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.4.0</version>
</dependency>
<!-- 通⽤Maper -->
<dependency>
<groupId&batis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.2.4</version>
</dependency>
<!--druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.28</version>
</dependency>
<!-- log4j -->
<dependency>
<groupId>org.slf4j</groupId>
做鬼脸的英文<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
如果没有log包,可能在配置⽂件中的DruidDataSource会出现错误提⽰,但实际上不会影响到程序。
也可以⽤application.properties格式配置,但是感觉yml格式看起来⽐较直观.
##端⼝
rver:
port:8081
logging:
level:
cn.javabb.bootdemo.mapper: info
spring:
remote-datasource:
url: jdbc:sqlrver://66.66.66.66:1433;DatabaName=dbName
urname: sa
password: 123
#使⽤Druid的数据源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.microsoft.sqlrver.jdbc.SQLServerDriver
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
八天英语
validationQuery: lect 'x'
testWhileIdle: true
testOnBorrow: fal
testOnReturn: fal
poolPreparedStatements: true
maxOpenPreparedStatements: 20
local-datasource:
url: jdbc:sqlrver://77.77.77.77:1433;DatabaName=dbName
url: jdbc:sqlrver://77.77.77.77:1433;DatabaName=dbName
urname: sa
password: 1234
#使⽤Druid的数据源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.microsoft.sqlrver.jdbc.SQLServerDriver
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: lect 'x'
testWhileIdle: true
testOnBorrow: fal
testOnReturn: fal
poolPreparedStatements: true
maxOpenPreparedStatements: 20
oracle-datasource:
url: jdbc:oracle:thin:@88.88.88.88:1521/sid
urname: tiger
password: tigercroquis
白雪公主>环保发言稿#使⽤Druid的数据源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: oracle.jdbc.driver.OracleDriver
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: lect 'x'
testWhileIdle: true
testOnBorrow: fal
testOnReturn: fal
poolPreparedStatements: true
maxOpenPreparedStatements: 20
#Mybatis
mybatis:
configuration:
map-underscore-to-camel-ca: true
mapper-locations: classpath:mapper/*.xml
typeAliasPackage: cn.javabb.**.entity
#Mapper
mapper:
mappers:
- cn.javabb.bootdemo.ba.BaMapper
not-empty: fal
identity: sqlrver
before: true
其中remote-datasource,oracle-datasource和local-datasource为两个不同的数据源,mybatis配置和通⽤mapper配置⼀样的,如果不需要⽤到通⽤mapper可以去掉配置。
application启动类:
@EnableAsync
@EnableScheduling
@ServletComponentScan
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class,DataSourceTransactionManagerAutoConfiguration.class, MybatisAutoConfiguration.class public class BootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(BootDemoApplication.class, args);
}
}
注解说明:
@EnableAsync:开启异步任务⽀持,如果不需要可以去掉
@EnableScheduling:开启定时任务⽀持
@ServletComponentScan:⽤来扫描mapper
@SpringBootApplication:springboot注解⽀持
注意:如果项⽬依赖中有,mybatis-spring-boot-starter,会默认⾃动根据配置⽂件配置mybatis,如果刚开始项⽬没有配置
DataSource,启动项⽬就会报错,在这⾥去掉DataSource的默认⾃动配置类不加载,使项⽬在使⽤的时候选择去DataSource。
数据源配置
有⼏个数据源配置⼏个DataSource
配置第⼀个数据源:local-datasource
import org.apache.ibatis.ssion.SqlSessionFactory;
batis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.t.properties.ConfigurationProperties;
import t.annotation.Bean;
import t.annotation.Configuration;
import io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
batis.spring.annotation.MapperScan;
英汉翻译器import javax.sql.DataSource;
@Configuration
@MapperScan(baPackages = "cn.javabb.bootdemo.mapper.local",sqlSessionFactoryRef = "localSqlSessionFactory")
public class LocalDataSourceConfig {
@Bean(name = "localDataSource")
@ConfigurationProperties("spring.local-datasource")
public DataSource localDataSource(){
ate().build();
}
@Bean(name = "localSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("localDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean ssionFactoryBean = new SqlSessionFactoryBean();
ssionFactoryBean.tDataSource(dataSource);
经典童谣ssionFactoryBean.tMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath*:mapper/local/*.xml"));
Object();
}
@Bean(name = "localTransactionManager")
public DataSourceTransactionManager localTransactionManager(){
return new DataSourceTransactionManager(localDataSource());
}
}
spring.local-datasource 为l中配置的数据源名称
classpath*:mapper/local/*.xml 指定local数据源的mybatis的xml⽂件位置
原本⼀个数据源的时候,可以在启动程序的时候加载DataSource,但是这⾥是多数据源,需要为每个数据源配置DataSource,并且需要添加MapperScan注解,(因为这⾥是使⽤的通⽤Mapper,所以引⼊的class应该是mapper的,import
baPackages这⾥的包路径是需要⽤到这个数据源的路径,这个包下所有的接⼝⽂件都会使⽤这个local数据源
配置第⼆个数据源:remote-datasource

本文发布于:2023-05-21 12:45:40,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/78/719644.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:配置   数据源   需要   数据库   数据
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图