因为网上的很多都不能用,所以做完之后,就把自己的代码上来
直接上代码 :
pom :
4.0.0
com.shsany
Myboot
0.0.1-SNAPSHOT
jar
Myboot
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.2.RELEASE
UTF-8
UTF-8
1.8
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
io.netty
netty-all
4.1.12.Final
com.alibaba
druid
1.0.5
com.microsoft.sqlserver
mssql-jdbc
6.1.0.jre8
com.mchange
c3p0
0.9.5.2
com.fasterxml.jackson.core
jackson-core
com.fasterxml.jackson.core
jackson-databind
2.6.4
com.fasterxml.jackson.core
jackson-annotations
2.6.4
mysql
mysql-connector-java
8.0.11
org.springframework.boot
spring-boot-starter-data-redis
commons-io
commons-io
2.4
org.projectlombok
lombok
1.16.18
org.springframework.boot
spring-boot-maven-plugin
com.shsany.MybootApplication
repackage
src/main/java
**/*.xml
${project.basedir}/src/main/resources
application.yml:
spring:
datasource:
mysql:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
username: root
password: root
sqlserver:
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc-url: jdbc:sqlserver://localhost:1433;databaseName=test1
username: shsany
password: goodluck
main:
allow-bean-definition-overriding: true
这里做完就需要我们再做两个配置类: MysqlConfig:
package com.shsany.config;
import javax.sql.DataSource;
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.shsany.dao”, sqlSessionFactoryRef = “masterSqlSessionFactory”)
public class MysqlConfig {
@Primary
@Bean(name = “masterDataSource”)
@ConfigurationProperties( prefix = “spring.datasource.mysql”)
public DataSource dataSource() {
DataSource dataSource = DataSourceBuilder.create().build();
return dataSource;
}
@Primary
@Bean(name = "masterSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
// factoryBean.setTypeAliasesPackage("com.pactera.scm.entity");
factoryBean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath:com/shsany/dao/*.xml"));
return factoryBean.getObject();
}
@Primary
@Bean(name = "masterTransactionManager")
public DataSourceTransactionManager transactionManager(@Qualifier("masterDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "masterSqlSessionTemplate")
@Primary
public SqlSessionTemplate testSqlSessionTemplate(
@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
SqlserverConfig:
package com.shsany.config;
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.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = “com.shsany.mapper”, sqlSessionFactoryRef = “otherSqlSessionFactory”)
public class SqlserverConfig {
@Bean(name = “otherDataSource”)
@ConfigurationProperties(prefix = “spring.datasource.sqlserver”)
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "otherTransactionManager")
public DataSourceTransactionManager transactionManager(@Qualifier("otherDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "otherSqlSessionFactory")
public SqlSessionFactory basicSqlSessionFactory(@Qualifier("otherDataSource") DataSource basicDataSource) throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(basicDataSource);
factoryBean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath:com/shsany/mapper/*.xml"));
return factoryBean.getObject();
}
@Bean(name = "otherSqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(
@Qualifier("otherSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
dao and mapper
**这里再添加dao 和mapper 包, 就不上代码了, 写了个定时方法,然后分别调用dao, 和mapper 中的方法,
@Component
public class MyTask {
@Autowired
private MysqlUserMapper mysqlUserMapper;
@Autowired
private SqlServerUserMapper sqlServerUserMapper;
@Scheduled(fixedRate=10000)
public void consumeRealTimeData(){
Listusers= mysqlUserMapper.findUser();
System.out.println("mysql 查出来的结果=="+users);
}
@Scheduled(fixedRate=15000)
public void consumeRealTimeData1(){
Listusers= sqlServerUserMapper.findUser();
System.out.println("sqlServer 查出来的结果=="+users);
}
ok , 完事