• Mybatis 10


    10.1 动态 SQL 常用标签


    除了 IF,还有 choose when otherwise trim where set 等 常用标签。


    10.1.1 <where> 标签 的牛逼之处

    我们在 写 <if> 标签的时候的,肯定会碰到如下的 事故。

        <select id="queryBlogIF" parameterType="map" resultType="top.muquanyu.pojo.Blog">
            select * from test.blog where
        <if test="title != null">
            and title = #{title}
        </if>
        <if test="author != null">
            and author = #{author}
        </if>
        </select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这里插入图片描述
    也就是 说 可能会 出现 where 后面 进行 SQL 拼接时 的 一些 小问题。拼接 错喽 ~

    那怎么 去解决呢? 这里 有一个 标签 叫 <where>

    <select id="queryBlogIF" parameterType="map" resultType="top.muquanyu.pojo.Blog">
            select * from test.blog
            <where>
            <if test="title != null">
                and title = #{title}
            </if>
            <if test="author != null">
                and author = #{author}
            </if>
            </where>
        </select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述


    10.1.2 <choose> 选择标签

    当你 只想 满足一个条件后,就不再去 判断其它条件了,类似于选择的作用。我们 就可以 写 choose 标签。它类似于 Java 里面的 switch。而 when 等价于 case、otherwise 等价于 default。

    在这里插入图片描述

    在这里插入图片描述


    10.1.3 <set> 标签

    在 更新 数据的时候,我们 update set 后面 要 拼接 多个 字段,然后 会 带有 逗号。这个 时候 往往 可能在 mybatis 里面 就出错了。那么 <set> 标签 就是 为了 避免 这样的问题的。

        <update id="updateBlog" parameterType="map">
            update test.blog
            <set>
                <if test="title != null">
                    title = #{title},
                </if>
                <if test="author != null">
                    author = #{author}
                </if>
            </set>
            where id = #{id}
        </update>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    @Test
        public void updateTest(){
            SqlSession sqlSession = MybatisUtils.getSqlSession();
            BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
            HashMap<String, Object> map = new HashMap<>();
            map.put("id","dc804c0110814455bdc1a6b3f7a9f8fa");
            map.put("author","lwc");
            int i = mapper.updateBlog(map);
            if(i > 0){
                System.out.println("update 成功!");
            }else{
                System.out.println("update 失败!");
            }
            sqlSession.commit(); // 事务提交
            sqlSession.close();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在这里插入图片描述


    10.2.1 <trim> 标签 可以 自定义一个 规则

    trim 格式: <trim prefix="" suffix="" suffixOverrides="" prefixOverrides=""></trim>

    • prefix:在trim标签内sql语句加上前缀。 比如说 你要拼接 where xxxx 那么这个 prefix = "where"

    • suffix:在trim标签内sql语句加上后缀。 比如说 你拼接完事后 要有个 分号,那么 你就写 suffix = ";"

    • suffixOverrides:指定去除多余的后缀内容。 如:suffixOverrides=",",去除trim标签内sql语句多余的后缀","。

    • prefixOverrides:指定去除多余的前缀内容 如:我们 where 后面 接的 and || or 有时候 就会 多出来,那么 就得 prefixOverrides = "and || or" 这里的 多余 前缀 是从 prefix 之后 开始计算的。

    <select id="queryBlogIF" parameterType="map" resultType="top.muquanyu.pojo.Blog">
            select * from test.blog
            <trim prefix="where" prefixOverrides="and || or">
                <choose>
                    <when  test="title != null">
                        and title = #{title}
                    </when>
                    <when test = "author != null">
                        and author = #{author}
                    </when>
                    <otherwise>
                        and views = #{views}
                    </otherwise>
                </choose>
            </trim>
        </select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在这里插入图片描述

    Mybatis 29 道 练习题目

  • 相关阅读:
    Django 管网项目 三
    单字段纵向分栏
    状态空间模型(SSM)
    翻阅必备----Java窗口组件,容器,布局,监听,事件 API大全
    【Ubuntu】Systemctl 管理 MinIO 服务器的启动和停止
    Netty编解码机制(二)
    【C++】命名空间
    .Net分表分库动态化处理
    【R语言】生存分析模型
    【JavaScript】解决 JavaScript 语言报错:Uncaught TypeError: XYZ is not a function
  • 原文地址:https://blog.csdn.net/qq_52606908/article/details/125528555