Cause: java.sql.SQLException: No suitable driver found for http://www.example.com

问题

当我们使用Maven搭建Mybatis时,Maven在程序运行进行编译时,如果mybatis使用外部配置文件,会发生url地址替换错误的现象。错误信息如下。

org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.sql.SQLException: No suitable driver found for http://www.example.com
### The error may exist in cn/guxiang/dao/TeacherDao.xml
### The error may involve cn.guxiang.dao.TeacherDao.findAll
### The error occurred while executing a query
### Cause: java.sql.SQLException: No suitable driver found for http://www.example.com

外部配置文件如下:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/aini?useSSL=true&;useUnicode=true&;characterEncoding=UTF-8
username=root
password=1234

查询maven编译程序后的文件,发现将我外部配置文件的参数替换成http://www.example.com

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--configuration核心配置文件-->
<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="http://www.example.com"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="cn/guxiang/dao/TeacherDao.xml"/>
    </mappers>
</configuration>

解决方案

将外部配置文件的url改为jdbc.url。

driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/aini?useSSL=true&;useUnicode=true&;characterEncoding=UTF-8
username=root
password=1234

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--configuration核心配置文件-->
<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="${jdbc.url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="cn/guxiang/dao/TeacherDao.xml"/>
    </mappers>
</configuration>

改过之后可以正常运行

总结

在写外部配置文件的时候,driver、username、password的命名可以随意写。但是url必须写jdbc.url !!!

Cause: java.sql.SQLException: No suitable driver found for http://www.example.com

上一篇:淘宝店铺商品列表接口


下一篇:怎么获取淘宝商品详情