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>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<!-- sqlserver -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.4</version>
</dependency>
<!-- oracle -->
<dependency>
<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格式看起来⽐较直观.
##端⼝
server:
port:8081
logging:
level:
cn.javabb.bootdemo.mapper: info
spring:
remote-datasource:
url: jdbc:sqlserver://66.66.66.66:1433;DatabaseName=dbName
username: sa
password: 123jpa mybatis
#使⽤Druid的数据源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
local-datasource:
url: jdbc:sqlserver://77.77.77.77:1433;DatabaseName=dbName
url: jdbc:sqlserver://77.77.77.77:1433;DatabaseName=dbName
username: sa
password: 1234
#使⽤Druid的数据源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
oracle-datasource:
url: jdbc:oracle:thin:@88.88.88.88:1521/sid
username: tiger
password: tiger
#使⽤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: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
#Mybatis
mybatis:
configuration:
map-underscore-to-camel-case: true
mapper-locations: classpath:mapper/*.xml
typeAliasesPackage: cn.javabb.**.entity
#Mapper
mapper:
mappers:
- cn.javabb.bootdemo.base.BaseMapper
not-empty: false
identity: sqlserver
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.session.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(basePackages = "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 sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource);
sessionFactoryBean.setMapperLocations(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
basePackages这⾥的包路径是需要⽤到这个数据源的路径,这个包下所有的接⼝⽂件都会使⽤这个local数据源
配置第⼆个数据源:remote-datasource
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论