参考:
配置MyBatis错误Cannot load connection class because of underlying exception: com.mysql.cj.exceptions..
情况:
想将resource里面的db.properties数据传到Mybatis-config,运行UserDaoTest出错
db.properties:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT
username=root
password=123456
Mybatis-config.xml
<?xml version="1.0" encoding="utf8" ?>
<!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="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/kuang/dao/UserMapper.xml"/>
</mappers>
</configuration>
出错表现为:
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.sql.SQLNonTransientConnectionException: Cannot load connection class because of underlying exception: com.mysql.cj.exceptions.WrongArgumentException: Malformed database URL, failed to parse the connection string near ';useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT'.
### The error may exist in com/kuang/dao/UserMapper.xml
### The error may involve com.kuang.dao.UserMapper.getUserLike
### The error occurred while executing a query
### Cause: java.sql.SQLNonTransientConnectionException: Cannot load connection class because of underlying exception: com.mysql.cj.exceptions.WrongArgumentException: Malformed database URL, failed to parse the connection string near ';useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT'.
根据出错信息,定位到db.properties文件,百度到下面的链接:
配置MyBatis错误Cannot load connection class because of underlying exception: com.mysql.cj.exceptions..
将mybatis中xml文件的properties标签内容转到另外一个外部配置文件jdbcCondig.properties中报错
我意识到应该是.properties文件和.xml数据库连接的url部分中的&&的解读方式不一样,在.xml文件中&&应该被写成&
而在.properties中应该是&&,所以我把.xml文件的url中的&改为&&后成功运行。
修改为:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSL=true&&useUnicode=true&&characterEncoding=UTF-8&&serverTimezone=GMT
username=root
password=123456
得以解决。