必须拿下的Mybatis动态SQL

1、if标签

    <select id="getUser" parameterType="java.lang.Integer" resultType="User">
        select * from user
        <if test="id != null">
            where id = #{id}
        </if>
    </select>

这样写如果id为null则查询所有,传了id就按id精准查询,如果有多个参数呢?

    <select id="getUser" resultType="User">
        select * from user where
        <if test="id != null">
            id = #{id}
        </if>
        <if test="userName != null and userName !=''">
            and user_name like CONCAT(CONCAT('%',#{userName}),'%')
        </if>
    </select>

如果 userName等于 null,那么查询语句为 select * from user where id=#{id},但是如果id为空呢?那么查询语句为 select * from user where and user_name like "%#{author}%",这是错误的SQL 语句,如何解决呢?请看下面的 where 标签!

2、where标签

    <select id="getUser" resultType="User">
        select * from user
        <where>
            <if test="id != null">
                id = #{id}
            </if>
            <if test="userName != null and userName !=''">
                and user_name like CONCAT(CONCAT('%',#{userName}),'%')
            </if>
            <if test="code != null">
                and code = #{code}
            </if>
        </where>
    </select>

<where>标签会进行自动判断
如果任何条件都不成立,那么就在sql语句里就不会出现where关键字
如果有任何条件成立,会自动去掉多出来的 and 或者 or。

在查询时我们会遇到多参数问题,那么在update时候也是,这时候可以使用set标签

3、set标签

    <update id="updateUser" parameterType="User">
        update user
        <set>
        <if test="userName != null and userName != ''">
            user_name = #{userName,jdbcType=VARCHAR},
        </if>
        <if test="password != null and password !=''">
            password = #{password,jdbcType=VARCHAR},
        </if>
        </set>
        where id=#{id}
    </update>

set 标签会动态地在行首插入 set 关键字,并会删掉额外的逗号(这些逗号是在使用条件语句给列赋值时引入的)

4、Choose标签

Mybatis里面没有else标签,有时候,我们不想用到所有的查询条件,只想选择其中的一个,查询条件有一个满足即可,使用 choose 标签可以解决此类问题

    <select id="getUser" resultType="User">
        select * from user
        <where>
        <choose>
            <when test="userName != null and userName !=''">
                and user_name like CONCAT(CONCAT('%',#{userName}),'%')
            </when>
            <when test="code != null">
                and code =#{code}
            </when>
            <otherwise>
                and id > 1
            </otherwise>
        </choose>
        </where>
    </select>

其作用是: 提供了任何条件,就进行条件查询,否则就使用id>1这个条件。

5、 foreach标签

  •   collection:指定输入对象中的集合属性
  •   item:每次遍历生成的对象
  •   open:开始遍历时的拼接字符串
  •   close:结束时拼接的字符串
  •   separator:遍历对象之间需要拼接的字符串
        select * from user
        where id in
        <foreach collection="list" item="id" open="(" close=")" separator=",">
            id=#{id}
        </foreach>

温馨提示:List对象默认用"list"代替作为键;数组对象有"array"代替作为键;当然在作为入参时可以使用@Param("keyName")来设置键,设置keyName后,list和array将会失效,需要使用keyName。

除了这种情况外,还有一种作为参数对象的某个字段的时候。举个例子:如果User有属性List ids。入参是User对象,那么这个collection = "ids"。如果User有属性IdEntity Ids;其中IdEntity是个对象,Ids有个属性List id;入参是User对象,那么collection = "ids.id"

6、sql片段

有时候可能某个 sql 语句我们用的特别多,为了增加代码的重用性,简化代码,我们需要将这些代码抽取出来,然后使用时直接调用。

比如我们上述查询sql都用的select * ,实际工作中不推荐使用select *,写个sql片段

    <sql id="Base_Column_List">
        id, user_name, code
    </sql>

看看如何使用它


       select <include refid="Base_Column_List"/> from user
        where id in
        <foreach collection="list" item="id" open="(" close=")" separator=",">
            id=#{id}
        </foreach>

注意:

  • 最好基于 单表来定义 sql 片段,提高片段的可重用性
  • 在 sql 片段中不要包括 where

7、bind标签

bind标签就像是再做一次字符串拼接,方便后续使用

      <select id="listUser" resultType="User">
            <bind name="likename" value="'%' + userName + '%'" />
            select * from  user where user_name like #{likename}
        </select>

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LoneWalker、

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值