SSM整合过程中遇到的BUG 数据库连接的问题
# SSM整合过程中遇到的BUG 数据库连接的问题
service调用mybatis时报异常
```
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error updating database. Cause: java.sql.SQLException: Unable to load authentication plugin 'caching_sha2_password'.
### Cause: java.sql.SQLException: Unable to load authentication plugin 'caching_sha2_password'.
```
我使用的mysql-connector-java版本是8.0.15,在junit测试代码下能够正常连接数据库,网上该异常的大多原因是mysql数据库的加密方案不一致,需要更新版本,但我版本是8.0.x,因此问题不在这。我在进行junit测试代码时,发现了如下警告:
```
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
```
也就是说SPI自动将数据库驱动进行注入,于是我尝试消除该警告,在自己的jdbc.properties文件下更改jdbc.driver的值为
```
com.mysql.cj.jdbc.Driver
```
然后重新启动服务器,不在报异常,问题解决! 该问题可能是由于Springmvc框架没有调用SPI对新版的driver进行自动装载而引起,而在junit单元测下出现的警告是SPI将新版的进行了自动装载,所有junit测试数据库连接没问题,浏览器访问数据库则会出现该异常。
service调用mybatis时报异常
```
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error updating database. Cause: java.sql.SQLException: Unable to load authentication plugin 'caching_sha2_password'.
### Cause: java.sql.SQLException: Unable to load authentication plugin 'caching_sha2_password'.
```
我使用的mysql-connector-java版本是8.0.15,在junit测试代码下能够正常连接数据库,网上该异常的大多原因是mysql数据库的加密方案不一致,需要更新版本,但我版本是8.0.x,因此问题不在这。我在进行junit测试代码时,发现了如下警告:
```
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
```
也就是说SPI自动将数据库驱动进行注入,于是我尝试消除该警告,在自己的jdbc.properties文件下更改jdbc.driver的值为
```
com.mysql.cj.jdbc.Driver
```
然后重新启动服务器,不在报异常,问题解决! 该问题可能是由于Springmvc框架没有调用SPI对新版的driver进行自动装载而引起,而在junit单元测下出现的警告是SPI将新版的进行了自动装载,所有junit测试数据库连接没问题,浏览器访问数据库则会出现该异常。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)