欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 运维知识 > 数据库 >内容正文

数据库

Spring连接mysql数据库错误:Cannot load JDBC driver class '${driver}'

发布时间:2023/12/4 数据库 35 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Spring连接mysql数据库错误:Cannot load JDBC driver class '${driver}' 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

在用Spring使用连接mysql数据库时出现如下错误:

Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception isjava.sql.SQLException: Cannot load JDBC driver class '${driver}'


错误详细信息如下:

Exception in thread "main" org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Cannot load JDBC driver class '${driver}' ### The error may exist in file [/Users/guowei/EclipseProjects/workspace/FrameworkMaven/target/classes/com/guowei/maven/framework/mapping/UserMapper.xml] ### The error may involve com.guowei.maven.framework.dao.UserMapper.getUserById ### The error occurred while executing a query ### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Cannot load JDBC driver class '${driver}' at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:76) at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:399) at com.sun.proxy.$Proxy7.selectOne(Unknown Source) at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:165) at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:69) at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:53) at com.sun.proxy.$Proxy8.getUserById(Unknown Source) at com.guowei.maven.framework.servlet.Test.main(Test.java:18) Caused by: org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Cannot load JDBC driver class '${driver}' ### The error may exist in file [/Users/guowei/EclipseProjects/workspace/FrameworkMaven/target/classes/com/guowei/maven/framework/mapping/UserMapper.xml] ### The error may involve com.guowei.maven.framework.dao.UserMapper.getUserById ### The error occurred while executing a query ### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Cannot load JDBC driver class '${driver}' at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:122) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:113) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:73) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:386)
原因是因为配置了applicationContext配置文件配置了default-autowire="byName"属性,这样会导致提前初始化一些类,这个时候,PropertyPlaceholderConfigurer还没来得及替换定义中的变量,导致把表达式当作字符串复制了。


解决方案就是删掉这个配置属性即可。


参考资料:

http://songjianyong.iteye.com/blog/1663170

http://www.oschina.net/question/188964_32305



总结

以上是生活随笔为你收集整理的Spring连接mysql数据库错误:Cannot load JDBC driver class '${driver}'的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。