服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|

服务器之家 - 编程语言 - JAVA教程 - 详解mybatis通过mapper接口加载映射文件

详解mybatis通过mapper接口加载映射文件

2020-12-13 20:14YSOcean JAVA教程

本篇文章主要介绍了mybatis通过mapper接口加载映射文件 ,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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

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

以前的做法:

详解mybatis通过mapper接口加载映射文件  

改进做法:使用 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
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.ys.mapper;
 
 
 
import org.apache.ibatis.annotations.delete;
 
import org.apache.ibatis.annotations.insert;
 
import org.apache.ibatis.annotations.select;
 
import org.apache.ibatis.annotations.update;
 
 
 
import com.ys.po.user;
 
 
 
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;
 
}

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

详解mybatis通过mapper接口加载映射文件

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?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>
 
        <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.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
10
11
12
13
14
15
16
17
18
19
20
21
//根据id查询user表数据
 
@test
 
public void testselectuserbyid(){
 
  /*这个字符串由 usermapper.xml 文件中 两个部分构成
 
    <mapper namespace="com.ys.po.usermapper"> 的 namespace 的值
 
    <select id="selectuserbyid" > id 值*/
 
  string statement = "com.ys.mapper.usermapper.selectuserbyid";
 
  user user = session.selectone(statement, 1);
 
  system.out.println(user);
 
  session.close();
 
}

5、批量加载映射文件

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

6、注意 

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

详解mybatis通过mapper接口加载映射文件  

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

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

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/ysocean/p/7301548.html?utm_source=tuicool&utm_medium=referral

延伸 · 阅读

精彩推荐