1、编写配置文件 application.yml
spring:
datasource:
primary:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://ip:5432/iotdata
username: postgre
password: postgre123
type: com.zaxxer.hikari.HikariDataSource
maximum-pool-size: 5
minimum-idle: 1
connection-test-query: SELECT 1
pool-name: DatebookHikariCP1
connection-timeout: 30000
idle-timeout: 30000
max-lifetime: 1800000
secondary:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://ip:5432/safety
username: postgre
password: postgre123
type: com.zaxxer.hikari.HikariDataSource
maximum-pool-size: 5
minimum-idle: 1
connection-test-query: SELECT 1
pool-name: DatebookHikariCP2
connection-timeout: 30000
idle-timeout: 30000
max-lifetime: 1800000
2、写配置映射实体 DbPropertyConfig
import lombok.Data;
@Data
public class DbPropertyConfig {
private String poolName;
private boolean autoCommit;
private long connectionTimeout;
private long idleTimeout;
private long maxLifetime;
private int maximumPoolSize;
private int minimumIdle;
private String connectionTestQuery;
private String type;
private String password;
private String username;
private String url;
private String driverClassName;
}
3、数据源primary配置
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
* Created by tao.zeng on 2019-03-20.
*/
@Data
@Configuration
@EqualsAndHashCode(callSuper = true)
@ConfigurationProperties(prefix = "spring.datasource.primary")
public class PrimaryDbConfig extends DbPropertyConfig {
}
4、数据源secondary配置
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
* secondary 数据库配置
*/
@Data
@Configuration
@EqualsAndHashCode(callSuper = true)
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public class SecondaryDbConfig extends DbPropertyConfig {
}
5、数据源primary配置类
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.commons.lang3.ArrayUtils;
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.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* Created by tao.zeng on 2019-03-19.
*/
@Configuration
@MapperScan(basePackages =
{
"com..mapper",
"com.sys.mapper",
}
, sqlSessionTemplateRef = "primarySessionTemplate")
public class PrimaryDataSourceConfigure {
private PrimaryDbConfig mysqlConfig;
public PrimaryDataSourceConfigure(PrimaryDbConfig mysqlConfig) {
this.mysqlConfig = mysqlConfig;
}
@Bean(name = "primaryDataSource")
@ConfigurationProperties("spring.datasource.primary")
public DataSource primaryDataSource() {
DataSource dataSource = DataSourceBuilder.create().build();
HikariDataSource hikariDataSource = null;
if (dataSource instanceof HikariDataSource) {
// 连接池配置
hikariDataSource = (HikariDataSource) dataSource;
hikariDataSource.setPoolName(mysqlConfig.getPoolName());
hikariDataSource.setAutoCommit(mysqlConfig.isAutoCommit());
hikariDataSource.setConnectionTestQuery(mysqlConfig.getConnectionTestQuery());
hikariDataSource.setIdleTimeout(mysqlConfig.getIdleTimeout());
hikariDataSource.setConnectionTimeout(mysqlConfig.getConnectionTimeout());
hikariDataSource.setMaximumPoolSize(mysqlConfig.getMaximumPoolSize());
hikariDataSource.setMaxLifetime(mysqlConfig.getMaxLifetime());
hikariDataSource.setMinimumIdle(mysqlConfig.getMinimumIdle());
hikariDataSource.setJdbcUrl( mysqlConfig.getUrl());
hikariDataSource.setUsername(mysqlConfig.getUsername() );
hikariDataSource.setPassword(mysqlConfig.getPassword());
}
return hikariDataSource == null ? dataSource : hikariDataSource;
}
@Bean(name = "primarySessionFactory")
public SqlSessionFactory primarySessionFactory(@Qualifier("primaryDataSource") DataSource primaryDataSource) throws Exception {
// MyBatis-Plus使用MybatisSqlSessionFactoryBean MyBatis直接使用SqlSessionFactoryBean
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
// 给MyBatis-Plus注入数据源
bean.setDataSource(primaryDataSource);
bean.setPlugins(paginationInterceptor()); //添加分页插件
Resource[] resources = new PathMatchingResourcePatternResolver().
getResources("classpath:com/路径/mapping/*.xml");
Resource[] resources2 = new PathMatchingResourcePatternResolver().
getResources("classpath:com/路径/mapping/*.xml");
Resource[] resources1 = ArrayUtils.addAll(resources, resources2);
bean.setMapperLocations(resources1);
return bean.getObject();
}
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
@Bean(name = "primaryTransactionManager")
public DataSourceTransactionManager primaryTransactionManager(@Qualifier("primaryDataSource") DataSource primaryDataSource) {
return new DataSourceTransactionManager(primaryDataSource);
}
@Bean(name = "primarySessionTemplate")
public SqlSessionTemplate primarySessionTemplate(@Qualifier("primarySessionFactory") SqlSessionFactory primarySessionFactory) {
return new SqlSessionTemplate(primarySessionFactory);
}
}
6、数据源 secondary配置类
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
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.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* SecondaryDbConfig
* secondary
*/
@Configuration
@MapperScan(basePackages = "com.safety", sqlSessionTemplateRef = "secondarySessionTemplate")
public class SecondaryDataSourceConfigure {
private SecondaryDbConfig mysqlConfig;
public SecondaryDataSourceConfigure(SecondaryDbConfig verticaConfig) {
this.mysqlConfig = verticaConfig;
}
@Bean(name = "secondaryDataSource")
@ConfigurationProperties("spring.datasource.secondary")
public DataSource secondaryDataSource() {
DataSource dataSource = DataSourceBuilder.create().build();
HikariDataSource hikariDataSource = null;
if (dataSource instanceof HikariDataSource) {
// 连接池配置
hikariDataSource = (HikariDataSource) dataSource;
hikariDataSource.setPoolName(mysqlConfig.getPoolName());
hikariDataSource.setAutoCommit(mysqlConfig.isAutoCommit());
hikariDataSource.setConnectionTestQuery(mysqlConfig.getConnectionTestQuery());
hikariDataSource.setIdleTimeout(mysqlConfig.getIdleTimeout());
hikariDataSource.setConnectionTimeout(mysqlConfig.getConnectionTimeout());
hikariDataSource.setMaximumPoolSize(mysqlConfig.getMaximumPoolSize());
hikariDataSource.setMaxLifetime(mysqlConfig.getMaxLifetime());
hikariDataSource.setMinimumIdle(mysqlConfig.getMinimumIdle());
hikariDataSource.setJdbcUrl( mysqlConfig.getUrl());
hikariDataSource.setUsername(mysqlConfig.getUsername() );
hikariDataSource.setPassword(mysqlConfig.getPassword());
// hikariDataSource.setUsername();
}
return hikariDataSource == null ? dataSource : hikariDataSource;
}
@Bean(name = "secondarySqlSessionFactory")
public SqlSessionFactory secondarySessionFactory(@Qualifier("secondaryDataSource") DataSource dataSource) throws Exception {
// MyBatis-Plus使用MybatisSqlSessionFactoryBean MyBatis直接使用SqlSessionFactoryBean
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
// 给MyBatis-Plus注入数据源
bean.setDataSource(dataSource);
bean.setPlugins(paginationInterceptor()); //添加分页插件
Resource[] resources = new PathMatchingResourcePatternResolver().
getResources("classpath:com/xml路径/mapper/mapping/*.xml");
bean.setMapperLocations(resources);
return bean.getObject();
}
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
@Bean(name = "secondaryTransactionManager")
public DataSourceTransactionManager secondaryTransactionManager(@Qualifier("secondaryDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "secondarySessionTemplate")
public SqlSessionTemplate secondarySessionTemplate(@Qualifier("secondarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}