Mybatis(18) ——动态SQL之choose&trim

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

    可见choose、when 、otherwise这3个标签分别对应switch语句中的switch、case、default这3个关键字,它们的用法都是一样的



1.需求

    还是使用17里面的需求:传递title就按照title查询,传递author就按照author查询,什么都不传递就都查询出来


2.需求分析

    我们在数据库中使用SQL语句实现上面的需求就是在传入title就加上title的where子句,传入author就加上author的子句,什么都不传就整表查询

    在17中我们使用的是IF标签来实现需求

    <select id="qureryBlogIf" parameterType="map" resultType="blog">
        select * from blog WHERE 1=1
        <if test="title!=null">
            AND title = #{title}
        </if>
        <if test="author!=null">
            AND author = #{author}
        </if>
    </select>

问题
    ①需要注意的是,我们为了使用WHERE关键字,在WHERE之后写上了一个 1=1 ,这显然是不被允许的,这是原来SQL注入问题的SQL语句,所以在实际开发中我们是不会这么写的,但是不这样使用怎么保证我们可以动态的使用WHERE关键字呢?
    ②除了使用WHERE关键字以外,还有一个问题就是WHERE关键字之后跟的条件的编写格式,第一个条件前面不能加关键字AND,其他条件前面需要加关键字AND,这就使得我们编写的SQL子句格式不一致

解决
    我们可以使用MyBatis为我们提供的where标签解决上面的问题
在这里插入图片描述


3.代码编写

1、使用where标签改写原来使用IF标签查询数据的SQL语句

  • 改写前
    <select id="qureryBlogIf" parameterType="map" resultType="blog">
        select * from blog WHERE 1=1
        <if test="title!=null">
            AND title = #{title}
        </if>
        <if test="author!=null">
            AND author = #{author}
        </if>
    </select>

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
    很显然,直接将关键字where写在SQL语句后面是行不通的,所以我们需要借助mybatis的where标签

  • 改写后
    在这里插入图片描述
    在这里插入图片描述

2、使用where+choose节点实现相同的需求

  • 接口
    package com.thhh.dao;
    
    import com.thhh.pojo.Blog;
    
    import java.util.List;
    import java.util.Map;
    
    public interface BlogMapper {
       //1、添加一条博客信息
       int addBlog(Blog blog);
    
       //2、使用IF标签来查询博客
       List<Blog> qureryBlogIf(Map<String,Object> map);
    
       //3、使用choose标签来查询博客
       List<Blog> qureryBlogChoose(Map<String,Object> map);
    
    }
    
  • mapper.xml
    <select id="qureryBlogChoose" resultType="blog" parameterType="map">
        select * from blog
        <where>
            <choose>
                <when test="title!=null">
                    title = #{title}
                </when>
                <when test="author!=null">
                    AND author = #{author}
                </when>
                <otherwise>
    
                </otherwise>
            </choose>
        </where>
    </select>
    
  • 测试
    • 一个参数
          @Test
          public void testQureryBlogChoose(){
              SqlSession sqlSession = MyBatisUtils.getSqlSession();
              BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
              Map<String,Object> map = new HashMap<String,Object>();
              map.put("author","作者2");
              List<Blog> blogList = mapper.qureryBlogChoose(map);
              for (Blog blog : blogList) {
                  System.out.println(blog);
              }
              sqlSession.close();
          }
      
    • 两个参数
          @Test
          public void testQureryBlogChoose(){
              SqlSession sqlSession = MyBatisUtils.getSqlSession();
              BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
              Map<String,Object> map = new HashMap<String,Object>();
              map.put("author","作者2");
              map.put("title","标题2");
              List<Blog> blogList = mapper.qureryBlogChoose(map);
              for (Blog blog : blogList) {
                  System.out.println(blog);
              }
              sqlSession.close();
          }
      

在这里插入图片描述

在这里插入图片描述
对比一下不使用choose而使用 if 的情况
在这里插入图片描述

3、通过set节点实现数据修改

在这里插入图片描述

  • 接口
    package com.thhh.dao;
    
    import com.thhh.pojo.Blog;
    
    import java.util.List;
    import java.util.Map;
    
    public interface BlogMapper {
        //1、添加一条博客信息
        int addBlog(Blog blog);
    
        //2、使用IF标签来查询博客
        List<Blog> qureryBlogIf(Map<String,Object> map);
    
        //3、使用choose标签来查询博客
        List<Blog> qureryBlogChoose(Map<String,Object> map);
    
        //4、使用SET标签来实现博客修改
        int updateBlog(Map<String,Object> map);
    }
    
  • mapper.xml
        <update id="updateBlog" parameterType="map">
            update blog
            <set>
                <if test="title!=null">
                    title = #{title}
                </if>
                <if test="author!=NULL">
                    author = #{author}
                </if>
            </set>
            where id = #{id}
        </update>
    
    上面这个XML是错误,错误的地方在于没有加","
    <update id="updateBlog" parameterType="map">
        update mybatis.blog
        <set>
            <if test="title!=null">
                title = #{title},
            </if>
            <if test="author!=NULL">
                author = #{author}
            </if>
        </set>
        where id = #{id}
    </update>
    
    因为我们在编写SQL修改语句的时候,要修改的字段都是通过","进行的分割,所以上面的XML中,只要不是最后一个 if 语句,则子句的结尾都应该加上一个","
    在这里插入图片描述
  • 测试
        @Test
        public void testUpdateBlog(){
            SqlSession sqlSession = MyBatisUtils.getSqlSession();
            BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("id","ba5e257c42ff4fedaac27e516ec9dbbd");
            map.put("author","作者2");
            map.put("title","标题2");
            int result = mapper.updateBlog(map);
            System.out.println("数据库受影响行数 = "+result);
            sqlSession.close();
        }
    

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
我们再来理解一次mybatis官方文档对于SET节点功能的解释
在这里插入图片描述

  • set 元素会动态地在行首插入 SET 关键字:即set节点会自动在第一个赋值子句前面加上一个SET关键字,我们可以从上面的例子中看出,我们并没有显式的写一个SET关键字,但是日志打印输出的SQL语句中都是有SET关键字的
  • 并会删掉额外的逗号:这一句是在描述如果在set节点中的 if 节点如果只有一部分成立,且这些子句的最后都带上了",",那么mybatis会自动将最后一个子句的",“去除,以此保证SQL语句的语法正确,否则在执行SQL语句的时候,最后会因为多了一个”,"而报错

4、自定义修剪子句内容的trim节点

    ①在mybatis官方文档上,讲解where和set节点的同时,还描述了一个trim节点

    ②我们小结一下where和set节点的功能,都是在子句的首部为我们添加上一个关键字WHERW或者SET,并且在子句的的指定位置(首部或尾部)删除一些我们不需要的、会导致SQL执行失败的东西,比如set删除最后的",",where删除与where关键字直接连接的and关键字等情况

    ③其实where和set节点都是trim节点的特例,trim的作用就是在子句的首尾两处,添加或删除指定的字符串/符号
在这里插入图片描述

    ④使用语法

<trim prefix|suffix="在首部/尾部 指定添加的 字符串/符号" prefixOverrides|suffixOverrides="在首部/尾部 指定删除的 字符串/符号">

    注意:这4个属性自己看情况使用,没有必须成对使用的规则,想怎么用就怎么用

例子
    使用trim实现set节点的功能

	 <!--xml-->
    <update id="updateBlog" parameterType="map">
        update mybatis.blog

        <trim prefix="set" suffixOverrides="," suffix="" prefixOverrides="">
                <if test="title!=null">
                    title = #{title},
                </if>
                <if test="author!=NULL">
                    author = #{author}
                </if>
            where id = #{id}
        </trim>
    </update>
	//测试
    @Test
    public void testUpdateBlog(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("id","ba5e257c42ff4fedaac27e516ec9dbbd");
        map.put("author","作者7");
        map.put("title","标题7");
        int result = mapper.updateBlog(map);
        System.out.println("数据库受影响行数 = "+result);
        sqlSession.close();
    }

在这里插入图片描述
在这里插入图片描述
    同理,可是通过trim节点实现where节点的功能


小结

  • 所谓的动态SQL,本质上还是原来的SQL语句,只是我们可以在SQL语句层面,执行逻辑代码,实现SQL语句在基础语句上的变化
  • 到现在我们需要掌握的动态SQL的标签有
    • if
    • choose、when、otherwise
    • trim(where、set)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值