配置Mybatis-config報錯:Cannot load connection class because of underlying exception: com.mysql.cj.exceptions.WrongArgumentException: failed to parse the connection string near ';useUnicode=true&


參考:

配置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&amp;characterEncoding=UTF-8&amp;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&amp;characterEncoding=UTF-8&amp;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文件中&&應該被寫成&amp;
而在.properties中應該是&&,所以我把.xml文件的url中的&amp;改為&&后成功運行。

修改為:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSL=true&&useUnicode=true&&characterEncoding=UTF-8&&serverTimezone=GMT
username=root
password=123456

得以解決。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM