mybatis 详解(六)------通过mapper接口加载映射文件

摘要:
通过mapper接口加载映射文件,这对于SSM的三个主要框架的集成非常重要——根据id查询用户表中的数据id:唯一标识符。此文件中的id值不能是重复的resultType:返回值类型。数据库记录对应于对象parameterType:实体类的参数类型,即查询条件的类型--˃select*from userwhere id=#{id1}˂!

通过 mapper 接口加载映射文件,这对于后面 ssm三大框架 的整合是非常重要的。那么什么是通过 mapper 接口加载映射文件呢?

  我们首先看以前的做法,在全局配置文件 mybatis-configuration.xml 通过 <mappers> 标签来加载映射文件,那么如果我们项目足够大,有很多映射文件呢,难道我们每一个映射文件都这样加载吗,这样肯定是不行的,那么我们就需要使用 mapper 接口来加载映射文件

  以前的做法:

  mybatis 详解(六)------通过mapper接口加载映射文件第1张

  改进做法:使用 mapper 接口来加载映射文件

1、定义 userMapper 接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
packagecom.ys.mapper;
 
importorg.apache.ibatis.annotations.Delete;
importorg.apache.ibatis.annotations.Insert;
importorg.apache.ibatis.annotations.Select;
importorg.apache.ibatis.annotations.Update;
 
importcom.ys.po.User;
 
publicinterfaceUserMapper {
    //根据 id 查询 user 表数据
    publicUser selectUserById(intid) throwsException;
 
    //向 user 表插入一条数据
    publicvoidinsertUser(User user) throwsException;
     
    //根据 id 修改 user 表数据
    publicvoidupdateUserById(User user) throwsException;
     
    //根据 id 删除 user 表数据
    publicvoiddeleteUserById(intid) throwsException;
}

2、在全局配置文件 mybatis-configuration.xml 文件中加载 UserMapper 接口(单个加载映射文件)

mybatis 详解(六)------通过mapper接口加载映射文件第2张

3、编写UserMapper.xml 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?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.ys.mapper.UserMapper">
 
     
    <!-- 根据 id 查询 user 表中的数据
       id:唯一标识符,此文件中的id值不能重复
       resultType:返回值类型,一条数据库记录也就对应实体类的一个对象
       parameterType:参数类型,也就是查询条件的类型
    -->
    <select id="selectUserById"
            resultType="com.ys.po.User"parameterType="int">
        <!-- 这里和普通的sql 查询语句差不多,后面的 #{id}表示占位符,里面不一定要写id,写啥都可以,但是不要空着 -->
        select * from user where id = #{id1}
    </select>
     
     
     
    <!-- 根据 id 更新 user 表的数据 -->
    <update id="updateUserById"parameterType="com.ys.po.User">
        update user u
            <!-- <set>
                <iftest="username != null and username != ''">
                    u.username = #{username},
                </if>
                <iftest="sex != null and sex != ''">
                    u.sex = #{sex}
                </if>
            </set> -->
            <trim prefix="set"suffixOverrides=",">
                <iftest="username != null and username != ''">
                    u.username = #{username},
                </if>
                <iftest="sex != null and sex != ''">
                    u.sex = #{sex},
                </if>
            </trim>
         
         where id=#{id}
    </update>
     
     
    <!-- 向 user 表插入一条数据 -->
    <insert id="insertUser"parameterType="com.ys.po.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>

  

4、测试

1
2
3
4
5
6
7
8
9
//根据id查询user表数据
    @Test
    publicvoidtestSelectUserById() throwsException{
        //获取mapper接口
        UserMapper userMapper = session.getMapper(UserMapper.class);
        User user = userMapper.selectUserById(1);
        System.out.println(user);
        session.close();
    }

  

5、批量加载映射文件

1
2
3
4
5
6
<mappers>
       <!--批量加载mapper
          指定 mapper 接口的包名,mybatis自动扫描包下的mapper接口进行加载
         -->
       <packagename="com.ys.mapper"/>
</mappers>

  

 

6、注意 

  1、UserMapper 接口必须要和 UserMapper.xml 文件同名且在同一个包下,也就是说 UserMapper.xml 文件中的namespace是UserMapper接口的全类名

  mybatis 详解(六)------通过mapper接口加载映射文件第3张

  2、UserMapper接口中的方法名和 UserMapper.xml 文件中定义的 id 一致

  3、UserMapper接口输入参数类型要和 UserMapper.xml 中定义的 parameterType 一致

  4、UserMapper接口返回数据类型要和 UserMapper.xml 中定义的 resultType 一致

免责声明:文章转载自《mybatis 详解(六)------通过mapper接口加载映射文件》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇python操作Excel模块openpyxl领域驱动设计(DDD:Domain-Driven Design)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Spring Boot -- Spring Boot之@Async异步调用、Mybatis、事务管理等

这一节将在上一节的基础上,继续深入学习Spring Boot相关知识,其中主要包括@Async异步调用,@Value自定义参数、Mybatis、事务管理等。 本节所使用的代码是在上一节项目代码中,继续追加的,因此需要先学习上一节内容。 一、使用@Async实现异步调用 要在springboot中使用异步调用方法,只要在被调用的方法上面加上@Async就可以...

mybatis问题合集:#{}与${}区别、动态sql语句、缓存机制

一、MyBatis 中#{}和${}区别   #{} 是预编译处理,像传进来的数据会加个" "(#将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号)   ${} 就是字符串替换。直接替换掉占位符。$方式一般用于传入数据库对象,例如传入表名.   使用 ${} 的话会导致 sql 注入。什么是 SQL 注入呢?比如 select * from u...

利用Kettle转储接口数据

1.     项目背景 1.1.  项目背景 数据接口 API:应用程序接口(Application Program Interface)的简称,是实现计算机软件之间数据通信的工具。同时API也是一种中间件,为各个平台提供数据共享。在大数据与物联网发展的背景下,目前有大量的数据接口被提供或发掘出来,提供给开发者使用,应用到生活中的每一个细节中。本文旨在阐述...

SpringBoot添加对Mybatis的支持

1、修改maven配置文件pom.xml,添加对mybatis的支持: <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-bo...

如何使用Fiddler抓取APP接口和微信授权网页源代码

 Fiddler,一个抓包神器,不仅可以通过手机访问APP抓取接口甚至一些数据,还可以抓取微信授权网页的代码。 下载安装 1. 下载地址(官网):  https://www.telerik.com/download/fiddler 2. 或者关注以下公众号,回复“抓包工具”就可以获取网盘地址。 3. 官网填完账号等信息下载,然后安装,打开。 设置当前电脑...

【环境巡检】使用jmeter+ant+Jenkins+企业微信自动化巡检_jmeter实现脚本及响应超时、失败重试(1)

 一、分析准备  由于项目上线了,需要尽快接入巡检避免环境挂了不自知。虽然运维有运维侧的监控,但是测试还是从业务侧的巡检是否成功开展一些工作比较好。经过了一番调研决定使用jmeter+ant+Jenkins+企业微信自动化巡检及相关通知。 经过分析,我们要做如下的准备: 1、jmeter:实现巡检的接口调用,判断前后端接口返回内容、响应时间是否正常; 2、...