EmployeeMapperDynamicSql.java
package com.gong.mybatis.mapper; import java.util.List; import java.util.Map; import org.apache.ibatis.annotations.MapKey; import org.apache.ibatis.annotations.Param; import com.gong.mybatis.bean.Employee; public interface EmployeeMapperDynamicSql { public List<Employee> getEmpByConditionIf(Employee employee); public List<Employee> getEmpByForeach(@Param("ids") List<Integer> ids); }
EmployeeMapperDynamicSql.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.gong.mybatis.mapper.EmployeeMapperDynamicSql"> <!-- 查詢,要查那個就帶上那個條件 --> <select id="getEmpByConditionIf" resultType="com.gong.mybatis.bean.Employee"> select * from tbl_employee <where> <choose> <when test="id!=null"> id=#{id} </when> <when test="lastName!=null"> last_name like #{lastName} </when> <when test="email!=null"> email=#{email} </when> <otherwise> </otherwise> </choose> </where> </select> <select id="getEmpByForeach" resultType="com.gong.mybatis.bean.Employee"> select * from tbl_employee where id in <foreach collection="ids" item="item_id" separator="," open="(" close=")"> #{item_id} </foreach> </select> </mapper>
說明:
- item:集合中元素迭代時的別名,該參數為必選。
- index:在list和數組中,index是元素的序號,在map中,index是元素的key,item是元素的value,該參數可選
- open:foreach代碼的開始符號,一般是(和close=")"合用。常用在in(),values()時。該參數可選
- separator:元素之間的分隔符,例如在in()的時候,separator=","會自動在元素中間用“,“隔開,避免手動輸入逗號導致sql錯誤,如in(1,2,)這樣。該參數可選。
- close: foreach代碼的關閉符號,一般是)和open="("合用。常用在in(),values()時。該參數可選。
- collection: 要做foreach的對象,作為入參時,List對象默認用"list"代替作為鍵,數組對象有"array"代替作為鍵,Map對象沒有默認的鍵。當然在作為入參時可以使用@Param("keyName")來設置鍵,設置keyName后,list,array將會失效。 除了入參這種情況外,還有一種作為參數對象的某個字段的時候。舉個例子:如果User有屬性List ids。入參是User對象,那么這個collection = "ids".如果User有屬性Ids ids;其中Ids是個對象,Ids有個屬性List id;入參是User對象,那么collection = "ids.id"
也就是說整個sql語句是這樣的:select * from tbl_employee where id in (1,2,3)
注意的是我們在接口中定義了傳入參數的名稱,因此在collection中可以傳入該名稱。
進行測試:
package com.gong.mybatis.test; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; 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; import com.gong.mybatis.bean.Employee; import com.gong.mybatis.mapper.EmployeeMapperDynamicSql; public class TestMybatis3 { public SqlSessionFactory getSqlSessionFactory() throws IOException { String resource = "mybatis-config.xml"; InputStream is = Resources.getResourceAsStream(resource); return new SqlSessionFactoryBuilder().build(is); } @Test public void test() throws IOException { SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); SqlSession openSession = sqlSessionFactory.openSession(); try { EmployeeMapperDynamicSql mapper = openSession.getMapper(EmployeeMapperDynamicSql.class); List<Employee> es = mapper.getEmpByForeach(Arrays.asList(1,2,3)); for(Employee e:es) { System.out.println(e); } openSession.commit(); } finally { openSession.close(); } } }
最終結果:
DEBUG 01-21 15:31:16,176 ==> Preparing: select * from tbl_employee where id in ( ? , ? , ? ) (BaseJdbcLogger.java:145) DEBUG 01-21 15:31:16,241 ==> Parameters: 1(Integer), 2(Integer), 3(Integer) (BaseJdbcLogger.java:145) DEBUG 01-21 15:31:16,335 <== Total: 3 (BaseJdbcLogger.java:145) Employee [id=1, lastName=dema, gender=1, email=dema@qq.com, dept=null] Employee [id=2, lastName=jack, gender=1, email=675544321@qq.com, dept=null] Employee [id=3, lastName=小紅, gender=0, email=xiaohong@qq.com, dept=null]