Dawn's Blogs

分享技术 记录成长

0%

SSM学习之MyBatis (3) 映射文件

Mybatis 映射文件由 mapper 标签作为顶层标签,拥有属性 namespace。一个基本的映射文件内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<mapper namespace="ns">
<!-- 插入操作 -->
<insert id="insertID" parameterType="Xxx">
insert into xxx values(#{field1}, #{field2}, #{field3})
</insert>
<!-- 删除操作 -->
<delete id="deleteID", parameterType="Xxx">
delete frrom xxx where ...
</delete>
<!-- 更新操作 -->
<update id="updateID", parameterType="Xxx">
update xxx ser ... where ...
</update>
<!-- 查询操作 -->
<select id="selectID", parameterType="Xxx", resultType="Xxx">
select * from xxx where ...
</select>
</mapper>

${} 和 #{} 的区别:${} 本质上就是字符串的拼接,需要手动加上单引号;#{} 是占位符的方式。

增删改查

插入

insert 标签表示插入操作。

1
2
3
4
<!-- 插入操作 -->
<insert id="insertID" parameterType="Xxx">
insert into xxx values(#{field1}, #{field2}, #{field3})
</insert>

删除

delete 标签表示删除操作。

1
2
3
4
<!-- 删除操作 -->
<delete id="deleteID", parameterType="Xxx">
delete frrom xxx where ...
</delete>

更新

update 标签表示更新操作。

1
2
3
4
<!-- 更新操作 -->
<update id="updateID", parameterType="Xxx">
update xxx ser ... where ...
</update>

查询

select 标签表示查询操作。

1
2
3
4
<!-- 查询操作 -->
<select id="selectID", parameterType="Xxx", resultType="Xxx">
select * from xxx where ...
</select>

其他配置标签

自定义映射 resultMap

resultMap 用于处理字段名和实体类中的属性名不一致的情况。

  • 属性包括:collection
    • id:自定义映射的唯一标识。
    • type:查询的数据要映射的实体类的类型。
  • 子标签包括以下几种:
    • id:设置主键的映射关系,包括两种属性 property(设置实体类中的属性名)和column(设置数据库表中的字段名)。
    • result:设置普通字段的映射关系,包括两种属性 property(设置实体类中的属性名)和column(设置数据库表中的字段名)。
    • association:设置多对一的映射关系。
    • collection:设置一对多的映射关系。

动态 SQL

动态 SQL 包括以下几个标签。

if

if 标签可通过 test 属性的表达式进行判断,若表达式的结果为 true,则标签中的内容会执行;反之标签中的内容不会执行。

1
2
3
4
5
6
7
8
9
10
11
12
13
<!--List<Emp> getEmpListByCondition(Emp emp);-->
<select id="getEmpListByMoreTJ" resultType="Emp">
select * from t_emp where 1=1
<if test="ename != '' and ename != null">
and ename = #{ename}
</if>
<if test="age != '' and age != null">
and age = #{age}
</if>
<if test="sex != '' and sex != null">
and sex = #{sex}
</if>
</select>

where

where 用于添加 where 关键字,一般和 if 结合使用。

  • 若 where 标签中的 if 条件都不满足,不会添加 where 关键字。

  • 若 where 标签中的 if 条件满足,则 where 标签会自动添加 where 关键字,并将条件最前方多余的 and 去掉(但是不会去掉最后多余的 and)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<select id="getEmpListByMoreTJ2" resultType="Emp">
select * from t_emp
<where>
<if test="ename != '' and ename != null">
ename = #{ename}
</if>
<if test="age != '' and age != null">
and age = #{age}
</if>
<if test="sex != '' and sex != null">
and sex = #{sex}
</if>
</where>
</select>

trim

trim 用于去掉或添加标签中的内容,常用属性:

  • prefix:在 trim 标签中的内容的前面添加某些内容。
  • prefixOverrides:在 trim 标签中的内容的前面去掉某些内容。
  • suffix:在 trim 标签中的内容的后面添加某些内容。
  • suffixOverrides:在 trim 标签中的内容的后面去掉某些内容。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<select id="getEmpListByMoreTJ" resultType="Emp">
select * from t_emp
<trim prefix="where" suffixOverrides="and">
<if test="ename != '' and ename != null">
ename = #{ename} and
</if>
<if test="age != '' and age != null">
age = #{age} and
</if>
<if test="sex != '' and sex != null">
sex = #{sex}
</if>
</trim>
</select>

choose、when、otherwise

这三个标签相当于 if、else if、else。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!--List<Emp> getEmpListByChoose(Emp emp);-->
<select id="getEmpListByChoose" resultType="Emp">
select <include refid="empColumns"></include> from t_emp
<where>
<choose>
<when test="ename != '' and ename != null">
ename = #{ename}
</when>
<when test="age != '' and age != null">
age = #{age}
</when>
<when test="sex != '' and sex != null">
sex = #{sex}
</when>
<when test="email != '' and email != null">
email = #{email}
</when>
</choose>
</where>
</select>

foreach

foreach 用于循环生成语句。

1
2
3
4
5
6
7
<!--int deleteMoreByArray(int[] eids);-->
<delete id="deleteMoreByArray">
delete from t_emp where eid in
<foreach collection="eids" item="eid" separator="," open="(" close=")">
#{eid}
</foreach>
</delete>

sql、include

sql 可以记录一段公共 sql 片段,在使用的地方通过 include 标签进行引入。

1
2
3
4
5
<sql id="empColumns">
eid,ename,age,sex,did
</sql>

select <include refid="empColumns"></include> from t_emp