Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to integrate Druid and connect MySQL8.0.11 with SpringBoot

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "SpringBoot how to integrate Druid connection MySQL8.0.11", so the editor summarizes the following contents, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "SpringBoot how to integrate Druid connection MySQL8.0.11" article.

1. Configuration dependency

Dependency management we can use maven or gradle

MySQL Connector/J version selection:

Note: if it is the MySQL5.X series, there will be compatibility problems with 8.0.x drivers, please pay attention!

1.1MySQL Connector/J depends on configuration maven mysql mysql-connector-java 8.0.11

Gradle

/ / https://mvnrepository.com/artifact/mysql/mysql-connector-javacompile group: "mysql", name: "mysql-connector-java", version: "8.0.11" 1.2Druid dependency configuration

Maven

Com.alibaba druid 1.1.10

# gradle

/ / https://mvnrepository.com/artifact/com.alibaba/druidcompile group: "com.alibaba", name: "druid", version: "1.1.10" 2. Configure DataSource

Here we use Java Config. When I use the YAML configuration, I find that the code hints are incomplete, which greatly increases the possibility of errors, so I recommend using Java Config.

2.1 create configuration class @ Configurationpublic class Config {@ Bean public DruidDataSource druidDataSource () {/ / Druid data source configuration 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"); / / initial number of connections (default 0) dataSource.setInitialSize (8); / / minimum number of connections (default 0) dataSource.setMinIdle (8); / / maximum number of connections (default value 8, note that the attribute "maxIdle" has been deprecated) dataSource.setMaxActive (32); return dataSource;}} 2.2 considerations

Some of the comments are written in the code, here are two points for attention

One is DriverClassName.

8.0.11 driver (should be the beginning of version 8), abandoned the original method, we can also 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 to load the class "com.mysql.jdbc.Driver". This has been abandoned. The new driver class is `com.mysql.cj.jdbc.Driver ", so pay attention to the setting of one property" setDriverClassName "and the other is the setting of URL. There are four parameters to pay attention to.

CharacterEncoding=utf8 (character Encoding)

UseSSL=false (it is found that it needs to be added at the beginning of version 8, but not in 5.x impression. Adding this parameter may be related to the SSL connection setting of MySQL)

ServerTimezone=UTC (add this parameter when there is a Time Zone error when connecting to the database, which seems to be a problem when I use Druid connection pooling)

AllowPublicKeyRetrieval=true (no problem logging in using a root account, but a PublicKeyRetrieval error will be prompted if you use a regular account)

For more configurations, please refer to wiki in the Druid project and configure them according to your needs.

The above is about the content of this article on "how SpringBoot integrates Druid to connect MySQL8.0.11". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report