前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Mybatis】动态sql

【Mybatis】动态sql

作者头像
小尘要自信
发布2023-10-10 15:27:07
1850
发布2023-10-10 15:27:07
举报
文章被收录于专栏:CSDN小尘要自信

Mybatis框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能,它存在的意义是为了 解决 拼接SQL语句字符串时的痛点问题。

if

if:根据标签内中test属性对应的表达式决定标签中的内容是否需要拼接到sql中

代码语言:javascript
复制
<!--List<emp> getEmpByCondition(Emp emp);-->
    <!-- if:根据标签内中test属性对应的表达式决定标签中的内容是否需要拼接到sql中 -->
    <select id="getEmpByConditionOne" resultType="Emp">
        select * from t_emp where 1=1
        <if test="empName !=null and empName !='' ">
          and  emp_name=#{empName}
        </if>
        <if test="age !=null and age !=''">
           and age=#{age}
        </if>
        <if test="sex !=null and sex!=''">
           and  sex=#{sex}
        </if>
        <if test="email !=null and email !=''">
           and  email=#{email}
        </if>
    </select>

where

where 标签中如果有内容的话,会自动添加where关键字,并且会把where标签中多于的and或者or去掉(内容前的and),如果where标签中没有任何内容的话,where标签就不会出现

代码语言:javascript
复制
<!-- where 标签中如果有内容的话,会自动添加where关键字,并且会把where标签中多于的and或者or去掉(内容前的and),如果where标签中没有任何内容的话,where标签就不会出现-->
    <select id="getEmpByConditionTwo" resultType="Emp">
        select * from t_emp
        <where>
            <if test="empName !=null and empName !='' ">
                emp_name=#{empName}
            </if>
            <if test="age !=null and age !=''">
                and age=#{age}
            </if>
            <if test="sex !=null and sex!=''">
                and  sex=#{sex}
            </if>
            <if test="email !=null and email !=''">
                and  email=#{email}
            </if>
        </where>
    </select>

trim

prefix/suffix:将trim标签中内容前面或后面添加指定内容 prefixOverrides/suffixOverrides:将trim标签中内容前面或后面去掉指定内容 若标签中没有内容:trim标签也没有任何效果

代码语言:javascript
复制
 <select id="getEmpByCondition" resultType="Emp">
        select * from t_emp
        <trim prefix="where" suffixOverrides="and|or">
            <if test="empName !=null and empName !='' ">
                emp_name=#{empName} and
            </if>
            <if test="age !=null and age !=''">
                age=#{age}  and
            </if>
            <if test="sex !=null and sex!=''">
              sex=#{sex}   and
            </if>
            <if test="email !=null and email !=''">
               email=#{email}  and
            </if>
        </trim>
    </select>

choose、when、otherwise

choose: when: otherwise: choose标签为父标签,when与otherwise需要写在choose中 这一套标签相当于Java中的if,else if else,when相当于if,else if ,otherwise相当于else when至少要有一个,otherwise最多只能有一个

代码语言:javascript
复制
 <!-- List<Emp> getEmpByChoose(Emp emp);-->
    <select id="getEmpByChoose" resultType="Emp">
        select * from t_emp
        <where>
            <choose>
                <when test="empName !=null and empName !='' ">
                    emp_name=#{empName}
                </when>
                <when test="age !=null and age !='' ">
                    age=#{age}
                </when>
                <when test="sex !=null and sex !='' ">
                    sex=#{sex}
                </when>

                <when test="email !=null and email !='' ">
                    email=#{email}
                </when>
                <otherwise>
                    did=1
                </otherwise>
            </choose>
        </where>
    </select>

foreach

场景:批量操作:添加;删除 foreach: collection:需要设置循环的数组或集合 即 传来的数据-即数组 item:表示数组或集合中的每一个数据 即 in里面数据的属性 separator:循环体之间的分隔符 以什么为分隔符 open:以什么开始:foreach标签所循环的所有内容的开始符 close以什么结束:foreach标签所循环的所有内容的结束符 通过这两个标签可以省略() separator:以什么为分隔符 item:in里面数据的属性,即数组中的每一个数据

代码语言:javascript
复制
 <!--int deleteMoreByArray(@Param("eids") Integer[] eids);-->
    <delete id="deleteMoreByArray">
        delete from t_emp where
            <foreach collection="eids" item="eid" separator="or" >
                eid=#{eid}
            </foreach>
        <!--delete from t_emp where eid in
            (
        <foreach collection="eids" item="eid" separator=",">
            #{eid}
        </foreach>
                )-->

    </delete>
代码语言:javascript
复制
<!--批量增加-->
    <!--int insertMoreByList(@Param("emps") List<Emp> emps);-->
    <insert id="insertMoreByList" >
        insert into t_emp values
        <foreach collection="emps" item="emp" separator=",">
            (null,#{emp.empName},#{emp.age},#{emp.sex},#{emp.email},null)
        </foreach>
    </insert>

sql

代码语言:javascript
复制
将常用的sql片段进行记录,需要用的时候可以直接引入
       <sql id="" ></sql>//id自己命名
       <include refid=" "></include>//这里填id名称
代码语言:javascript
复制
<sql id="empColumns">
eid,ename,age,sex,did
</sql>
select <include refid="empColumns"></include> from t_emp
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-07-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • if
  • where
  • trim
  • choose、when、otherwise
  • foreach
  • sql
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档