MyBatis 抛出 java.net.MalformedURLException: no protocol: com.hxstrive.**.UserMapper 错误

解决 org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.net.MalformedURLException: no protocol: com.hxstrive.mybatis.simple.UserMapper 错误信息

在使用 MyBatis 时,抛出“java.net.MalformedURLException: no protocol: com.hxstrive.mybatis.simple.UserMapper”错误。MyBatis 配置如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <properties resource="database.properties"/>
    <environments default="MySqlDatabase" >
        <environment id="MySqlDatabase" >
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper url="com.hxstrive.mybatis.simple.UserMapper" />
    </mappers>
</configuration>

错误详细信息如下:

Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: 

### Error building SqlSession.

### The error may exist in com.hxstrive.mybatis.simple.UserMapper

### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.net.MalformedURLException: no protocol: com.hxstrive.mybatis.simple.UserMapper

    at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23)

    at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:79)

    at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:63)

    at com.hxstrive.mybatis.simple.SimpleDemo.<init>(SimpleDemo.java:23)

    at com.hxstrive.mybatis.simple.SimpleDemo.main(SimpleDemo.java:15)

Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.net.MalformedURLException: no protocol: com.hxstrive.mybatis.simple.UserMapper

    at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:106)

    at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:89)

    at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:77)

    ... 3 more

Caused by: java.net.MalformedURLException: no protocol: com.hxstrive.mybatis.simple.UserMapper

    at java.net.URL.<init>(URL.java:586)

    at java.net.URL.<init>(URL.java:483)

    at java.net.URL.<init>(URL.java:432)

    at org.apache.ibatis.io.Resources.getUrlAsStream(Resources.java:209)

    at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:322)

    at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:104)

    ... 5 more

Process finished with exit code 1

上面错误信息是因为配置 Mapper 资源时出现了错误,错误配置如下:

<mapper url="com.hxstrive.mybatis.simple.UserMapper" />

需要将 url 改为 resource,修改后的配置文件如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <properties resource="database.properties"/>
    <environments default="MySqlDatabase" >
        <environment id="MySqlDatabase" >
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/hxstrive/mybatis/simple/UserMapper.xml" />
    </mappers>
</configuration>

点击学习 MyBatis 教程,了解更多的 MyBatis 知识!

学习必须与实干相结合。 —— 泰戈尔
0 不喜欢
说说我的看法 -
全部评论(
没有评论
关于
本网站专注于 Java、数据库(MySQL、Oracle)、Linux、软件架构及大数据等多领域技术知识分享。涵盖丰富的原创与精选技术文章,助力技术传播与交流。无论是技术新手渴望入门,还是资深开发者寻求进阶,这里都能为您提供深度见解与实用经验,让复杂编码变得轻松易懂,携手共赴技术提升新高度。如有侵权,请来信告知:hxstrive@outlook.com
公众号