zoukankan      html  css  js  c++  java
  • springboot 2 多数据源 hikari 连接池

    1.配置文件

    #第一数据源
    spring.datasource.primary.jdbc-url=jdbc:sqlserver://192.168.1.159\aaa:1433;database=dataserver
    spring.datasource.primary.username=sa
    spring.datasource.primary.password=fr123456
    spring.datasource.primary.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
    spring.datasource.primary.type=com.zaxxer.hikari.HikariDataSource
    spring.datasource.primary.minimum-idle=1
    spring.datasource.primary.maximum-pool-size=15
    spring.datasource.primary.auto-commit=true
    spring.datasource.primary.idle-timeout=60000
    spring.datasource.primary.pool-name=fang_sqlserver
    spring.datasource.primary.max-lifetime=1800000
    spring.datasource.primary.connection-timeout=30000
    spring.datasource.primary.connection-test-query=SELECT 1
    
    #第二数据源
    spring.datasource.secondary.jdbc-url=jdbc:mysql://192.168.1.130:33306/reptie?zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2B8&useSSL=false&useUnicode=true&autoReconnect=true
    spring.datasource.secondary.username=root
    spring.datasource.secondary.password=123456
    spring.datasource.secondary.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.secondary.type=com.zaxxer.hikari.HikariDataSource
    spring.datasource.secondary.minimum-idle=1
    spring.datasource.secondary.maximum-pool-size=15
    spring.datasource.secondary.auto-commit=true
    spring.datasource.secondary.idle-timeout=60000
    spring.datasource.secondary.pool-name=fang_mysql
    spring.datasource.secondary.max-lifetime=1800000
    spring.datasource.secondary.connection-timeout=30000
    spring.datasource.secondary.connection-test-query=SELECT 1

    2.java代码,配置DataSource

     
    
    import com.zaxxer.hikari.HikariDataSource;
    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 javax.sql.DataSource;
    
    /**
     * @author PangG
     * @MapperScan 扫描mapper所在路径
     */
    @Configuration
    @MapperScan(basePackages = "aa.com.cn.mapper.sqlserver", sqlSessionFactoryRef = "sqlserverSqlSessionFactory")
    public class SqlserverDataSourceConfig {
    
        /**
         * @Bean 注册Bean对象
         * @Primary 表示默认数据源
         * @ConfigurationProperties 读取properties中的配置参数映射成为一个对象
         */
        @Bean(name = "sqlserverDataSource")
        @Primary
        @ConfigurationProperties(prefix = "spring.datasource.primary")
        public HikariDataSource getSqlserverDateSource() {
            return new HikariDataSource();
        }
    
        /**
         * @param datasource 数据源
         * @return SqlSessionFactory
         * @Primary 默认SqlSessionFactory
         */
        @Bean(name = "sqlserverSqlSessionFactory")
        @Primary
        public SqlSessionFactory sqlserverSqlSessionFactory(@Qualifier("sqlserverDataSource") DataSource datasource) throws Exception {
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            bean.setDataSource(datasource);
            //mybatis扫描xml所在位置
            bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/sqlserver/*.xml"));
            //分页插件
            Interceptor interceptor = new PageInterceptor();
            Properties properties = new Properties();
            //数据库
            properties.setProperty("helperDialect", "sqlserver");
    //        //是否将参数offset作为PageNum使用
    //        properties.setProperty("offsetAsPageNum", "true");
    //        //是否进行count查询
    //        properties.setProperty("rowBoundsWithCount", "true");
            //是否分页合理化
            properties.setProperty("reasonable", "true");
            interceptor.setProperties(properties);
            bean.setPlugins(new Interceptor[] {interceptor});
            return bean.getObject();
        }
    
        @Bean("sqlserverSessionTemplate")
        @Primary
        public SqlSessionTemplate sqlserverSqlSessionTemplate(@Qualifier("sqlserverSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
            return new SqlSessionTemplate(sqlSessionFactory);
        }
    }
    

      

     
    
    import com.zaxxer.hikari.HikariDataSource;
    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 javax.sql.DataSource;
    
    /**
     * @author PangG
     * @MapperScan 扫描mapper所在路径
     */
    @Configuration
    @MapperScan(basePackages = "aa.com.cn.mapper.mysql", sqlSessionFactoryRef = "mysqlSqlSessionFactory")
    public class MySqlDataSourceConfig {
    
        /**
         * @Bean 注册Bean对象
         * @Primary 表示默认数据源
         * @ConfigurationProperties 读取properties中的配置参数映射成为一个对象
         */
        @Bean(name = "mysqlDataSource")
        @ConfigurationProperties(prefix = "spring.datasource.secondary")
        public HikariDataSource getMysqlDateSource() {
            return new HikariDataSource();
        }
    
        /**
         * @param datasource 数据源
         * @return SqlSessionFactory
         * @Primary 默认SqlSessionFactory
         */
        @Bean(name = "mysqlSqlSessionFactory")
        public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("mysqlDataSource") DataSource datasource) throws Exception {
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            bean.setDataSource(datasource);
            //mybatis扫描xml所在位置
            bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/mysql/*.xml"));
            //分页插件
            Interceptor interceptor = new PageInterceptor();
            Properties properties = new Properties();
            //数据库
            properties.setProperty("helperDialect", "mysql");
    //        //是否将参数offset作为PageNum使用
    //        properties.setProperty("offsetAsPageNum", "true");
    //        //是否进行count查询
    //        properties.setProperty("rowBoundsWithCount", "true");
            //是否分页合理化
            properties.setProperty("reasonable", "true");
            interceptor.setProperties(properties);
            bean.setPlugins(new Interceptor[]{interceptor});
            return bean.getObject();
        }
    
        @Bean("mysqlSessionTemplate")
        public SqlSessionTemplate mysqlSqlSessionTemplate(@Qualifier("mysqlSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
            return new SqlSessionTemplate(sqlSessionFactory);
        }
    }
    

      

    如果有 Thread starvation or clock leap detecte 警告 把minimum-idle的值设置成1即可

    下面有评论,看不懂,感觉没必要?

    getMysqlDateSource(),这个方法的返回值应该改为DataSource,不能是HikariDataSource。 否则的话,@Bean初始化不会执行这个方法。 因为springboot自己已经创建了一个HikariDataSource对象放在ioc容器里,@Bean不会重复创建

    心稳了,手也就稳了。
  • 相关阅读:
    Java多线程系列 JUC锁03 公平锁(一)
    Java多线程系列 JUC锁02 互斥锁ReentrantLock
    JDBC课程3--通过ResultSet执行查询操作
    JDBC课程2--实现Statement(用于执行SQL语句)--使用自定义的JDBCTools的工具类静态方法,包括insert/update/delete三合一
    JDBC_通过DriverManager获得数据库连接
    JDBC课程1-实现Driver接口连接mysql数据库、通用的数据库连接方法(使用文件jdbc.properties)
    [终章]进阶20-流程控制结构--if/case/while结构
    MySQL进阶19--函数的创建(举例)/设置mysql的创建函数的权限/查看(show)/删除(drop) / 举4个栗子
    MySQL进阶18- 存储过程- 创建语句-参数模式(in/out/inout-对应三个例子) -调用语法-delimiter 结束标记'$'- 删除/查看/修改-三个练习
    SQL进阶17-变量的声明/使用(输出)--全局变量/会话变量--用户变量/局部变量
  • 原文地址:https://www.cnblogs.com/wangxiaofengde/p/13235732.html
Copyright © 2011-2022 走看看