【Mybatis学习】Mybatis框架中的动态sql

Mybatis框架中的动态SQL

1.主要元素

动态sql的元素
元素作用备注
if语句判断单选择
choose(when、otherwise)语句选择,与case when类似多选择
trim(where、set) 处理语句拼装
foreach循环遍历遍历插入、删除、in语句等

2. if

用于查询语句中。 注意:where后面跟“1 = 1”,防止语句拼装错误。
    <select id="findByMsg" resultMap="bookMap1" parameterType="string">
        select <include refid="bookSql"/>
        from t_book
        where 1 = 1
          <if test="name != null and name != '' ">
              and name = #{name}
          </if>
    </select>
用于更新语句中。 注意:set会自动处理语句末尾的逗号。
    <update id="updateBook" parameterType="mybatis.domain.Book">
        update t_book
        set
        <if test="author != null and author !=''">
           author = #{author},
        </if>
        <if test="name != null and name !=''">
            name = #{name},
        </if>
        <if test="title != null and title !=''">
           title = #{title},
        </if>
        <if test="price != null and price !=''">
           price = #{price},
        </if>
        <if test="des != null and des !=''">
           des = #{des},
        </if>
        <if test="userId != null and userId !=''">
           user_id = #{userId}
        </if>
        where bid = #{id}
    </update>

3.choose(when、otherwise)

用于动态判断和拼装sql,满足不同要求。
   <select id="findBook" parameterType="mybatis.domain.Book" resultMap="bookMap1">
        select <include refid="bookSql"/>
        from t_book
        where 1 = 1
          <choose>
              <when test="name != null and name !='' ">
                  and name like concat('%',#{name},'%')
              </when>
              <when test="author != null and author != '' ">
                  and author = #{author}
              </when>
              <otherwise>
                  and name is not null
              </otherwise>
          </choose>
    </select>

4.trim(where、set)

对于 上面特殊语句“1 = 1”,我们可以用where去除。
    <select id="findByMsg" resultMap="bookMap1" parameterType="string">
        select <include refid="bookSql"/>
        from t_book
        <where>
            <if test="name != null and name != '' ">
              and name = #{name}
            </if>
        </where>
    </select>
trme可以调整特殊的sql语句,比如and、or。
trim属性
prefix语句的前缀
prefixOverrides即将去掉的内部语句的前缀
suffix语句的后缀
suffixOverrides 

    <select id="findByMsg" resultMap="bookMap1" parameterType="string">
        select <include refid="bookSql"/>
        from t_book
        <trim prefix="where" prefixOverrides="and" suffix="" suffixOverrides="">
            <if test="name != null and name !=''">
                and name = #{name}
            </if>
        </trim>
    </select>

set可以用于更新语句中,更新表中某一字段信息,而不用把整个对象属性全部更新。
    <!--set末尾遇到逗号自动去掉 -->
    <update id="updateBook" parameterType="mybatis.domain.Book">
        update t_book
        set
        <if test="author != null and author !=''">
           author = #{author},
        </if>
        <if test="name != null and name !=''">
            name = #{name},
        </if>
        <if test="title != null and title !=''">
           title = #{title},
        </if>
        <if test="price != null and price !=''">
           price = #{price},
        </if>
        <if test="des != null and des !=''">
           des = #{des},
        </if>
        <if test="userId != null and userId !=''">
           user_id = #{userId}
        </if>
        where bid = #{id}
    </update>
也可用trim元素自动管理后缀的逗号。
    <update id="updateBook" parameterType="mybatis.domain.Book">
        update t_book
        <trim prefix="set" suffixOverrides=",">
            ***
        </trim>
        where bid = #{id}
    </update>

5.foreach批量操作

foreach内部属性
collection传递到语句中的参数,可以是数组、List、Set等集合
item循环中当前的元素信息
index当前元素在集合中的位置下标
open/close以定义的符号来包裹每一组集合元素
separator每次遍历元素之后的分隔符号

批量插入。
    <insert id="batchInsertBook">
        insert into t_book(bid, author, name, title, price, des,user_id)
        values
          <foreach collection="list" item="item" open="" separator="," close="" index="key">
              (#{item.id},#{item.author},#{item.name},#{item.title},#{item.price},#{item.des},#{item.userId})
          </foreach>
    </insert>
批量删除
    <delete id="batchDelete">
        delete
        from t_book
        where bid in
          <foreach collection="list" item="item" open="(" separator="," close=")" index="key">
              #{item}
          </foreach>
    </delete>

7.bind自定义变量

可以用于不同数据库之间特殊字段的调整与转换。like模糊查询时,在Mysql中使用“%”,但是在Oracle中使用的时“||”,使用bind可以提高移植性。
    <!--可以传入多个查询字段,返回一个list-->
    <select id="find" resultType="mybatis.domain.Book">
        <bind name="nameLike" value=" '%' + name + '%' "/> <!--可以多次定义-->
        select <include refid="bookSql"/>
        from t_book
        where name like #{nameLike}
    </select>










  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值