Mybatis动态sql之trim、where、set

trim元素

trim元素包含prefix(添加前缀)、prefixOverrides(自动忽略前缀)、suffix(添加后缀)、suffixOverrides(自动忽略后缀)

需求:更新一条数据

  1. 接口类:
//更新一条数据
    int updateBlog(Map map);
  1. Mapper.xml文件
<update id="updateBlog" parameterType="map">
        update blog
        <trim prefix="set" suffixOverrides="," >
            title=#{title},author=#{author},
        </trim>
        where id=#{id}
    </update>
  1. 测试类
public class Test {
    
    
    @org.junit.Test
    public void test(){
    
    
        //通过封装好的工具类获取SqlSession会话
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        //通过接口类型class获取接口对象实例(动态代理)
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        //执行接口中的方法
        HashMap<String,Object> map=new HashMap<String, Object>();
        map.put("title","Java如此简单1");
        map.put("id","2ca4530ae2b74f148ac8e19a2aa0c376");
        map.put("author","chenhui1");
        mapper.updateBlog(map);

        sqlSession.commit();
        //关闭SqlSession
        sqlSession.close();
    }
}
  1. 结果
    在这里插入图片描述

测试二
接口类:

 //根据条件查询
    List<Blog> query(Map map);

mapper.xml文件

<select id="query" parameterType="blog" resultType="map">
    select * from blog
    <trim prefix="where" prefixOverrides="and | or">
       <if test="title!=null">
           and title=#{title}
       </if>
       <if test="author!=null">
           and author=#{author}
       </if>
    </trim>
</select>

测试类:

public class Test {
    
    
    @org.junit.Test
    public void test(){
    
    
        //通过封装好的工具类获取SqlSession会话
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        //通过接口类型class获取接口对象实例(动态代理)
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        //执行接口中的方法
        HashMap<String,Object> map=new HashMap<String, Object>();
        map.put("title","Java如此简单1");
        map.put("author","chenhui1");
        mapper.query(map);

        sqlSession.commit();
        //关闭SqlSession
        sqlSession.close();
    }
}

结果:

在这里插入图片描述

where元素

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

Mapper.xml

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

    </where>

测试类:

如果子句条件未返回查询结果,则不会执行where元素以下内容

public class Test {
    
    
    @org.junit.Test
    public void test(){
    
    
        //通过封装好的工具类获取SqlSession会话
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        //通过接口类型class获取接口对象实例(动态代理)
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        //执行接口中的方法
        HashMap<String,Object> map=new HashMap<String, Object>();
        //map.put("title","Java如此简单1");

        mapper.query(map);

        sqlSession.commit();
        //关闭SqlSession
        sqlSession.close();
    }
}

在这里插入图片描述
如果子句的开头为 “AND” 或 “OR”,where 元素也会将它们去除。

测试类:

public class Test {
    
    
    @org.junit.Test
    public void test(){
    
    
        //通过封装好的工具类获取SqlSession会话
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        //通过接口类型class获取接口对象实例(动态代理)
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        //执行接口中的方法
        HashMap<String,Object> map=new HashMap<String, Object>();
        map.put("title","Java如此简单1");

        mapper.query(map);

        sqlSession.commit();
        //关闭SqlSession
        sqlSession.close();
    }
}

结果:
在这里插入图片描述

set元素

set 元素会动态地在行首插入 SET 关键字,并会删掉额外的逗号(这些逗号是在使用条件语句给列赋值时引入的)

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>

测试类:

public class Test {
    
    
    @org.junit.Test
    public void test(){
    
    
        //通过封装好的工具类获取SqlSession会话
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        //通过接口类型class获取接口对象实例(动态代理)
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        //执行接口中的方法
        HashMap<String,Object> map=new HashMap<String, Object>();
        map.put("title","Java如此简单2");
        map.put("author","chenhui2");
        map.put("id","2ca4530ae2b74f148ac8e19a2aa0c376");
        mapper.updateBlog(map);

        sqlSession.commit();
        //关闭SqlSession
        sqlSession.close();
    }
}

结果:
在这里插入图片描述

总结:set元素和where元素都可以与trim元素进行搭配

猜你喜欢

转载自blog.csdn.net/weixin_45608165/article/details/113748913