摘要:这里详细介绍一个Mybatis中mapper.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
<?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标签开始,由/mapper结束,可以把它想成一个空间,是映射文件
属性namespace:空间名,主要在代理中使用。这个namespace是唯一的。
这里把mapper标签和接口联系在一起了,namespace=写接口路径,映射文件要和接口在同一目录下
-->
<mapper namespace="com.dao.UserinfoDAO">
<!-- =============映射关系标签=============
属性type:写po类的包名类名,由于之前定义了po类的别名,这里就写这个别名
属性id:是这个映射标签的唯一标识
id标签是查询结果集中的唯一标识
属性column:查询出来的列名
属性property:是po类里所指定的列名
通常会在原列名后面加下划线,这是固定的,这里就是id后面_
-->
<resultMap type="com.po.UserinfoPO" id="userinfoMap">
<result column="userid" property="userid"/>
<result column="loginname" property="loginname"/>
<result column="loginpass" property="loginpass"/>
<result column="username" property="username"/>
<result column="upower" property="upower"/>
<result column="birthday" property="birthday"/>
<result column="sex" property="sex"/>
</resultMap>
<!-- ==================定义sql片段==============
sql:是sql片段标签属性id是该片段的唯一标识 -->
<sql id="zd">
userid,loginname,loginpass,username,upower,birthday,sex
</sql>
<!-- 增删改查标签里的id:一定要和接口里对应的方法名一致,
resultMap输出类型里写映射标签里的id
parameterType:输入类型,规范输入数据类型,指明查询时使用的参数类型-->
<!-- 验证登录 -->
<select id="login" resultMap="userinfoMap" parameterType="com.po.UserinfoPO">
<!-- 用include标签引入sql片段,refid写定义sql片段的id,where标签不要写在片段里 -->
select <include refid="zd"/> from userinfo
<where>
loginname=#{loginname} and loginpass=#{loginpass}
</where>
</select>

<!-- 查询用户列表 -->
<select id="userList" resultMap="userinfoMap" parameterType="com.po.UserinfoPO">
<!-- 用include标签引入sql片段,refid写定义sql片段的id,where标签不要写在片段里 -->
select <include refid="zd"/> from userinfo
</select>

<!-- 查询修改用户信息的id -->
<select id="updateid" resultMap="userinfoMap" parameterType="com.po.UserinfoPO">
<!-- 用include标签引入sql片段,refid写定义sql片段的id,where标签不要写在片段里 -->
select <include refid="zd"/> from userinfo
<where>userid=#{userid}</where>
</select>

<!-- 修改用户信息 -->
<update id="update" parameterType="com.po.UserinfoPO">
update userinfo
set loginname=#{loginname},loginpass=#{loginpass},username=#{username},
upower=#{upower},birthday=#{birthday},sex=#{sex}
where userid=#{userid}
</update>

<!-- 添加用户信息 -->
<insert id="insert" parameterType="com.po.UserinfoPO">
insert into userinfo(<include refid="zd"/>)
values
(#{userid},#{loginname},#{loginpass},#{username},#{upower},#{birthday},#{sex})
</insert>

<!-- 增删改查标签里的id:一定要和接口里对应的方法名一致 -->
<delete id="delete" parameterType="int">
delete from userinfo where userid=#{userid}
</delete>

<!-- 根据用户名模糊查询,根据权限查询 -->
<select id="select" resultMap="userinfoMap" parameterType="java.util.Map">
<!-- 用include标签引入sql片段,refid写定义sql片段的id,where标签不要写在片段里 -->
select <include refid="zd"/> from userinfo
<!-- 当页面没有输入用户名和选择权限,就让它的条件永远为真,就变成全查询了 -->
<where>
<if test="username == null and username = '' and upower == -1">
and 1=1
</if>
<if test="username != null and username !=''">
and username LIKE '%${username}%'
</if>
<if test="upower != -1">
and upower=#{upower}
</if>
</where>
</select>
</mapper>