mybatis中的动态sql拼写

1.关于map做为paramterType,使用的时候直接#{key}便可,key为map中的key。这个时候paramterType能够省略。html

 

Java代码 mybatis中关于传入参数parameterType - 韦成真 - 韦成真——我的博客 
public List<StudentEntity> getStudentListByClassIds_foreach_list(List<String> classIdList);  java

 

动态SQL语句:sql

Xml代码 mybatis中关于传入参数parameterType - 韦成真 - 韦成真——我的博客 
<!-- 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代码 mybatis中关于传入参数parameterType - 韦成真 - 韦成真——我的博客 
@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中不能直接使用<,要使用   &lt;   来代替

<if test="endDate != null and endDate !=''" >        and APPLY_TIME &lt; #{endDate,jdbcType=TIMESTAMP} </if>

相关文章
相关标签/搜索