1. 程式人生 > 實用技巧 >spring和mybatis整合時Access denied for user '***'@'localhost' (using password: YES)錯誤的解決方案

spring和mybatis整合時Access denied for user '***'@'localhost' (using password: YES)錯誤的解決方案

參考文章:部落格園文章

參考解決辦法:

將資料庫配置檔案格式

key=value

改為

jdbc.key=value

以下為問題分析

使用Spring + Mybatis + Mysql整合時,測試報錯

SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1d296da] was not registered for synchronization because synchronization is not active

Access denied for user '***'@'localhost' (using password: YES)

整合時執行是沒有問題的,然後我修改為使用外部prop配置檔案後,通過Spring配置檔案匯入資料庫配置資訊,報的這個錯誤.再結合錯誤提示,猜測是由於使用者名稱或密碼錯誤導致,網上查詢發現是因為prop檔案key名稱寫錯
如下:
jdbc.properties檔案內容(錯誤的)

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=test

key前面加上jdbc.key即可解決
修改後

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=test

再次執行成功!