有時候要查詢條件是多條件的,尤其是使用mybatis的時候如何創建sql語句呢?
這里mybatis有自己的辦法,如下:
案例:通過傳入map,根據map里面的數據來查詢
mapper配置如下:
<select id="select04" parameterType="Emp1" resultType="Emp1"> select * from emp <where> <if test="empno != 0"> and empno = #{empno} </if> <if test="ename != null"> and ename = #{ename} </if> <if test = "deptno != null "> and deptno = #{deptno} </if> </where> </select>
測試類如下:
package com.yc.mybatis; import java.io.IOException; import java.io.InputStream; import java.util.List; import java.util.Map; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Test; public class TestTest01 { InputStream is = null; SqlSessionFactory factory = null; SqlSession session = null; { try { is = Resources.getResourceAsStream("mybatis-config.xml"); factory = new SqlSessionFactoryBuilder().build(is); session = factory.openSession(); } catch (IOException e) { e.printStackTrace(); } } @Test public void TTest04(){ Emp1 emp = new Emp1(); emp.setDeptno(20); List<Emp1> list = session.selectList("TTest.select04", emp);//TTest為mapper的命名空間 for(Emp1 e : list){ System.out.println(e); } } }
實體類如下:

package com.yc.mybatis; public class Emp1 { private int empno; private int deptno; private String ename; @Override public String toString() { return "Emp1 [empno=" + empno + ", deptno=" + deptno + ", ename=" + ename + "]"; } public int getEmpno() { return empno; } public void setEmpno(int empno) { this.empno = empno; } public int getDeptno() { return deptno; } public void setDeptno(int deptno) { this.deptno = deptno; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + deptno; result = prime * result + empno; result = prime * result + ((ename == null) ? 0 : ename.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Emp1 other = (Emp1) obj; if (deptno != other.deptno) return false; if (empno != other.empno) return false; if (ename == null) { if (other.ename != null) return false; } else if (!ename.equals(other.ename)) return false; return true; } public Emp1(int empno, int deptno, String ename) { super(); this.empno = empno; this.deptno = deptno; this.ename = ename; } public Emp1() { super(); } }
結果截圖如下:
多條件查詢到此結束,但是相關的思想卻非常重要,比如有多條件更新,多個值的賦值再插入數據等等,這里我就將配置項發一下,相關的測試類和實體類就不一 一列舉了。
多數據更新:
<update id="update01" parameterType="Emp1"> update emp <set> <if test="ename != null "> ename = #{ename}, </if> <if test = "deptno != 0"> deptno = #{deptno}, </if> </set> where empno = #{empno} </update>
多條件刪除:
<delete id="delete01" parameterType="Emp"> delete emp <where> <if test="empno != 0"> and empno = #{empno} </if> <if test="ename != null"> and ename = #{ename} </if> <if test="deptno != 0"> and deptno = #{deptno} </if> </where> </delete>
注意:關於添加、刪除和修改必須要手動提交一次才行,要session.commit(),因為mybatis不會自動幫你提交。
基本的mybatis到此。