社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
// https://mvnrepository.com/artifact/mysql/mysql-connector-java
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.11'
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
####gradle
// https://mvnrepository.com/artifact/com.alibaba/druid
compile group: 'com.alibaba', name: 'druid', version: '1.1.10'
@Configuration
public class Config {
@Bean
public DruidDataSource druidDataSource() {
//Druid 数据源配置
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/work?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true");
dataSource.setUsername("webuser");
dataSource.setPassword("123456");
//初始连接数(默认值0)
dataSource.setInitialSize(8);
//最小连接数(默认值0)
dataSource.setMinIdle(8);
//最大连接数(默认值8,注意"maxIdle"这个属性已经弃用)
dataSource.setMaxActive(32);
return dataSource;
}
}
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.
characterEncoding=utf8 (字符编码)
useSSL=false (发现是8版本开始才需要添加,5.X印象中不需要,添加这个参数可能和MySQL的SSL连接设置有关系)
serverTimezone=UTC (当连接数据库时候,出现Time Zone错误时添加此参数,我貌似是使用Druid连接池时才出现的这个问题)
allowPublicKeyRetrieval=true (使用root账户登陆没问题,使用普通账户会提示Public Key Retrieval错误)
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!