hikari数据源配置类_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=1spring.datasource.primary.maximum-pool-size=15spring.datasource.primary.auto-commit=truespring.datasource.primary.idle-timeout=60000spring.datasource.primary.pool-name=fang_sqlserver

spring.datasource.primary.max-lifetime=1800000spring.datasource.primary.connection-timeout=30000spring.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=123456spring.datasource.secondary.driver-class-name=com.mysql.cj.jdbc.Driver

spring.datasource.secondary.type=com.zaxxer.hikari.HikariDataSource

spring.datasource.secondary.minimum-idle=1spring.datasource.secondary.maximum-pool-size=15spring.datasource.secondary.auto-commit=truespring.datasource.secondary.idle-timeout=60000spring.datasource.secondary.pool-name=fang_mysql

spring.datasource.secondary.max-lifetime=1800000spring.datasource.secondary.connection-timeout=30000spring.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不会重复创建


版权声明:本文为weixin_33819646原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。