赞
踩
- <!-- MyBatis+Mysql 5添加依赖-->
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>1.1.1</version>
- </dependency>
-
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.41</version>
- </dependency>
com.mysql.cj.jdbc.Driver
是MySQL Connector/J 8.x及以上版本中的JDBC驱动类名。
在早期的版本中,如MySQL Connector/J 5.x,驱动类名通常是com.mysql.jdbc.Driver
MySQL Connector/J 5.x
- spring.datasource.url=jdbc:mysql://localhost:3306/your_database?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
- spring.datasource.username=root
- spring.datasource.password=your_password
- spring.datasource.driver-class-name=com.mysql.jdbc.Driver
MySQL Connector/J 8.x
- spring.datasource.url=jdbc:mysql://localhost:3306/your_database?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
- spring.datasource.username=root
- spring.datasource.password=your_password
- spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url
属性指定了数据库的URL,包括主机名、端口号、数据库名以及一些可选的连接参数。spring.datasource.username
和spring.datasource.password
分别指定了连接数据库所需的用户名和密码。最后,spring.datasource.driver-class-name
属性指定了JDBC驱动类名。
serverTimezone=UTC
参数用于指定服务器的时区,这有助于解决因时区差异而导致的日期时间问题。
在MySQL JDBC连接字符串中,useSSL=false
和 allowPublicKeyRetrieval=true
是两个重要的连接参数,尤其是在使用较新版本的MySQL数据库(如MySQL 8.0及以上)和MySQL Connector/J JDBC驱动时。这两个参数的作用和设置原因如下:
useSSL=false
false
意味着不使用SSL加密来建立连接。require_secure_transport=ON
),则即使将useSSL
设置为false
,连接也可能无法成功建立。在这种情况下,应该配置数据库服务器以允许非SSL连接,或者在客户端使用SSL连接。allowPublicKeyRetrieval=true
true
时,它允许客户端在连接时从服务器检索公钥,并使用这个公钥来验证服务器的身份。allowPublicKeyRetrieval
被设置为false
(这是MySQL 8.0及更高版本JDBC驱动的默认值),并且客户端没有预配置的公钥来验证服务器,那么连接将失败。allowPublicKeyRetrieval
设置为true
可以允许连接成功,但在生产环境中,更好的做法是使用预配置的公钥或证书链来验证服务器的身份,而不是依赖动态检索的公钥。这可以通过设置trustCertificateKeyStoreUrl
、trustCertificateKeyStorePassword
等参数来实现,以指向包含受信任证书的密钥库。Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。