We can use maven or gradle for dependency management
MySQL Connector/J version selection:
Note: If it is the MySQL5.X series, there will be compatibility issues when using the 8.0.X driver, please pay attention!
<!-- 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"
maven
<!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency>
// 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; } }
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
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
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.
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)
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!