yml配置文件
sharding.jdbc: datasource: names: master,slave0 master: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.cj.jdbc.Driver jdbc-url: jdbc:mysql://***?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&useSSL=false&zeroDateTimeBehavior=convertToNull username: password: connection-timeout: 30000 idle-timeout: 600000 max-lifetime: 1800000 connection-test-query: SELECT 1 maximum-pool-size: 20 minimum-idle: 5 pool-name: APIHikariCP auto-commit: true slave0: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.cj.jdbc.Driver jdbc-url: jdbc:mysql://***?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&useSSL=false&zeroDateTimeBehavior=convertToNull username: password: connection-timeout: 30000 idle-timeout: 600000 max-lifetime: 1800000 connection-test-query: SELECT 1 maximum-pool-size: 20 minimum-idle: 5 pool-name: APIHikariCP auto-commit: true config: masterslave: load-balance_algorithm-type: round_robin name: ms master-data-source-name: master slave-data-source-names: slave0 props.sql.show: true
代码实现
HintManager hintManager = HintManager.getInstance(); hintManager.setMasterRouteOnly(); QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("phone", phone); User user = this.baseMapper.selectOne(queryWrapper); hintManager.close();
SpringBoot 使用Hikaricp连接池
1.添加pom.xml依赖
如果是SpringBoot2.0,那么默认的连接池就是Hikaricp,不需要配置
其他的,如果继承
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> </parent>
直接添加Hikaricp包就可以
<dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>3.1.0</version> </dependency>
反之使用jdbc包的要去除掉tomcat的连接配置
<!-- JDBC连接数据库,因为要用HikariCP,所以需要将SpringBoot中的tomcat-jdbc排除 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> <exclusions> <exclusion> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jdbc</artifactId> </exclusion> </exclusions> </dependency> <!-- HikariCP 连接池依赖,从父依赖获取额版本 --> <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <!-- <scope>runtime</scope> --> </dependency>
2.添加配置:
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test1?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull spring.datasource.username=root spring.datasource.password=hongda$123456 spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.max-idle=10 spring.datasource.max-active=15 spring.datasource.max-lifetime=86430000 spring.datasource.log-abandoned=true spring.datasource.remove-abandoned=true spring.datasource.remove-abandoned-timeout=60 spring.datasource.initialize=false spring.datasource.sqlScriptEncoding=UTF-8
DataSourceConfig:
import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; /** * HikariCP连接池配置 */ @Configuration public class DataSourceConfig { @Value("${spring.datasource.url}") private String dataSourceUrl; @Value("${spring.datasource.username}") private String user; @Value("${spring.datasource.password}") private String password; @Bean public DataSource dataSource() { HikariConfig config = new HikariConfig(); config.setJdbcUrl(dataSourceUrl); //数据源 config.setUsername(user); //用户名 config.setPassword(password); //密码 config.addDataSourceProperty("cachePrepStmts", "true"); //是否自定义配置,为true时下面两个参数才生效 config.addDataSourceProperty("prepStmtCacheSize", "250"); //连接池大小默认25,官方推荐250-500 config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); //单条语句最大长度默认256,官方推荐2048 config.addDataSourceProperty("useServerPrepStmts", "true"); //新版本MySQL支持服务器端准备,开启能够得到显著性能提升 config.addDataSourceProperty("useLocalSessionState", "true"); config.addDataSourceProperty("useLocalTransactionState", "true"); config.addDataSourceProperty("rewriteBatchedStatements", "true"); config.addDataSourceProperty("cacheResultSetMetadata", "true"); config.addDataSourceProperty("cacheServerConfiguration", "true"); config.addDataSourceProperty("elideSetAutoCommits", "true"); config.addDataSourceProperty("maintainTimeStats", "false"); HikariDataSource ds = new HikariDataSource(config); return ds; } }
参考:
https://www.cnblogs.com/hongdada/p/9360155.html
https://gitee.com/mirrors/hikaricp
http://fanlychie.github.io/post/spring-boot-tomcat-pool-hikaricp-dbcp-dbcp2.html
https://www.jianshu.com/p/f728e8c131a9
https://blog.csdn.net/chen15369337607/article/details/78142751
http://blog.didispace.com/Springboot-2-0-HikariCP-default-reason/
https://my.oschina.net/u/3452433/blog/1607850