1.關於map做爲paramterType,使用的時候直接#{key}便可,key爲map中的key。這個時候paramterType能夠省略。html
Java代碼
public List<StudentEntity> getStudentListByClassIds_foreach_list(List<String> classIdList); java
動態SQL語句:sql
Xml代碼
<!-- 7.2 foreach(循環List<String>參數) - 做爲where中in的條件 -->
<select id="getStudentListByClassIds_foreach_list" resultMap="resultMap_studentEntity">
SELECT ST.STUDENT_ID,
ST.STUDENT_NAME,
ST.STUDENT_SEX,
ST.STUDENT_BIRTHDAY,
ST.STUDENT_PHOTO,
ST.CLASS_ID,
ST.PLACE_ID
FROM STUDENT_TBL ST
WHERE ST.CLASS_ID IN
<foreach collection="list" item="classIdList" open="(" separator="," close=")">
#{classIdList}
</foreach>
</select>
mybatis
測試代碼,查詢學生中,在2000000一、20000002這兩個班級的學生:app
Java代碼
@Test
public void test7_2_foreach() {
ArrayList<String> classIdList = new ArrayList<String>();
classIdList.add("20000001");
classIdList.add("20000002");
List<StudentEntity> list = this.dynamicSqlMapper.getStudentListByClassIds_foreach_list(classIdList);
for (StudentEntity e : list) {
System.out.println(e.toString());
}
} dom
2.關於集合做爲ParamterType,配置文件中的parameterType是能夠不配置的,mybatis會自動傳入的。當您想傳入collection時,並不能直接傳入collection對象,要將其先轉換爲list,而後才能傳入。由於mybatis生成SQL語句遍歷list時是須要用到get()方法的,而這個方法只在List中才有,Collection裏是沒有的。測試
java代碼
Map<String,String> map=new HasMap<String,String>();
map.put("id","2");
map.put("sex","男");
List<Teacher> tList = teacherMapper.selectTeacher(map);
for (Teacher entityTemp : tList) {
System.out.println(entityTemp.toString()); }
另外MyBatis還提供了一個使用註解來參入多個參數的方式。這種方式須要在接口的參數上添加@Param註解this
示例:.net
接口方法htm
[java] view plaincopy
public List<Teacher> selectTeacher(@Param(value="id") String id,@Param(value="sex") String sex);
XML文件
[html] view plaincopy
<select id="selectTeacher" resultType="com.myapp.domain.Teacher">
select * from Teacher where c_id=#{id} and sex=#{sex}
</select>
測試代碼
[java] view plaincopy
List<Teacher> tList = teacherMapper.selectTeacher("2","男");
for (Teacher entityTemp : tList) {
System.out.println(entityTemp.toString());
3.在mybatis的動態sql中不能直接使用<,要使用 < 來代替
<if test="endDate != null and endDate !=''" > and APPLY_TIME < #{endDate,jdbcType=TIMESTAMP} </if>