项目场景
练习使用MyBatis框架时,在resources文件夹中使用mybatis-config.xml配置文件,配置数据库驱动并使用db.properties 配置文件解决硬编码问题时出现.
问题描述
java.lang.ExceptionInInitializerError at cn.dyslzx.mybatis.impl.DAODYSImpl.selectAll(DAODYSImpl.java:42)
at cn.dyslzx.mybatis.test.DAODYSImplTest.testSelectALl(DAODYSImplTest.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 解决硬编码的问题 -->
<properties resource="db.properties"/>
<!-- 配置运行的环境 -->
<environments default="dev">
<environment id="dev">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driverClassName" value="${driverClassName}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!-- 与MyBatisMapper.xml相关联 -->
<mappers>
<mapper resource="cn/dyslzx/mybatis/mapper/MyBatisMapper.xml"/>
</mappers>
</configuration>
原因分析
因为<property name="driverClassName" value="${driverClassName}"/>
其中的name = driverClassName,是无法被识别的,不知道是约束问题,还是就是name的值是固定的
解决方案
只要将driverClassName改成driver就可以了!!!
<property name="driver" value="${driverClassName}"/>
如有错误请各位大佬指正,谢谢