springboot使用jpa連接mysql一些列問題:
問題一:java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)
導致原因:根據智能提示選擇了下面的配置書寫格式
spring.datasource.data-username=root
spring.datasource.data-password=123456
處理:正確的書寫應該爲
spring.datasource.username=root
spring.datasource.password=123456
問題二:You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
大意:您需要通過設置useSSL=false顯式禁用SSL,或者設置useSSL=true併爲服務器證書驗證提供信任存儲。
處理:在數據庫連接追加 &verifyServerCertificate\=false&useSSL\=false
spring.datasource.url=jdbc\:mysql\://localhost\:3306/db01?useUnicode\=true&characterEncoding\=UTF-8&autoReconnect\=true&autoReconnectForPools\=true&zeroDateTimeBehavior\=convertToNull&verifyServerCertificate\=false&useSSL\=false
問題三:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'db01.hibernate_sequence' doesn't exist
參考:https://www.cnblogs.com/cdfive2018/p/9801362.html
背景:springboot 1.5.9.RELEASE 升級至 2.0.5.RELEASE時,spring-boot-starter-data-jpa使用了hibernate5
解決方法:spring.jpa.hibernate.use-new-id-generator-mappings=false 或 @GeneratedValue(strategy =GenerationType.IDENTITY)
驗證:spring.jpa.hibernate.use-new-id-generator-mappings=false
問題消失====>出現新的問題:java.sql.SQLException: Field 'id' doesn't have a default value
原因是數據庫主鍵沒有設置自增:勾上自增繼續驗證
驗證成功:
驗證:@GeneratedValue(strategy =GenerationType.IDENTITY)
去掉:spring.jpa.hibernate.use-new-id-generator-mappings=false
此時:
問題復現:
修改爲:
@GeneratedValue(strategy =GenerationType.IDENTITY)
驗證:驗證成功
說明兩種方案皆可以。