标签:添加 ack 中医 配置 col domain resultmap case select
case when then else then end....这种语法,能够根据某种条件动态的拼接出需要的SQL。如果的意思吧,Java语法中也有,只有判断条件为true才会执行其中的SQL语句。
<select id =‘selectPats‘ resultType=‘com.xxx.domain.PatientInfo‘>
select * from patient_info
where status=1
<!--前端传来的住院号不为null,表示需要根据住院号筛选,此时Where语句就需要加上这个条件-->
<if test="iptNum!=null">
and ipt_num=#{iptNum}
</if>
<!--床位号筛选-->
<if test="bedNum!=null">
and bed_num=#{bedNum}
</if>
</select>
<if>标签中的属性test用来指定判断条件,那么问题来了,上面的例子中的test中判断条件都是一个条件,如果此时变成两个或者多个条件呢?和SQL的语法类似,and连接即可,如下: <if test="bedNum!=null and bedNum!=‘‘ ">
and bed_num=#{bedNum}
</if>
choose 元素,它有点像 Java 中的 switch 语句。<select id="selectPats"
resultType="com.xxx.domain.PatientInfo">
select * from patient_info where 1=1
<choose>
<!--住院号不为null时,根据住院号查找-->
<when test="iptNum != null">
AND ipt_num=#{iptNum}
</when>
<!--床位号不是NUll-->
<when test="bedNum != null">
AND bed_num = #{bedNum}
</when>
<otherwise>
AND status=1
</otherwise>
</choose>
</select>
choose 元素,按顺序判断 when 中的条件出否成立,如果有一个成立,则 choose 结束。当 choose 中所有 when 的条件都不满则时,则执行 otherwise 中的 sql。类似于 Java 的 switch 语句,choose 为 switch,when 为 case,otherwise 则为default。choose标签的例子中的查询,如果去掉where后的1=1此时的SQL语句会变成什么样子,有三种可能的SQL,如下:select * from patient_info where AND ipt_num=#{iptNum};
select * from patient_info where AND bed_num = #{bedNum};
select * from patient_info where AND status=1;
where后面多了个AND。如何解决呢?此时就要用到where这个标签了。where 元素只会在子元素返回任何内容的情况下才插入 WHERE 子句。而且,若子句的开头为 AND 或 OR,where 元素也会将它们去除。<select id="selectPats"
resultType="com.xxx.domain.PatientInfo">
select * from patient_info
<where>
<choose>
<!--住院号不为null时,根据住院号查找-->
<when test="iptNum != null">
AND ipt_num=#{iptNum}
</when>
<!--床位号不是NUll-->
<when test="bedNum != null">
AND bed_num = #{bedNum}
</when>
<otherwise>
AND status=1
</otherwise>
</choose>
</where>
</select>
foreach是用来对集合的遍历,这个和Java中的功能很类似。通常处理SQL中的in语句。foreach 元素的功能非常强大,它允许你指定一个集合,声明可以在元素体内使用的集合项(item)和索引(index)变量。它也允许你指定开头与结尾的字符串以及集合项迭代之间的分隔符。这个元素也不会错误地添加多余的分隔符List、Set 等)、Map 对象或者数组对象作为集合参数传递给 foreach。当使用可迭代对象或者数组时,index 是当前迭代的序号,item 的值是本次迭代获取到的元素。当使用 Map 对象(或者 Map.Entry 对象的集合)时,index 是键,item 是值。<select id="selectPats" resultType="com.xxx.domain.PatientInfo">
SELECT *
FROM patient_info
WHERE ID in
<foreach item="item" index="index" collection="list"
open="(" separator="," close=")">
#{item}
</foreach>
</select>
| 属性 | 含义 |
|---|---|
| item | 表示在迭代过程中每一个元素的别名 |
| index | 表示在迭代过程中每次迭代到的位置(下标) |
| open | 前缀 |
| close | 后缀 |
| separator | 分隔符,表示迭代时每个元素之间以什么分隔 |
<update id="updateStudent" parameterType="Object">
UPDATE STUDENT
SET NAME = #{name},
MAJOR = #{major},
HOBBY = #{hobby}
WHERE ID = #{id};
</update>
<update id="updateStudent" parameterType="Object">
UPDATE STUDENT SET
<if test="name!=null and name!=‘‘ ">
NAME = #{name},
</if>
<if test="hobby!=null and hobby!=‘‘ ">
MAJOR = #{major},
</if>
<if test="hobby!=null and hobby!=‘‘ ">
HOBBY = #{hobby}
</if>
WHERE ID = #{id};
</update>
上面的例子中没有使用 if 标签时,如果有一个参数为 null,都会导致错误。当在 update 语句中使用 if 标签时,如果最后的 if 没有执行,则或导致逗号多余错误。使用 set 标签可以将动态的配置 set 关键字,和剔除追加到条件末尾的任何不相关的逗号。
使用 set+if 标签修改后,如果某项为 null 则不进行更新,而是保持数据库原值。此时的查询如下:
<update id="updateStudent" parameterType="Object">
UPDATE STUDENT
<set>
<if test="name!=null and name!=‘‘ ">
NAME = #{name},
</if>
<if test="hobby!=null and hobby!=‘‘ ">
MAJOR = #{major},
</if>
<if test="hobby!=null and hobby!=‘‘ ">
HOBBY = #{hobby}
</if>
</set>
WHERE ID = #{id};
</update>
<sql>这个标签了。<select> 结构清晰也可将 sql 语句分解。如下:<!-- 查询字段 -->
<sql id="Base_Column_List">
ID,MAJOR,BIRTHDAY,AGE,NAME,HOBBY
</sql>
<!-- 查询条件 -->
<sql id="Example_Where_Clause">
where 1=1
<trim suffixOverrides=",">
<if test="id != null and id !=‘‘">
and id = #{id}
</if>
<if test="major != null and major != ‘‘">
and MAJOR = #{major}
</if>
<if test="birthday != null ">
and BIRTHDAY = #{birthday}
</if>
<if test="age != null ">
and AGE = #{age}
</if>
<if test="name != null and name != ‘‘">
and NAME = #{name}
</if>
<if test="hobby != null and hobby != ‘‘">
and HOBBY = #{hobby}
</if>
</trim>
</sql>
<sql>是天仙配,是共生的,include用于引用sql标签定义的常量。比如引用上面sql标签定义的常量,如下:<select id="selectAll" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List" />
FROM student
<include refid="Example_Where_Clause" />
</select>
refid这个属性就是指定<sql>标签中的id值(唯一标识)。魔数的,何为魔数?简单的说就是一个数字,一个只有你知道,别人不知道这个代表什么意思的数字。通常我们在Java代码中都会定义一个常量类专门定义这些数字。type来区分,比如type=1是医生,type=2是护士,估计一般人会这样写:<if test="type!=null and type==1">
-- ....获取医生的权限
</if>
<if test="type!=null and type==2">
-- ....获取护士的权限
</if>
type代表的含义变了,那你是不是涉及到的SQL都要改一遍。package com.xxx.core.Constants;
public class CommonConstants{
//医生
public final static int DOC_TYPE=1;
//护士
public final static int NUR_TYPE=2;
}
<if test="type!=null and type==@com.xxx.core.Constants.CommonConstants@DOC_TYPE">
-- ....获取医生的权限
</if>
<if test="type!=null and type==@com.xxx.core.Constants.CommonConstants@NUR_TYPE">
-- ....获取护士的权限
</if>
@+全类名+@+常量。resultMap或者这个<sql>片段已经在另外一个xxxMapper.xml中已经定义过了,此时当前的xml还需要用到,难不成我复制一份?小白什么也不问上来就复制了,好吧,后期修改来了,每个地方都需要修改了。难受不?com.xxx.dao.xxMapper这个Mapper的XML中定义了一个SQL片段如下:<sql id="Base_Column_List">
ID,MAJOR,BIRTHDAY,AGE,NAME,HOBBY
</sql>
com.xxx.dao.PatinetMapper中的XML文件中需要引用,如下: <include refid="com.xxx.dao.xxMapper.Base_Column_List"></include>
<select>标签中的resultMap同样可以这么引用,和上面引用的方式一样,不再赘述了。标签:添加 ack 中医 配置 col domain resultmap case select
原文地址:https://www.cnblogs.com/Chenjiabing/p/13620884.html