一次mapper.xml配置文件问题

一、包结构

mapper所在的包结构:
在这里插入图片描述

二、问题描述

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) : 
com.dapeng.flow.repository.mapper.RoleUserMapper.listUserId

经检查,RoleUserMapper.java中listUserId与RoleUserMapper.xml的对应id一致,xml所在namespace也没错,xml返回类型也没有错。经过一边耗时的排查,发现是mapper.xml所在位置在非resource下造成的。

三、解决方案

在项目所在的pom.xml中配置mapper所在的包扫描:

<resources>
    <!--mapper.xml如果没有放置在resource下,需要进行如下配置,否则会出现如下问题:
    org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
    -->
    <resource>
		<directory>src/main/java</directory>
		<includes>
			<include>**/*.xml</include>
		</includes>
    </resource>
</resources>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis 是一款使用 XML 或注解配置的持久层框架,它可以自动化地将数据库中的数据映射到 Java 对象中。在 MyBatis 中,mappermapper.xml 是配对使用的,其中 mapper 是接口,而 mapper.xml 是映射配置文件mapper 接口中定义了数据库操作的方法,而 mapper.xml 中则定义了这些方法的 SQL 语句以及参数映射规则、结果集映射规则等。 下面是一个简单的例子: 1. 定义 mapper 接口 ```java public interface UserMapper { User selectUserById(Integer id); } ``` 2. 定义 mapper.xml 映射配置文件 ```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="com.example.UserMapper"> <resultMap id="userResultMap" type="com.example.User"> <id column="id" property="id"/> <result column="username" property="username"/> <result column="password" property="password"/> </resultMap> <select id="selectUserById" resultMap="userResultMap"> SELECT * FROM user WHERE id = #{id} </select> </mapper> ``` 上述代码中,namespace 属性指定了 mapper 接口的全限定名,resultMap 标签定义了一个结果集映射规则,select 标签定义了一个查询操作,其中 id 属性指定了 mapper 接口中的方法名,resultMap 属性指定了结果集映射规则的 id。 3. 在 MyBatis 配置文件中引入 mapper.xml ```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> <mappers> <mapper resource="com/example/UserMapper.xml"/> </mappers> </configuration> ``` 上述代码中,mapper 标签指定了映射配置文件的位置。 这样就完成了 mappermapper.xml配置。在代码中调用 selectUserById 方法时,MyBatis 会根据 mapper.xml 中的配置自动生成 SQL 语句,并将查询结果映射到 User 对象中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值