mybatis之动态Sql笔记

提示:mybatis笔记整合


前言

提示:mybatis标签解释:
在这里插入图片描述


一、mybayis 的< if >元素

    <select id="test1" parameterType="com.service.boot.entity.User" resultType="com.service.boot.entity.User">
        select * from User where 1=1
        <if test="account !=null and account !=''">
        and account like concat('%',#{account},'%')
        </if>
        <if test="password !=null and password !=''">
            and password=#{password}
        </if>
    </select>

二、mybayis 的< choose > < when > < otherwise >元素

在这里插入图片描述

<select id="test2" parameterType="com.service.boot.entity.User" resultType="com.service.boot.entity.User">
        select * from User where 1=1
       <choose>
           <when test="username!=null and username!=''">
               and username like concat('%',#{username},'%')
           </when>
           <when test="job!=null and job!=''">
               and job=#{job}
           </when>
           <otherwise>
              and age is not null
           </otherwise>
       </choose>
    </select>

三、mybayis 的< where > < trim >元素

在这里插入图片描述
在这里插入图片描述

    <select id="test3" parameterType="com.service.boot.entity.User" resultType="com.service.boot.entity.User">
        select * from User
        <where>
        <if test="account !=null and account !=''">
            and account like concat('%',#{account},'%')
        </if>
        <if test="password !=null and password !=''">
            and password=#{password}
        </if>
        </where>
    </select>

在这里插入图片描述

 <select id="test4" parameterType="com.service.boot.entity.User" resultType="com.service.boot.entity.User">
        select * from User
        <trim prefix="where" prefixOverrides="and">
            <if test="account !=null and account !=''">
                and account like concat('%',#{account},'%')
            </if>
            <if test="password !=null and password !=''">
                and password=#{password}
            </if>
        </trim>
    </select>

四、mybayis 的< set >元素

   <update id="test5" parameterType="com.service.boot.entity.User">
        update User
        <set>
            <if test="account !=null and account !=''">
                 account=#{account}
            </if>
            <if test="password !=null and password !=''">
                 password=#{password}
            </if>
        </set>
        where 
        id =#{id}
    </update>

五、mybayis 的< foreach>元素

在这里插入图片描述

   <select id="test6" parameterType="List" resultType="com.service.boot.entity.User">
        select * from User where id in
        <foreach collection="list" item="id" index="index" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

友情链接:wt_better的: mybatis foreach标签的使用

六、mybayis 的< bind>元素

在这里插入图片描述
在这里插入图片描述

    <select id="test7" parameterType="com.service.boot.entity.User" resultType="com.service.boot.entity.User">
        select * from User
        <bind name="usernameaaaaa" value="'%'+account+'%'"/>
        <where>
            <if test="account !=null and account !=''">
                and account =#{usernameaaaaa}
            </if>
            <if test="password !=null and password !=''">
                and password=#{password}
            </if>
        </where>
    </select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值