通过mapper接口加载映射文件
通过 mapper 接口加载映射文件,对于以后与Spring框架的整合是非常重要的。那么什么是通过 mapper 接口加载映射文件呢?
我们首先看以前的做法,在全局配置文件 mybatis-configuration.xml 通过 <mappers> 标签来加载映射文件,那么如果我们项目足够大,有很多映射文件呢,难道我们每一个映射文件都这样加载吗,这样肯定是不行的,那么我们就需要使用 mapper 接口来加载映射文件。
<mappers>
<!-- 注册userMapper.xml文件-->
<mapper resource="mapper/userMapper.xml"/>
</mappers>
改进做法:使用 mapper 接口来加载映射文件。
特别注意
- XxxMapper 接口必须要和 XxxMapper.xml 文件同名且在同一个包下,也就是说 XxxMapper.xml 文件中的namespace是XxxMapper 接口的全类名
- XxxMapper 接口中的方法名和 XxxMapper .xml 文件中定义的 id 一致
- XxxMapper 接口输入参数类型要和 XxxMapper .xml 中定义的 parameterType 一致
- XxxMapper 接口返回数据类型要和 XxxMapper .xml 中定义的 resultType 一致
定义 UserMapper 接口
public interface UserMapper {
//根据 id 查询 user 表数据
public User selectUserById(int id) throws Exception;
//向 user 表插入一条数据
public void insertUser(User user) throws Exception;
//根据 id 修改 user 表数据
public void updateUserById(User user) throws Exception;
//根据 id 删除 user 表数据
public void deleteUserById(int id) throws Exception;
}
在mybatis-configuration.xml 文件中加载 UserMapper 接口
<mappers>
<mapper class="com.harvey.java01.mapper.UserMapper"/>
</mappers>
编写UserMapper.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.harvey.java01.mapper.UserMapper">
<!-- 根据 id 查询 user 表中的数据
id:唯一标识符,此文件中的id值不能重复
resultType:返回值类型,一条数据库记录也就对应实体类的一个对象
parameterType:参数类型,也就是查询条件的类型
-->
<select id="selectUserById"
resultType="com.harvey.java01.entity.User" parameterType="int">
<!-- 这里和普通的sql 查询语句差不多,后面的 #{id}表示占位符,里面不一定要写id,写啥都可以,但是不要空着 -->
select * from user where id = #{id1}
</select>
<!-- 根据 id 更新 user 表的数据 -->
<update id="updateUserById" parameterType="com.harvey.java01.entity.User">
update user u
<!-- <set>
<if test="username != null and username != ''">
u.username = #{username},
</if>
<if test="sex != null and sex != ''">
u.sex = #{sex}
</if>
</set> -->
<trim prefix="set" suffixOverrides=",">
<if test="username != null and username != ''">
u.username = #{username},
</if>
<if test="sex != null and sex != ''">
u.sex = #{sex},
</if>
</trim>
where id=#{id}
</update>
<!-- 向 user 表插入一条数据 -->
<insert id="insertUser" parameterType="com.harvey.java01.entity.User">
<!-- 将插入的数据主键返回到 user 对象中
keyProperty:将查询到的主键设置到parameterType 指定到对象的那个属性
select LAST_INSERT_ID():查询上一次执行insert 操作返回的主键id值,只适用于自增主键
resultType:指定 select LAST_INSERT_ID() 的结果类型
order:AFTER,相对于 select LAST_INSERT_ID()操作的顺序
-->
<selectKey keyProperty="id" resultType="int" order="AFTER">
select LAST_INSERT_ID()
</selectKey>
insert into user(username,sex,birthday,address)
value(#{username},#{sex},#{birthday},#{address})
</insert>
<!-- 根据 id 删除 user 表的数据 -->
<delete id="deleteUserById" parameterType="int">
delete from user where id=#{id}
</delete>
</mapper>
测试
//根据id查询user表数据
@Test
public void testSelectUserById() throws Exception{
//获取mapper接口
UserMapper userMapper = session.getMapper(UserMapper.class);
User user = userMapper.selectUserById(1);
System.out.println(user);
session.close();
}
批量加载映射文件
<mappers>
<!--批量加载mapper
指定 mapper 接口的包名,mybatis自动扫描包下的mapper接口进行加载
-->
<package name="com.harvey.java01.mapper"/>
</mappers>
参考: |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!