1.<sql>标签
定义重复使用的字段提高复用性
点击查看代码
<sql id="Base_Column_List">
id, name, contract_no, pdf_url, member_phone, elder_id, start_time, end_time, status,
sort, level_desc, create_time, create_by, update_time, update_by, remark, check_in_no,
sign_date, release_submitter, release_date, release_pdf_url
</sql>
2.<include>标签
通过<include refid=" " / >标签引用,refid的值就是
点击查看代码
<select id="listAllContracts" parameterType="java.lang.Long" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from contract
</select>
3.<select>标签
用于select映射
点击查看代码
<select id="selectDevices" resultType="com.zzyl.vo.FloorVo">
select id,remark from floor
</select>
4.<update>标签
用于update映射
点击查看代码
<update id="updateBedByBedId">
update bed
set bed_number = #{bedNumber},
sort = #{sort},
update_time = NOW()
where
id = #{id}
</update>
5.<insert>标签
用于insert映射
点击查看代码
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
insert into member_elder(member_id,elder_id,create_time,create_by)
values(#{memberId},#{elderId},#{createTime},#{createBy})
</insert>
6.<delete>标签
用于delete映射
点击查看代码
<delete id="deleteById">
delete from nursing_level where id = #{id}
</delete>
7.<selectKey>标签
用于数据回显,keyProperty为回显的属性
点击查看代码
<insert id="addUser" parameterType="com.zzyl.entity.Elder">
<selectKey resultType="int" keyProperty="id" order="AFTER">
SELECT LAST_INSERT_ID()
</selectKey>
INSERT INTO user (name, age) VALUES (#{name}, #{age})
</insert>
8.<mapper>标签
用于定义一个 Mapper 接口的命名空间。属性 namespace 指定该 Mapper 接口的全限定名
点击查看代码
<mapper namespace="com.zzyl.mapper.ElderMapper">
</mapper>
9.<resultMap>标签
用于复杂的结果集映射场景。
10.<collection>标签
用于处理一对多关系
如:
可以看到我在对象里嵌套了一个对象集合
点击查看代码
public class Department {
private Long id;
private String name;
private List<Employee> employees;
// Getters and Setters
}
点击查看代码
<!-- 定义结果映射 -->
<resultMap id="DepartmentResultMap" type="com.example.model.Department">
<id column="id" property="id" />
<result column="name" property="name" />
<!-- 处理一对多关系 -->
<collection property="employees" ofType="com.example.model.Employee" select="com.example.mapper.EmployeeMapper.selectEmployeesByDepartmentId" column="id" />
</resultMap>
11.<foreach>标签
遍历标签,open为头,close为尾,separator为连接符,collection为返回的名字
点击查看代码
<select id="selectByPostname" resultType="java.lang.String">
select post_no from sys_post where post_name
in
<foreach collection="list" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
12.<where>标签
点击查看代码
<select id="selctByPage" resultType="com.zzyl.vo.ApplicationVo">
select
c.check_in_code code,c.applicat_id,c.applicat,c.create_time applicationTime,
c.update_time finishTime,c.flow_status,c.id,c.status,c.title,e.status type
from check_in c
left join elder e on c.elder_id=e.id
<where>
<if test="type!=null and type!=''" >
and e.status=#{type}
</if>
<if test="status!=null and status != ''">
and c.status=#{status}
</if>
<if test="startTime!=null and startTime != ''">
and c.create_time >= #{startTime}
</if>
<if test="endTime!=null and endTime != ''">
and c.create_time <= #{endTime}
</if>
</where>
</select>
13.<set>标签
自动生成 SET 子句,确保每个条件前都有适当的逗号分隔。
点击查看代码
<update id="updatePost" parameterType="com.zzyl.vo.ApplicationVo">
UPDATE sys_post
<set>
<if test="postName != null">
post_name = #{postName},
</if>
<if test="postNo != null">
post_no = #{postNo},
</if>
<if test="status != null">
status = #{status},
</if>
</set>
WHERE post_id = #{postId}
</update>
14.<if>标签
判断若test满足则拼接,不满足不拼接,上面演示过了这里不演示
15.<choose>标签
16.<when>标签
17.<otherwise>标签
三个标签相互联系放在一起说
<choose> 标签:
类似于 sql 中的 case 语句,用于在多个条件之间进行选择。只执行第一个匹配的条件块。
<when> 标签:
当..时满足则执行
<otherwise> 标签:
都不满足则执行
点击查看代码
<select id="selectByPostConditions" resultType="java.lang.String">
SELECT post_no
FROM sys_post
<where>
<choose>
<when test="postName != null">
post_name = #{postName}
</when>
<when test="postId != null">
post_id = #{postId}
</when>
<otherwise>
post_status = 'ACTIVE'
</otherwise>
</choose>
</where>
</select>