1. 程式人生 > >MyBatis-動態SQL的if、choose、when、otherwise、trim、where、set、foreach標籤的使用

MyBatis-動態SQL的if、choose、when、otherwise、trim、where、set、foreach標籤的使用

動態SQL是MyBatis最強大的特性之一。用於實現動態SQL的主要元素如下:
1、if
2、choose、when、otherwise
3、trim、where、set
4、foreach

程式碼示例:


1、if

EmpMapper.xml配置

<select id="getEmpByIf" resultType="Emp" parameterType="Emp">
    select * from emp where 1 = 1
    <if test="job != null and job != ''"
> and job = #{job} </if> <if test="deptno != null "> and deptno = #{deptno} </if> </select>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

測試程式碼

    public void getEmpByIf() {
        SqlSession sqlSession = null;
        Emp emp = new Emp();
        //工作為空字串
emp.setJob(""); //部門編號為10 emp.setDeptno(10); List<Emp> empList = new ArrayList<>(); try { sqlSession = MyBatisUtil.createSqlSession(); //EmpMapper介面中新增對應方法 empList = sqlSession.getMapper(EmpMapper.class).getEmpByIf(emp); } catch
(Exception e) { e.printStackTrace(); } finally { MyBatisUtil.closeSqlSession(sqlSession); } for (Emp emp1 : empList) { logger.debug(emp1.getEmpno() + "-->" + emp1.getEname() + "-->" + emp1.getDeptno()); } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

輸出結果

這裡寫圖片描述

從結果可以看出因為job為空字串所以條件沒拼接,輸出結果正確


2、choose、when、otherwise

類似於Java中的switch case default

EmpMapper.xml配置

<select id="getEmpByChoose" resultType="Emp" parameterType="Emp">
    select * from emp where 1 = 1
    <choose>
        <when test="job != null">
            and job = #{job}
        </when>
        <when test="deptno != null">
            and deptno = #{deptno}
        </when>
        <otherwise>
            and mgr = #{mgr}
        </otherwise>
    </choose>
</select>
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

測試程式碼

    public void getEmpByChoose() {
        SqlSession sqlSession = null;
        Emp emp = new Emp();
        //工作為CLERK
        emp.setJob("CLERK");
        //部門編號為10
        emp.setDeptno(10);
        //經理為7698
        emp.setMgr(7698);
        List<Emp> empList = new ArrayList<>();
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            //EmpMapper介面新增對應方法
            empList = sqlSession.getMapper(EmpMapper.class).getEmpByChoose(emp);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        for (Emp emp1 : empList) {
            logger.debug(emp1.getEmpno() + "-->" + emp1.getEname() + "-->" + emp1.getDeptno());
        }
    }
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

執行結果

這裡寫圖片描述

可以看出,只有一個條件生效,也就是隻執行滿足的條件when,沒有滿足的條件就執行otherwise,表示預設條件。


3、where

EmpMapper.xml配置

<select id="getEmpByWhere" resultType="Emp" parameterType="Emp">
    select * from emp
    <where>
        <if test="job != null and job != ''">
            and job = #{job}
        </if>
        <if test="deptno != null">
            and deptno = #{deptno}
        </if>
    </where>
</select>
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

測試程式碼

    public void getEmpByWhere() {
        SqlSession sqlSession = null;
        Emp emp = new Emp();
        //工作為CLERK
        emp.setJob("CLERK");
        //部門編號為10
        emp.setDeptno(10);
        List<Emp> empList = new ArrayList<>();
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            //EmpMapper介面中新增對應方法
            empList = sqlSession.getMapper(EmpMapper.class).getEmpByWhere(emp);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        for (Emp emp1 : empList) {
            logger.debug(emp1.getEmpno() + "-->" + emp1.getEname() + "-->" + emp1.getDeptno());
        }
    }
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

執行結果

這裡寫圖片描述

從配置的SQL語句及結果來看,where能在第一次滿足新增條件時自動補全where這個單詞,而且如果有and會替換掉,如果滿足條件拼接的第二個語句沒有and,會報錯


4、set

EmpMapper.xml配置

<update id="updateEmpBySet" parameterType="Emp">
    update emp
    <set>
        <if test="ename != null and ename != ''">
            ename = #{ename},
        </if>
        <if test="job != null and job != ''">
            job = #{job},
        </if>
    </set>
    where empno = #{empno}
</update>
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

測試程式碼

    public void updateEmpBySet() {
        SqlSession sqlSession = null;
        //要更新的Emp物件
        Emp emp = new Emp();
        emp.setEmpno(2333);
        emp.setEname("new name");
        emp.setJob("new job");

        try {
            sqlSession = MyBatisUtil.createSqlSession();
            sqlSession.getMapper(EmpMapper.class).updateEmpBySet(emp);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
    }
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

執行結果

這裡寫圖片描述

可以看出,set會在成功拼接的條件前加上SET單詞且最後一個”,”號會被無視掉,但是有可能需要”,”的地方不能省略”,”否則異常。


5、trim

EmpMapper.xml配置

胡亂寫了一段程式碼用來測試(●ˇ∀ˇ●)

這裡寫圖片描述

直接上結果。。。

這裡寫圖片描述

o(^▽^)o可以看到成功匹配掉了開頭的$和末尾的*


6、foreach

foreach標籤用於遍歷生成單個或多個數據,根據傳入的引數型別有倆種配置方式

1、引數為陣列

這裡寫圖片描述

EmpMapper介面新增方法

public List<Emp> getEmpByArray(String[] deptnos);
  
  • 1

測試程式碼

    public void getEmpByArray() {
        SqlSession sqlSession = null;
        List<Emp> empList = new ArrayList<>();
        //引數為陣列
        String[] deptnos = {"10", "20", "30"};
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            empList = sqlSession.getMapper(EmpMapper.class).getEmpByArray(deptnos);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        for (Emp emp : empList) {
            logger.debug(emp.getEmpno() + "-->" + emp.getEname() + "-->" + emp.getDeptno());
        }
    }
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

結果

這裡寫圖片描述

2、引數為List集合

這裡寫圖片描述

EmpMapper介面新增方法

public List<Emp> getEmpByList(List<String> deptnos);
  
  • 1

測試程式碼

    public void getEmpByList() {
        SqlSession sqlSession = null;
        List<Emp> empList = new ArrayList<>();
        //引數為集合
        List<String> list = new ArrayList<>();
        list.add("10");
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            empList = sqlSession.getMapper(EmpMapper.class).getEmpByList(list);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        for (Emp emp : empList) {
            logger.debug(emp.getEmpno() + "-->" + emp.getEname() + "-->" + emp.getDeptno());
        }
    }
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

結果

這裡寫圖片描述

動態SQL是MyBatis最強大的特性之一。用於實現動態SQL的主要元素如下:
1、if
2、choose、when、otherwise
3、trim、where、set
4、foreach