天天看點

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. 結果
    Mybatis動态sql之trim、where、set

測試二

接口類:

//根據條件查詢
    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();
    }
}
           

結果:

Mybatis動态sql之trim、where、set

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();
    }
}
           
Mybatis動态sql之trim、where、set

如果子句的開頭為 “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();
    }
}
           

結果:

Mybatis動态sql之trim、where、set

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();
    }
}
           

結果:

Mybatis動态sql之trim、where、set

總結:set元素和where元素都可以與trim元素進行搭配

繼續閱讀