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

Mybatis动态SQL解析

作者头像
向着百万年薪努力的小赵
发布2022-12-02 10:50:57
4850
发布2022-12-02 10:50:57
举报
文章被收录于专栏:小赵的Java学习

文章目录

1 为什么需要动态SQL?

看一段Oracle存储过程代码:

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

由于前台传入的查询参数不同,所以写了很多的if else,还需要非常注意SQL语句里面的and、空格、逗号和转移的单引号这些,拼接和调试SQL就是一件非常耗时的工作。 MyBaits的动态SQL就帮助我们解决了这个问题,它是基于OGNL表达式的。

2 动态标签有哪些?

按照官网的分类,MyBatis 的动态标签主要有四类:

  • if
  • choose (when, otherwise)
  • trim (where, set)
  • foreach

3 举例说明

if

需要判断的时候,条件写在test中:

代码语言:javascript
复制
    <!-- 动态SQL where 和 if  -->
    <select id="selectBlogListIf" parameterType="blog" resultMap="BaseResultMap" >
        select bid, name, author_id authorId from blog
        <where>
            <if test="bid != null">
                AND bid = #{bid}
            </if>
            <if test="name != null and name != ''">
                AND name LIKE '%${name}%'
            </if>
            <if test="authorId != null">
                AND author_id = #{authorId}
            </if>
        </where>
    </select>

choose (when, otherwise)

需要选择—个条件的时候:

代码语言:javascript
复制
    <!-- 动态SQL choose -->
    <select id="selectBlogListChoose" parameterType="blog" resultMap="BaseResultMap" >
        select bid, name, author_id authorId from blog
        <where>
            <choose>
                <when test="bid !=null">
                    bid = #{bid, jdbcType=INTEGER}
                </when>
                <when test="name != null and name != ''">
                    AND name LIKE CONCAT(CONCAT('%', #{name, jdbcType=VARCHAR}),'%')
                </when>
                <when test="authorId != null ">
                    AND author_id = #{authorId, jdbcType=INTEGER}
                </when>
                <otherwise>
                </otherwise>
            </choose>
        </where>
    </select>

trim (where, set)

需要去掉where, and、逗号之类的符号的时候:

代码语言:javascript
复制
    <!-- 动态SQL set -->
    <update id="updateByPrimaryKey" parameterType="blog">
        update blog
        <set>
            <if test="name != null">
                name = #{name,jdbcType=VARCHAR},
            </if>
            <if test="authorId != null">
                author_id = #{authorId,jdbcType=CHAR},
            </if>
        </set>
        where bid = #{bid,jdbcType=INTEGER}
    </update>

用来指定或者去掉前缀或者后缀:

代码语言:javascript
复制
    <insert id="insertBlog" parameterType="blog">
    insert into blog
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="bid != null">
                bid,
            </if>
            <if test="name != null">
                name,
            </if>
            <if test="authorId != null">
                author_id,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="bid != null">
                #{bid,jdbcType=INTEGER},
            </if>
            <if test="name != null">
                #{name,jdbcType=VARCHAR},
                <!-- #{name,jdbcType=VARCHAR,typeHandler=com.gupaoedu.type.MyTypeHandler}, -->
            </if>
            <if test="authorId != null">
                #{authorId,jdbcType=INTEGER},
            </if>
        </trim>
    </insert>

foreach

需要遍历集合的时候:

代码语言:javascript
复制
    <!-- foreach 动态SQL 批量删除 -->
    <delete id="deleteByList" parameterType="java.util.List">
        delete from blog where bid in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item.bid,jdbcType=INTEGER}
        </foreach>
    </delete>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-07-20,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 1 为什么需要动态SQL?
  • 2 动态标签有哪些?
  • 3 举例说明
    • if
      • choose (when, otherwise)
        • trim (where, set)
          • foreach
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档