动态SQL常用标签

本文介绍了MyBatis中`where`、`choose`和`set`标签的使用,通过示例展示了如何根据传入参数动态生成SQL查询和更新语句。`where`标签能避免生成冗余的`WHERE`和`AND`,`choose`标签实现了类似Java的`switch`语句功能,而`set`标签则用于动态更新记录,确保只有非空字段才会被更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、Where

1.1、接口

//查询博客
    List<Blog> queryBlogIF(Map map);

1.2、Mapper

<select id="queryBlogIF" parameterType="map" resultType="Blog">
        select * from blog
        <where>
            <if test="title != null">
                 title = #{title}
            </if>
            <if test="author != null">
                and author = #{author}
            </if>
        </where>
    </select>

1.3、测试

@Test
    public void queryBlogIF(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        HashMap map = new HashMap();
        map.put("title" , "Java如此简单");
        map.put("author" , "秋风");
        List<Blog> blogs = mapper.queryBlogIF(map);
        for (Blog blog : blogs){
            System.out.println(blog);
        }
        sqlSession.close();
    }

1.4、效果

在这里插入图片描述
where 元素只会在子元素返回任何内容的情况下才插入 “WHERE” 子句。而且,若子句的开头为 “AND” 或 “OR”,where 元素也会将它们去除。

二、Choose

2.1、接口

List<Blog> queryBlogChoose(Map map);

2.2、Mapper

<select id="queryBlogChoose" parameterType="map" resultType="blog">
        select * from blog
        <where>
           <choose>
               <when test="title != null">
                   title = #{title}
               </when>
               <when test="author != null">
                   author = #{author}
               </when>
               <otherwise>
                   and views = #{views}
               </otherwise>
           </choose>
        </where>
    </select>

2.3、测试

@Test
    public void queryBlogChoose(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        HashMap map = new HashMap();
        map.put("views" , 9999);
        List<Blog> blogList = mapper.queryBlogChoose(map);
        for (Blog blogs : blogList){
            System.out.println(blogs);
        }
        sqlSession.close();
    }

2.4、效果

在这里插入图片描述

三、Set

3.1、接口

    //更新博客
    int updateBlog(Map map);

3.2、Mapper

<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>

3.3、测试

 @Test
    public void updateBlogSet(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        HashMap map = new HashMap();
        map.put("title" , "Java如此简单2");
        map.put("author" , "孤冬");
        map.put("id","1094fc8af22b425ebca9f138cf550b9d");
        mapper.updateBlog(map);
        sqlSession.close();
    }

3.4、效果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值