Myabtis动态SQL,你真的会了吗?

时间:2020-09-06 14:22:41   收藏:0   阅读:52

目录

前言

什么是动态SQL?

常用的标签

if

<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="bedNum!=null and bedNum!=‘‘ ">
      and bed_num=#{bedNum}
  </if>

choose、when、otherwise

<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>

where

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;
<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

<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 分隔符,表示迭代时每个元素之间以什么分隔

set

<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>
<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

<!-- 查询字段 -->
<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>

include

<select id="selectAll" resultMap="BaseResultMap">
    SELECT
    <include refid="Base_Column_List" />
    FROM student
    <include refid="Example_Where_Clause" />
</select>

总结

拓展一下

Mybatis中如何避免魔数

<if test="type!=null and type==1">
    -- ....获取医生的权限
</if>

<if test="type!=null and type==2">
    -- ....获取护士的权限
</if>
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>

如何引用其他XML中的SQL片段

<sql id="Base_Column_List">
    ID,MAJOR,BIRTHDAY,AGE,NAME,HOBBY
</sql>
  <include refid="com.xxx.dao.xxMapper.Base_Column_List"></include>

总结

原文:https://www.cnblogs.com/Chenjiabing/p/13620884.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!