Springboot 多数据源配置及与tkmybatis 的集成

需要导入的maven依赖:


        
            com.alibaba
            druid-spring-boot-starter
            1.1.6
        
        
        
            tk.mybatis
            mapper-spring-boot-starter
            2.0.0
        
        
        
            com.github.pagehelper
            pagehelper
        
        
        
            mysql
            mysql-connector-java
            5.1.44
        

        
        
            org.postgresql
            postgresql
            42.1.4
        

        
        
            com.ibm
            db2jcc
            1.0
        
        
            com.ibm
            db2jcc_license_cu
            1.0
        

1.在Springboot 配置文件中配置数据源信息:

   application-develop.properties

# mysql 数据库配置信息
spring.datasource.mysql.name=tdammdb
spring.datasource.mysql.url=jdbc:mysql://192.168.1.128:3306/tdammdb?useUnicode=true&characterEncoding=utf8
spring.datasource.mysql.username=root
spring.datasource.mysql.password=root
spring.datasource.mysql.driver-class-name=com.mysql.jdbc.Driver

#gp 数据库配置信息
spring.datasource.gp.name=tdamrdb
spring.datasource.gp.url=jdbc:postgresql://192.168.1.104:5432/tdamrdb
spring.datasource.gp.username=gpadmin
spring.datasource.gp.password=gpadmin
spring.datasource.gp.driver-class-name=org.postgresql.Driver

#db2 数据库配置信息
spring.datasource.tdcollect.name=tdcollect
spring.datasource.tdcollect.url=jdbc:db2://10.2.1.212:64000/tdamtdb
spring.datasource.tdcollect.username=db2todb
spring.datasource.tdcollect.password=tips2
spring.datasource.tdcollect.driver-class-name=com.ibm.db2.jcc.DB2Driver
spring.datasource.tdcollect.validationQuery=select 1 from sysibm.sysdummy1
spring.datasource.tdcollect.testWhileIdle=true

# Druid DataSource Info
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.filters=mergeStat,log4j
spring.datasource.maxActive=20
spring.datasource.initialSize=1
spring.datasource.maxWait=60000
spring.datasource.minIdle=1
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=select 'x'
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxOpenPreparedStatements=20
spring.datasource.removeAbandoned=true
spring.datasource.removeAbandonedTimeout=1800
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

2.使用配置类的方式读取配置文件的信息,配置数据源

package com.cfcc.tsms.base.config.druid;

import com.alibaba.druid.filter.Filter;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.alibaba.druid.wall.WallConfig;
import com.alibaba.druid.wall.WallFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;

/**
* Druid配置信息
* gezongyang
*/
@Configuration
@ConfigurationProperties(prefix = "spring.datasource")
public class DruidConfig extends DefaultDatabaseConfig {
    
    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Value("${spring.datasource.mysql.name}")
    private String mysql_name;
    @Value("${spring.datasource.mysql.url}")
    private String mysql_url;
    @Value("${spring.datasource.mysql.username}")
    private String mysql_username;
    @Value("${spring.datasource.mysql.password}")
    private String mysql_password;
    @Value("${spring.datasource.mysql.driver-class-name}")
    private String  mysqlDriverClassName;

    @Value("${spring.datasource.tdcollect.name}")
    private String tdcollect_name;
    @Value("${spring.datasource.tdcollect.url}")
    private String tdcollect_url;
    @Value("${spring.datasource.tdcollect.username}")
    private String tdcollect_username;
    @Value("${spring.datasource.tdcollect.password}")
    private String tdcollect_password;
    @Value("${spring.datasource.tdcollect.driver-class-name}")
    private String tdcollectDriverClassName;
    @Value("${spring.datasource.tdcollect.validationQuery}")
    private String tdcollect_validationQuery;
    @Value("${spring.datasource.tdcollect.testWhileIdle}")
    private String tdcollectTestWhileIdle;

    @Value("${spring.datasource.gp.name}")
    private String gp_name;
    @Value("${spring.datasource.gp.url}")
    private String gp_url;
    @Value("${spring.datasource.gp.username}")
    private String gp_username;
    @Value("${spring.datasource.gp.password}")
    private String gp_password;
    @Value("${spring.datasource.gp.driver-class-name}")
    private String  gpDriverClassName;


    @Bean(name = "gpDataSource")
    @Primary
    public DataSource gpDataSource() {

        DruidDataSource datasource = new DruidDataSource();
        datasource.setName(gp_name);
        datasource.setUrl(gp_url);
        datasource.setUsername(gp_username);
        datasource.setPassword(gp_password);
        datasource.setDriverClassName(gpDriverClassName);

        logger.info("gp 数据库连接池创建中.......");
        return datasource;
    }


    @Bean(name = "tdcollectDataSource")
    public DataSource tdcollectDataSource() {
        DruidDataSource datasource = new DruidDataSource();
        datasource.setName(tdcollect_name);
        datasource.setUrl(tdcollect_url);
        datasource.setUsername(tdcollect_username);
        datasource.setPassword(tdcollect_password);
        datasource.setDriverClassName(tdcollectDriverClassName);
        datasource.setValidationQuery(tdcollect_validationQuery);
        datasource.setTestWhileIdle(true);
        List filters = new ArrayList<>();
        filters.add(statFilter());
        filters.add(wallFilter());
        datasource.setProxyFilters(filters);

        logger.info("tdcollect 数据库连接池创建中.......");
        return datasource;
    }

    @Bean(name = "mysqlDataSource")
    public DataSource mysqlDataSource() {

        DruidDataSource datasource = new DruidDataSource();

        datasource.setName(mysql_name);
        datasource.setUrl(mysql_url);
        datasource.setUsername(mysql_username);
        datasource.setPassword(mysql_password);
        datasource.setDriverClassName(mysqlDriverClassName);
        completeDataSourceProperties(datasource);

        List filters = new ArrayList<>();
        filters.add(statFilter());
        filters.add(wallFilter());
        datasource.setProxyFilters(filters);
        return datasource;
    }

    @Bean
    public ServletRegistrationBean druidServlet() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");

        //控制台管理用户,加入下面2行 进入druid后台就需要登录
        //servletRegistrationBean.addInitParameter("loginUsername", "admin");
        //servletRegistrationBean.addInitParameter("loginPassword", "admin");
        return servletRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "/static/*,*.js,*.swf,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*");
        filterRegistrationBean.addInitParameter("profileEnable", "true");
        filterRegistrationBean.addInitParameter("sessionStatEnable", Boolean.TRUE.toString());
        return filterRegistrationBean;
    }

    /**
     * 统计SQL执行时间
     * @return
     */
    @Bean
    public StatFilter statFilter(){
        StatFilter statFilter = new StatFilter();
        statFilter.setLogSlowSql(true); //slowSqlMillis用来配置SQL慢的标准,执行时间超过slowSqlMillis的就是慢。
        statFilter.setMergeSql(true); //SQL合并配置
        statFilter.setSlowSqlMillis(1000);//slowSqlMillis的缺省值为3000,也就是3秒。
        return statFilter;
    }

    @Bean
    public WallFilter wallFilter(){
        WallFilter wallFilter = new WallFilter();


        //允许执行多条SQL
        WallConfig config = new WallConfig();
        config.setMultiStatementAllow(true);

        wallFilter.setConfig(config);
        //设置对存在风险的sql 打印日志
        wallFilter.setLogViolation(true);
        //true 对被认为是攻击的SQL 抛出 SQLException false 不抛出异常
        wallFilter.setThrowException(false);
        return wallFilter;
    }

    public DataSource completeDataSourceProperties(DruidDataSource dataSource) {
        dataSource.setInitialSize(getInitialSize());
        dataSource.setMinIdle(getMinIdle());
        dataSource.setMaxActive(getMaxActive());
        dataSource.setMaxWait(getMaxWait());
        dataSource.setTimeBetweenEvictionRunsMillis(getTimeBetweenEvictionRunsMillis());
        dataSource.setMinEvictableIdleTimeMillis(getMinEvictableIdleTimeMillis());
        dataSource.setValidationQuery(getValidationQuery());
        dataSource.setTestWhileIdle(getTestWhileIdle());
        dataSource.setTestOnBorrow(getTestOnBorrow());
        dataSource.setTestOnReturn(getTestOnReturn());
        dataSource.setPoolPreparedStatements(getPoolPreparedStatements());
        dataSource.setMaxOpenPreparedStatements(getMaxOpenPreparedStatements());
        dataSource.setRemoveAbandoned(getRemoveAbandoned());
        dataSource.setRemoveAbandonedTimeout(getRemoveAbandonedTimeout());
        dataSource.setConnectionProperties(getConnectionProperties());
        return dataSource;
    }

}

抽取出每个数据源配置的基础信息进行封装:

package com.cfcc.tsms.base.config.druid;

/**
* Druid数据源默认的参数
*
* @author gezongyang
* @version : 1.0
*/
public abstract class DefaultDatabaseConfig {
    private String name;
    private String url;
    private String username;
    private String password;

    private String type;
    private String driverClassName;
    private String filters;
    private int maxActive;
    private int initialSize;
    private int maxWait;
    private int minIdle;
    private long timeBetweenEvictionRunsMillis;
    private long minEvictableIdleTimeMillis;
    private String validationQuery;
    private boolean testWhileIdle;
    private boolean testOnBorrow;
    private boolean testOnReturn;
    private boolean poolPreparedStatements;
    private int maxOpenPreparedStatements;
    private boolean removeAbandoned;
    private int removeAbandonedTimeout;
    private String connectionProperties;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getDriverClassName() {
        return driverClassName;
    }

    public void setDriverClassName(String driverClassName) {
        this.driverClassName = driverClassName;
    }

    public String getFilters() {
        return filters;
    }

    public void setFilters(String filters) {
        this.filters = filters;
    }

    public int getMaxActive() {
        return maxActive;
    }

    public void setMaxActive(int maxActive) {
        this.maxActive = maxActive;
    }

    public int getInitialSize() {
        return initialSize;
    }

    public void setInitialSize(int initialSize) {
        this.initialSize = initialSize;
    }

    public int getMaxWait() {
        return maxWait;
    }

    public void setMaxWait(int maxWait) {
        this.maxWait = maxWait;
    }

    public int getMinIdle() {
        return minIdle;
    }

    public void setMinIdle(int minIdle) {
        this.minIdle = minIdle;
    }

    public long getTimeBetweenEvictionRunsMillis() {
        return timeBetweenEvictionRunsMillis;
    }

    public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) {
        this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
    }

    public long getMinEvictableIdleTimeMillis() {
        return minEvictableIdleTimeMillis;
    }

    public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) {
        this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
    }

    public String getValidationQuery() {
        return validationQuery;
    }

    public void setValidationQuery(String validationQuery) {
        this.validationQuery = validationQuery;
    }

    public boolean getTestWhileIdle() {
        return testWhileIdle;
    }

    public void setTestWhileIdle(boolean testWhileIdle) {
        this.testWhileIdle = testWhileIdle;
    }

    public boolean getTestOnBorrow() {
        return testOnBorrow;
    }

    public void setTestOnBorrow(boolean testOnBorrow) {
        this.testOnBorrow = testOnBorrow;
    }

    public boolean getTestOnReturn() {
        return testOnReturn;
    }

    public void setTestOnReturn(boolean testOnReturn) {
        this.testOnReturn = testOnReturn;
    }

    public boolean getPoolPreparedStatements() {
        return poolPreparedStatements;
    }

    public void setPoolPreparedStatements(boolean poolPreparedStatements) {
        this.poolPreparedStatements = poolPreparedStatements;
    }

    public int getMaxOpenPreparedStatements() {
        return maxOpenPreparedStatements;
    }

    public void setMaxOpenPreparedStatements(int maxOpenPreparedStatements) {
        this.maxOpenPreparedStatements = maxOpenPreparedStatements;
    }

    public boolean getRemoveAbandoned() {
        return removeAbandoned;
    }

    public void setRemoveAbandoned(boolean removeAbandoned) {
        this.removeAbandoned = removeAbandoned;
    }

    public int getRemoveAbandonedTimeout() {
        return removeAbandonedTimeout;
    }

    public void setRemoveAbandonedTimeout(int removeAbandonedTimeout) {
        this.removeAbandonedTimeout = removeAbandonedTimeout;
    }

    public String getConnectionProperties() {
        return connectionProperties;
    }

    public void setConnectionProperties(String connectionProperties) {
        this.connectionProperties = connectionProperties;
    }
}

3.集成tkmybatis 框架作为数据访问层

package com.cfcc.tsms.base.config.mybatis.gp;

import com.cfcc.tsms.auth.utils.BaseMapper;
import com.cfcc.tsms.base.config.interceptor.CameHumpInterceptor;
import com.github.pagehelper.PageHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.plugin.Interceptor;
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.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.util.Properties;

/**
* MyBatis基础配置
* gezongyang
*/

@Configuration
@MapperScan(basePackages ={"com.cfcc.tsms.stat.persistence.mapper"},
        sqlSessionFactoryRef = "sqlSessionFactory2",
        markerInterface = BaseMapper.class
)
public class MyBatisConfigOfGp {

    private static Log log = LogFactory.getLog(MyBatisConfigOfGp.class);

    @Autowired
    @Qualifier("gpDataSource")
    private DataSource dataSource;


    @Bean(name="sqlSessionFactory2")
    public SqlSessionFactory sqlSessionFactory2() throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);

        // 分页插件
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("reasonable", "true");
        properties.setProperty("supportMethodsArguments", "true");
        properties.setProperty("returnPageInfo", "check");
        properties.setProperty("params", "count=countSql");
        pageHelper.setProperties(properties);

        // 驼峰插件
        CameHumpInterceptor cameHumpInterceptor = new CameHumpInterceptor();
        Interceptor[] interceptorArray = new Interceptor[] {cameHumpInterceptor,pageHelper};
        bean.setPlugins(interceptorArray);
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

        // 添加扫描目录
        bean.setMapperLocations(resolver.getResources("classpath*:/mybatis/gp/tdamrdb/stat/mappers/*.xml"));

        return bean.getObject();
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate2() throws Exception {
        return  new SqlSessionTemplate(sqlSessionFactory2());
    }


    @Bean(name = "gpTransactionManager2")
    public DataSourceTransactionManager materialTransactionManager2() {
        return new DataSourceTransactionManager(dataSource);
    }
}
package com.cfcc.tsms.base.config.mybatis.mysql;
import com.cfcc.tsms.auth.utils.BaseMapper;
import com.cfcc.tsms.base.config.interceptor.CameHumpInterceptor;
import com.github.pagehelper.PageHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import tk.mybatis.spring.annotation.MapperScan;
;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.util.Properties;

/**
* MyBatis基础配置
*
* @author : gezongyang
*/
@Configuration
@MapperScan(basePackages = {"com.cfcc.tsms.auth.persistence.mapper",
                            "com.cfcc.tsms.param.persistence.mapper" },  //这里扫描dao层的接口
            sqlSessionFactoryRef = "sqlSessionFactory",
            markerInterface = BaseMapper.class   //指定tkmybatis 中的通用mapper
)
public class MyBatisConfigMysql {

    private static Log log = LogFactory.getLog(MyBatisConfigMysql.class);

    @Autowired
    @Qualifier("mysqlDataSource")
    private DataSource dataSource;


    @Primary
    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);

        // 分页插件
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("reasonable", "true");
        properties.setProperty("supportMethodsArguments", "true");
        properties.setProperty("returnPageInfo", "check");
        properties.setProperty("params", "count=countSql");
        pageHelper.setProperties(properties);


        // 驼峰插件
        CameHumpInterceptor cameHumpInterceptor = new CameHumpInterceptor();

        Interceptor[] interceptorArray = new Interceptor[] {cameHumpInterceptor, pageHelper};
        bean.setPlugins(interceptorArray);
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

        // 添加扫描目录
        bean.setMapperLocations(resolver.getResources("classpath*:/mybatis/mysql/tdamrdb/**/mappers/*.xml"));

        return bean.getObject();
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate() throws Exception {
        return  new SqlSessionTemplate(sqlSessionFactory());
    }

    @Bean(name = "mysqlTransactionManager")
    @Primary
    public DataSourceTransactionManager authTransactionManager() {
        return new DataSourceTransactionManager(dataSource);
    }
}

tkmybatis 通用mapper

package com.cfcc.tsms.auth.utils;

import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;

/**
* 所有Mapper的父接口
*
* @author : gezongyang
* @version : 1.0
*/

public interface BaseMapper extends Mapper, MySqlMapper {

}

 

你可能感兴趣的:(Springboot 多数据源配置及与tkmybatis 的集成)