Home >Database >Mysql Tutorial >How SpringBoot integrates Druid to connect to MySQL8.0.11

How SpringBoot integrates Druid to connect to MySQL8.0.11

王林
王林forward
2023-05-29 10:49:061472browse

1. Configure dependencies

We can use maven or gradle for dependency management
MySQL Connector/J version selection:

How SpringBoot integrates Druid to connect to MySQL8.0.11

Note: If it is the MySQL5.X series, there will be compatibility issues when using the 8.0.X driver, please pay attention!

1.1MySQL Connector/J dependency configuration maven

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.11</version>
</dependency>

gradle

// https://mvnrepository.com/artifact/mysql/mysql-connector-java
compile group: "mysql", name: "mysql-connector-java", version: "8.0.11"

1.2Druid dependency configuration

maven

<!-- 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"

2. Configure DataSource

Here we use Java Config. When I used YAML configuration, I found that the code prompt was incomplete, which greatly increased the possibility of errors, so it is recommended to use Java Config.

2.1 Create configuration class

@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;
    }
}

2.2 Notes


Some comments are written in the code, here are two notes
One is DriverClassName
8.0 The .11 driver (probably starting from version 8) has abandoned the original method. We can find it by looking directly at the source code. There are two sentences in

com.mysql.jdbc.Driver


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.

means loading class "com.mysql.jdbc .Driver”. This has been deprecated. The new driver class is `com.mysql.cj.jdbc.Driver" so pay attention to the setting of a property "setDriverClassName". Another note is the setting of the URL. There are 4 parameters that need to be paid attention to
  • characterEncoding=utf8 (Character encoding)
  • useSSL=false (It is found that it only needs to be added starting from version 8. It is not required in 5.X. Adding this parameter may be related to MySQL. It has to do with the SSL connection settings)
  • serverTimezone=UTC (When connecting to the database, add this parameter when a Time Zone error occurs. It seems that this problem only occurs when I use the Druid connection pool. )

  • allowPublicKeyRetrieval=true (It’s okay to log in with the root account, but a Public Key Retrieval error will be prompted when using a normal account)

More For multiple configurations, please refer to the wiki in the Druid project and configure according to your needs

How SpringBoot integrates Druid to connect to MySQL8.0.11

###

The above is the detailed content of How SpringBoot integrates Druid to connect to MySQL8.0.11. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete