要学习一个知识点,首要的还是查看官方文档:
https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/htmlsingle/#boot-features-sql
spring-boot选择HikariCP(号称最快最牛逼)作为默认连接池,以下介绍如何使用:
1. 在pom.xml中引入jar包
<!-- 使用默认的jdbc连接池HikariCP -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- spring-boot默认版本, 2.1.6.RELEASE对应8.0.16 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
2. 在application.properties中加入mysql配置:
# mysql
spring.datasource.url=jdbc:mysql://localhost:3306/springboot2
spring.datasource.username=kevin
spring.datasource.password=123
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
3. 启动应用
按照上述配置直接启动会出现以下错误:
Loading class
com.mysql.jdbc.Driver'. This is deprecated. The new driver class is
com.mysql.cj.jdbc.Driver’. The driver is
automatically registered via the SPI and manual loading of the driver
class is generally unnecessary.
意思是com.mysql.jdbc.Driver已经废弃,需要使用com.mysql.cj.jdbc.Driver(mysql-connector-java 6以上使用该驱动类),同时springboot会自动加入;所以我们只需要将spring.datasource.driver-class-name配置去掉或者设置为com.mysql.cj.jdbc.Driver,然后重新启动即可。
4. 测试jdbc是否连接(数据库手动增加一条记录用于测试)
package com.kevin.springbootstudy.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class JdbcController {
@Autowired
private JdbcTemplate jdbcTemplate;
@RequestMapping(value = "getUser")
public Object getUser(@RequestParam(value = "id")String id){
return jdbcTemplate.queryForObject("select name from user where id = ?", new String[]{id}, String.class);
}
}
测试http://localhost:8081//user?id=1报错:
java.sql.SQLException: The server time zone value ‘Öйú±ê׼ʱ¼ä’ is
unrecognized or represents more than one time zone. (也就是mysql连接需要配置时区)
1)修改数据库连接
spring.datasource.url=jdbc:mysql://localhost:3306/springboot2?characterEncoding=utf8&serverTimezone=UTC
访问http://localhost:8081/getUser?id=1返回kevin,测试成功
查看后台日志打印,说明HikariPool数据库连接池启动成功
2019-09-09 10:14:55.884 INFO 9432 --- [nio-8081-exec-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2019-09-09 10:14:56.243 INFO 9432 --- [nio-8081-exec-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2)校验时区是否正确
@RequestMapping("/dbtime")
public Object dbtime(){
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.format(jdbcTemplate.queryForObject("select now() ", Date.class));
}
访问http://127.0.0.1:8081/dbtime,发现返回的时间比实际时间多了8小时
3)修改数据库连接属性serverTimezone=Asia/Shanghai后恢复正常
使用serverTimezone=GMT%2B8也可以(即GMT+8)
5. 以上的数据库连接配置都是基本的参数,如果还要设置连接超时时间、连接池最大连接数怎么办?
参考Hikari的详细配置(https://github.com/brettwooldridge/HikariCP)
可以参考以下配置
# hikari
## default true 默认自动提交
spring.datasource.hikari.auto-commit=true
## lowest 250 ms. Default: 30000 (30 seconds) 连接超时时间,最小250毫秒,默认30秒
spring.datasource.hikari.connection-timeout=300
## This setting only applies when minimumIdle is defined to be less than maximumPoolSize. 空闲连接存活时间,默认10分钟,必须在minimumIdle设置比maximumPoolSize才生效
spring.datasource.hikari.idle-timeout=600000
## We strongly recommend setting this value, and it should be several seconds shorter than any database or infrastructure imposed connection time limit. 建议设置最大存活时间,默认30分钟
## Default: 1800000 (30 minutes)
spring.datasource.hikari.max-lifetime=1800000
## If your driver supports JDBC4 we strongly recommend not setting this property. Default: none 在jdbc下不建议设置测试sql查询,默认为空
#spring.datasource.hikari.connection-test-query= select 1
## we recommend not setting this value. Default: same as maximumPoolSize 不建议设置最小空闲数,默认与最大连接数一样
#spring.datasource.hikari.minimum-idle=
## Default: 10 最大连接数,默认为10
spring.datasource.hikari.maximum-pool-size=10
扩展:
mysql-connector-java遇到问题较多,所以研究下
https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-versions.html
1. 对mysql版本支持
2. 对java版本支持
3. maven引入规范
4. 在spring中使用
5.连接时必须加入serverTimezone参数
6. 常用的配置参数
参考https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-reference-configuration-properties.html
useUnicode(5.1中设置为true才能使用characterEncoding,8.0中已没有该参数)
characterEncoding
autoReconnect
failOverReadOnly 自动重连成功后,连接是否设置为只读
maxReconnects 重试连接的次数
serverTimezone
useSSL (For 8.0.13 and later: Default is ‘true’)
rewriteBatchedStatements 开启批处理,默认不开启
7. 驱动名称
8.0的驱动名称:
5.1的驱动名称