Mybatis动态sql常用标签

查询 where 和 if

这个适合多条件查询,并且要对于非空的条件进行判断

<select id="findByCondition" resultType="org.example.pojo.Dept">
        select * from Dept
        <where>
            <if test="deptno!=null and ''!= deptno ">
                and deptno = #{deptno}
            </if>
            <if test="dname!=null and dname != ''">
                and dname = #{dname}
            </if>
        </where>
    </select>

注意:<if>标签后面可以写and,where会自动把if标签后面的and根据适当情况过滤低掉

查询where 和choose,when

    <select id="findByCondition" resultType="org.example.pojo.Dept">
        select * from Dept
        <where>
            <choose>
               <when test="dname != null and dname!=''">
                   dname = concat('%',#{dname},'%')
               </when>
               <when test="deptno!=null and deptno!=''">
                   deptno = #{deptno}
               </when>
               <otherwise>
                   deptno is not null
               </otherwise>
            </choose>
        </where>
    </select>

choose标签里面使用when,一旦第一个when的条件满足了,就不继续往后面运行了,只拼接并运行该when里面的sql语句。如果全部when标签的调价都不满足,则运行otherwise里面的sql代码。

更新set和if

update的动态sql标签,比如你传入时有多个条件,你需要判断条件,才知道能不能进行update,那么可以使用这个标签

<update id="updateEmpbyEmp" >
        update emp
        <set>
            <if test="ename !=null and emname!=''">
                , ename = #{ename}
            </if>
            <if test="job !=null and job != ''">
                , job = #{job}
            </if>
            <if test="mgr != null and mgr != ''">
                , mgr = #{mgr}
            </if>
        </set>
        where empno = #{empno}
    </update>

trim标签

这个标签作用比较多,既可以在update时候用,也可以在select的时候用。
trim标签具有几个属性

  • prefix:要动态添加的前缀

  • prefixOverrides:需要替换的前缀

  • suffix: 要补充的后缀

  • suffixOverrides:需要替换的后缀

    <update id="updateEmpByTrim">
        update emp
            <trim prefix="set" suffixOverrides=",">
                <if test= "ename != null and ename != ''">
                    ename = #{ename},
                </if>
                <if test="job !=null and job != ''">
                    job = #{job},
                </if>
                <if test="mgr != null">
                    mgr = #{mgr},
                </if>
            </trim>
        where empno = #{empno}
    </update>
    <select id="findEmpByTrim" resultType="emp">
        select * from emp
        <trim prefix="where" suffixOverrides="and">
            <if test="ename != null and ename != ''">
                ename like concat('%',#{ename},'%') and
            </if>
            <if test="job != null and job != ''">
                job like concat('%',#{job},'%') and
            </if>
        </trim>
    </select>

foreach标签

这个标签很常用
foreach标签有四个属性,

  • collections表示遍历的是集合(list)或者数组(array)或者set,map
  • separator 遍历时用什么字符分配,例如","
  • open 以什么开头,例如"("
  • close 以什么结尾,例如")"
  • item 中间变量名
    <select id="findForEach" resultType="emp">
        select * from emp
        where empno in
        <foreach collection="list" open="(" close =")" item="item" separator=",">
            #{item}
        </foreach>
    </select>

用foreach如何访问collection为map或者set?有知道的朋友麻烦告诉我一声,歇歇啦~~

bind模糊查询模板(比较鸡肋)

   <select id="findEmpByTrim" resultType="emp">
        select * from emp
        <trim prefix="where" suffixOverrides="and">
            <if test="ename != null and ename != ''">
                ename like concat('%',#{ename},'%') and
            </if>
            <if test="job != null and job != ''">
                <bind name="likePattern" value="'%'+job+'%'"/>
                job like #{likePattern} and
            </if>
        </trim>
    </select>

sql标签(也挺鸡肋的)

使用场景:有某个sql片段经常出现,比如是一个查询条件,出现了n次,老是复制粘贴觉得好累啊,那么可以使用sql标签和incloud标签配合,用sql定义那段sql,然后指向那个sql标签,就避免了多次重复使用,这个标签也挺鸡肋的哈哈哈哈

    <sql id="columnSql">
        empno, ename, job, mgr, hiredate,sal, comm,deptno
    </sql>
    <select id="findEmpByTrim" resultType="emp">
        select <include refid="columnSql" /> from emp
        <trim prefix="where" suffixOverrides="and">
            <if test="ename != null and ename != ''">
                ename like concat('%',#{ename},'%') and
            </if>
            <if test="job != null and job != ''">
                <bind name="likePattern" value="'%'+job+'%'"/>
                job like #{likePattern} and
            </if>
        </trim>
    </select>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值