Cause: java.sql.SQLException: No suitable driver found for http://www.example.com(maven搭建mybatis出错)

问题

当我们使用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 !!!

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值