• Mybatis动态SQL解析


    1 为什么需要动态SQL?

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

    2 动态标签有哪些?

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

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

    3 举例说明

    if

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

        <!-- 动态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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    choose (when, otherwise)

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

        <!-- 动态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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    trim (where, set)

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

        <!-- 动态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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

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

        <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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    foreach

    需要遍历集合的时候:

        <!-- 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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 相关阅读:
    MobPush丨 iOS端快速集成方法
    2023-9-12 01背包问题
    网页论坛项目测试报告
    AB试验(四)基于规范流程的一个案例分析
    深刻解析数据库技术的要点以及应对策略 (软件设计师笔记)
    关于python中round方法在一些四舍五入时丢失精度
    经验分享:以国家新型基础测绘建设试点检验大势智慧技术能力
    python安全工具开发笔记(六)——Python爬虫BeautifulSoup模块的介绍
    C 风格文件输入/输出---无格式输入/输出
    springboot+jsp 农产品电子商城网站maven idea
  • 原文地址:https://blog.csdn.net/weixin_44688973/article/details/125893739