Mybatis中Mapper资源无法绑定XML文件,及Maven资源过滤?

问题:BindingException异常,即MapperXML之间没有绑定,也就是说Mapper接口没有找XML实现类,很大原因就是在MybatisConfiguration中,没有配置绑定关系,这里我们提供两种存放位置的绑定关系

org.apache.ibatis.binding.BindingException: Type interface dao.OrderMapper is not known to the MapperRegistry.
	at org.apache.ibatis.binding.MapperRegistry.getMapper(MapperRegistry.java:47)
	at org.apache.ibatis.session.Configuration.getMapper(Configuration.java:845)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at 
资源文件存放在src/main/java

Configuration 配置绑定关系,主要是指定正确的XML路径

<?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>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/gwmdb?useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
                <property name="username" value="root"/>
                <property name="password" value="itsme999"/>
            </dataSource>
        </environment>
    </environments>
     <!--指定Mapper接口的实现类xml文件-->
    <mappers>
        <mapper resource="mapper/OrderMapper.xml"/>
    </mappers>
</configuration>

实体类

public class Order {

    private String user_id;
    private String order_content;
    private String order_id;
    private Date create_time;

    public String getUser_id() {
        return user_id;
    }

mapper接口的xml实现类

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="dao.OrderMapper">
    <select id="getOrderList" resultType="entity.Order">
        select * from ksd_order where order_id = 1
    </select>
</mapper>

mybatis configuration 配置文件

<?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>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/gwmdb?useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
                <property name="username" value="root"/>
                <property name="password" value="itsme999"/>
            </dataSource>
        </environment>
    </environments>
    <!--mapper和xml之间的绑定关系-->
    <mappers>
        <mapper resource="gmaper/OrderMapper.xml"/>
    </mappers>
</configuration>

测试类

public class DaoTest {

    @Test
    public void test1(){
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        List<Order> orderList = mapper.getOrderList();

        orderList.forEach(e->{System.out.println(e);});
    }
}

运行结果:
在这里插入图片描述

为什么会报错呢?这里我们先看下我们的maven项目结构如图所示
在这里插入图片描述
从这项目结构图中我们明明有gmaper文件夹,但是在编译后target文件夹却没有生成,这是为什么呢?

是因为我们把非java文件的配置文件放到src/main/java目录下面,maven有一项默认对src/main/java目录下非java文件的资源过滤配置默认是开启的,这里我们只需要关闭不要过滤,只需要加一段配置即可:

注意:这段配置中我自加了一项额外文件资源不被过滤,所有txt文件,这可以忽略

<build>
     <resources>
         <resource>
             <directory>src/main/java</directory>
             <includes>
                 <include>**/*.properties</include>
                 <include>**/*.xml</include>
                 <include>**/*.txt</include>
             </includes>
             <filtering>false</filtering>
         </resource>
         <resource>
             <directory>src/main/resources</directory>
             <includes>
                 <include>**/*.properties</include>
                 <include>**/*.xml</include>
             </includes>
             <filtering>false</filtering>
         </resource>
     </resources>
     <plugins>
         <plugin>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-compiler-plugin</artifactId>
             <configuration>
                 <source>8</source>
                 <target>8</target>
             </configuration>
         </plugin>
     </plugins>
 </build>

加上这段配置之后,我们配置文件就会在编译后的target目录中清晰的看见,target中的classes文件夹其实就是指的我们的classpath也是我们的根目录/

此时再看下我们的target目录先就有了gmaper文件夹,XML文件也有了
在这里插入图片描述
此时我们的测试类查询结果也正常显示结果:
在这里插入图片描述

资源文件存放在src/main/resource

其实绝大部分情况下我们的配置文件是放在src/main/resource下,放在这其实就更简单了,maven会把这里的文件最终编译后和classes文件放在一起,即和我们代码放在classpath下,自然而然也就不需要这个管maven资源过滤

如下图所示:
在这里插入图片描述
然后在修改mybatis configuration配置中的绑定关系

<mappers>
     <mapper resource="mapper/OrderMapper.xml"/>
</mappers>

我们可以清晰看到src/mian/resource下文件都编译到classes下面,所以也可以正常查出结果,到这,我们就可以很快的解决一开始提出的绑定异常问题了

读取maven项目资源文件请看怎么样读取maven工程resource目录下资源文件?
 
 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

魔道不误砍柴功

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值