• 【Mybatis】动态 SQL


    动态 sql 是 Mybatis 的强⼤特性之⼀,能够完成不同条件下不同的 sql 拼接。

    标签

    前端用户输入时有些选项是非必填的, 那么此时传到后端的参数是不确定的, 比如:
    在这里插入图片描述

    那如果在添加⽤户的时候有不确定的字段传⼊,程序应该如何实现呢?

    这个时候就需要使⽤动态标签 来判断了,⽐如添加的时候性别 country 和 city 为⾮必填字段,具体实现如下:

        <insert id="insert" parameterType="com.example.springboot3.model.User">
            insert into userinfo (
                  username, phone, email,
                  <if test="country != null">
                        country,
                  </if>
                  <if test="city != null">
                        city,
                  </if>
            ) values (
                  #{username},#{phone}, #{email},
                  <if test="country != null">
                        #{country},
                  </if>
                  <if test="city != null">
                        #{city},
                  </if>
            )
        </insert>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    在这里插入图片描述

    这样当 test 中判断结果为 true 时才会拼接上去.

    标签

    上面的插⼊⽤户功能,country 和 city 是选填项,如果所有字段都是选填项,就考虑使⽤ 标签结合 标签,对多个字段都采取动态⽣成的⽅式。

    标签中有如下属性:

    • prefix:表示整个语句块,以prefix的值作为前缀
    • suffix:表示整个语句块,以suffix的值作为后缀
    • prefixOverrides:表示整个语句块要去除掉的前缀
    • suffixOverrides:表示整个语句块要去除掉的后缀
        <insert id="insert" parameterType="com.example.springboot3.model.User">
            insert into userinfo
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="username != null">
                    username,
                </if>
                <if test="phone != null">
                    phone,
                </if>
                <if test="email != null">
                    email,
                </if>
                <if test="country != null">
                    country,
                </if>
                <if test="city != null">
                    city,
                </if>
            </trim>
            <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="username != null">
                    #{username},
                </if>
                <if test="phone != null">
                    #{phone},
                </if>
                <if test="email != null">
                    #{email},
                </if>
                <if test="country != null">
                    #{country},
                </if>
                <if test="city != null">
                    #{city},
                </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
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    在以上 sql 动态解析时,会对 部分做如下处理:

    • 基于 prefix 配置,开始部分加上 (
    • 第二个 开始部分会加上 values (
    • 基于 suffix 配置,结束部分加上 )
    • 多个 组织的语句都以 , 结尾,在最后拼接好的字符串还会以 , 结尾,会基于 suffixOverrides 配置去掉最后⼀个 ,

    标签

    主要作用: 实现查询中的 where 替换, 可以实现无条件查询.
    若存在查询条件会生成 where 的 sql 查询, 并且 where 标签可以去除最前面的 and
    如果 where 中的所有参数为空, 那么 where 的 sql 就不会生成, 直接就是无条件查询

    比如根据 国家(地区) 或者城市进行搜索, 根据属性做 where 条件查询,国家(地区) 或者城市不为 null 的,作为查询条件。

    UserMapper 接口中的方法:

    List<User> selectByCondition(String country, String city);
    
    • 1

    UserMapper.xml 中的实现:

        <select id="selectByCondition" resultType="com.example.springboot3.model.User">
            select * from userinfo
            <where>
                <if test="country != null">
                    and country=#{country}
                </if>
                <if test="city != null">
                    and city=#{city}
                </if>
            </where>
        </select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

    以上标签也可以使⽤ 替换。

    标签

    主要作用: 进行修改操作时, 配合 if 处理非必须传输的参数, 特点是自动去除最后一个 ,

    根据传⼊的⽤户对象属性来更新⽤户数据,可以使⽤标签来指定动态内容。

    UserMapper 接口中的方法:

    int updateById(User user);
    
    • 1

    UserMapper.xml 中的实现:

        <update id="updateById">
            update userinfo
            <set>
                <if test="username != null">
                    username=#{username},
                </if>
                <if test="phone != null">
                    phone=#{phone},
                </if>
                <if test="email != null">
                    email=#{email},
                </if>
                <if test="country != null">
                    country=#{country},
                </if>
                <if test="city != null">
                    city=#{city},
                </if>
            </set>
            where id=#{id}
        </update>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    标签也可以使⽤ 替换。

    标签

    对集合进⾏遍历时可以使⽤该标签。

    标签有如下属性:

    • collection:绑定⽅法参数中的集合,如 List,Set,Map或数组对象
    • item:遍历时的每⼀个对象
    • open:语句块开头的字符串
    • close:语句块结束的字符串
    • separator:每次遍历之间间隔的字符串

    比如根据用户 id 删除用户:

    UserMapper 接口中的方法:

    int deleteByIds(List<Integer> ids);
    
    • 1

    UserMapper.xml 中的实现:

        <delete id="deleteByIds">
            delete from userinfo where id in
            <foreach collection="ids" open="(" item="item" close=")" separator=",">
                #{item}
            </foreach>
        </delete>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    好啦! 以上就是对 Mybatis 动态 SQL 的讲解,希望能帮到你 !
    评论区欢迎指正 !

  • 相关阅读:
    Flex and Bison 阅读与学习笔记
    试图颠覆 JavaScript 生态?亲身试用新 JS 运行时 Bun 后,我觉得未来可期
    香港服务器选纯国际线路上网稳定吗?
    layui2.4.3版本下拉框实现多选
    儿童剧本杀兴起,为少儿教育增色还是添乱?
    c++(23)纯虚函数和抽象类、C语言函数指针的语法和意义
    解决checkbox设置数字无法展示的错误
    如何在 Spring Boot 中使用 JPA 和 JPQL 进行自定义查询示例
    openvswitch group hash实现代码分析
    Unity 雷达项目分析(更新ing)
  • 原文地址:https://blog.csdn.net/m0_61832361/article/details/133735848