New->Project or Module->Spring Initializer
选择JDBC和mysql驱动,为了方便测试web等等也可以选上
项目创建成功之后,会自动配上如下的配置:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>新建一个application.yml配置文件:
spring: datasource: username: root password: root url: jdbc:mysql://127.0.0.1:3306/springboot driver-class-name: com.mysql.jdbc.Driver新建一个junit测试类进行测试:
package com.example.springboot.jdbc; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; @SpringBootTest class SppringbootJdbcApplicationTests { @Autowired DataSource dataSource; @Test public void contextLoads() throws SQLException { System.out.println(dataSource.getClass()); Connection connection = dataSource.getConnection(); System.out.println(connection); connection.close(); } }启动调试,发现控制台抛出异常信息:
Caused by: com.mysql.cj.exceptions.UnableToConnectException: CLIENT_PLUGIN_AUTH is required
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:85)
at com.mysql.cj.protocol.a.NativeAuthenticationProvider.connect(NativeAuthenticationProvider.java:205)
at com.mysql.cj.protocol.a.NativeProtocol.connect(NativeProtocol.java:1340)
at com.mysql.cj.NativeSession.connect(NativeSession.java:157)
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:956)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826)
... 90 more
原因是我的mysql服务器是5.+版本,而最新版2.2.1默认配置的mysql驱动版本是8.+的,8.+的驱动类是com.mysql.cj.jdbc.Driver,而非com.mysql.jdbc.Driver,所以解决方法还是手动加一下mysql驱动版本
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.27</version> <scope>runtime</scope> </dependency>ok,现在正常测试,测试通过:
class com.zaxxer.hikari.HikariDataSource
2019-12-14 15:47:25.241 INFO 6772 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2019-12-14 15:47:27.403 INFO 6772 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
HikariProxyConnection@23906266 wrapping com.mysql.jdbc.JDBC4Connection@717ed
可以看出2.2.1版本的Springboot默认数据源类是class com.zaxxer.hikari.HikariDataSource,而1.+版本很多都是以org.apache.tomcat.jdbc.pool.DataSource.class为默认数据源类的,具体哪个版本开始改变的不清楚
既然Springboot默认使用hikari数据源,那就可以加上hikari的一些自定义配置
spring: datasource: url: jdbc:mysql://127.0.0.1:3306/springboot?characterEncoding=utf8&useSSL=true username: root password: root driver-class-name: com.mysql.jdbc.Driver initialization-mode: always type: com.zaxxer.hikari.HikariDataSource hikari: jdbc-url: jdbc:mysql://127.0.0.1:3306/springboot?characterEncoding=utf8&useSSL=true minimum-idle: 5 maximum-pool-size: 15 auto-commit: true idle-timeout: 30000 pool-name: DatebookHikariCP max-lifetime: 1800000 connection-timeout: 30000 connection-test-query: select 1经过调试,属性配置是有起效的
ok,掌握简单应用之后,还是要跟一下源码,依照经验,找到对应的自动配置类DataSourceAutoConfiguration.java