Cannot load JDBC driver class '${jdbc.mysql.driverClassName}',继承mybatis,读取属性配置文件失败。

 在集成mybatis的时候,数据库的用户名、密码等相关信息是配置在属性文件中,当调用的时候获取属性文件的值失败,具体错误信息如下:

### Error querying database.  Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class '${jdbc.mysql.driverClassName}'
### The error may exist in class path resource [com/weshare/security/domain/mybatis/sysUserMapper.xml]
### The error may involve com.weshare.security.dao.mybatis.SysUserDao.selectSysUsers
### The error occurred while executing a query
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class '${jdbc.mysql.driverClassName}'] with root cause
java.lang.ClassNotFoundException: ${jdbc.mysql.driverClassName}
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1945)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1788)
    at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1420)
    at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)

 错误的配置方式:


    
    
    
    

Cannot load JDBC driver class '${jdbc.mysql.driverClassName}',继承mybatis,读取属性配置文件失败。_第1张图片

 

正确的配置方式:


    
    
    

 原因:

原因分析:是因为配置了class="org.mybatis.spring.mapper.MapperScannerConfigurer",而这句代码会在数据源加载前就执行了,之后就把表达式${jdbc.driverClassName}当成字符串执行了。

参考: https://blog.csdn.net/xuan_lu/article/details/80280276

你可能感兴趣的:(Spring)