mybatis传参——parameterType

Mybatis传参

可以使用各种java基本数据类型,包括int,String,Date等

用基本数据类型传参,只能传入一个参数。通过#{参数名}即可获取传入的参数值。

可以使用Java实体类、Map

通过#{java实体类的属性名},#{map的KeyName}即可获取传入的值。

传入一个Collection

可以传递一个List实例或者一个数组作为参数对象传给MyBatis,当你这么做的时候,MyBatis会自动将它包装在一个Map中,如果传入的是List,将使用"list"作为键,如果传入的是数组,将以"array"作为键。

要迭代一个集合,使用foreach,它可以指定一个集合,声明集合项和索引变量,也可以指定开放和关闭的字符串、分隔符。

注意:当想要传入collection时,并不能直接传入。因为Mybatis生成SQL语句遍历list时,是需要用到get()方法的,而Collection中没有get方法,参见MyBatis官方文档中“动态SQL”。

对象类型中的集合属性

List<User> selectByExample(UserExample example);
<where>
<foreach collection="oredCriteria" item="criteria" separator="or" >
<if test="criteria.valid" >

此时collection="oredCriteria"会找到入参example这个非集合对象的oredCriteria属性,此属性是一个集合

#示例

参数为list


public List<StudentEntity> getStudentListByClassIds_foreach_list(List<String> classIds);
<select id="getStudentByClassIds_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>

collection="list"这是默认写法,入参为数组Integer[] idarr,则用collection="array"。MyBatis会默认找到那个list属性。

@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());  
    }  
}  

JAVA实体类型参数示例:

<select id="selectTeacher" parameterType="com.myapp.domain.Teacher" resultType="com.myapp.domain.Teacher">  
    select * from Teacher where c_id=#{id}  
</select>  

Map参数示例:


<select id="findTeacherByPage"resultMap="supervisorResultMap" parameterType="java.util.Map">
select * from teacher where title like #{title}           
        order by ${sort} ${dir} limit #{start},#{limit} 
</select> 
public List<Teacher> findTeacherByPage(Map<String, Object> map); 
Map<String, Object> params = new HashMap<String, Object>(); 
//以name字段升序排序,
params.put("sort", "name"); 
params.put("dir", "asc");
//查询结果从第0条开始,查询2条记录 
params.put("start", 0);  
params.put("limit", 2);  
//查询职称为教授或副教授的教师  
params.put("title", "%教授"); 

此时入参map的key相当于一个object的属性名,value相当于属性值

传入多个参数

使用@Param注解。

示例

public List<Teacher> selectTeacher(@Param(value="id") String id,@Param(value="sex") String sex);  
<select id="selectTeacher"  resultType="com.myapp.domain.Teacher">  
    select * from Teacher where c_id=#{id} and sex=#{sex}  
</select>  
List<Teacher> tList = teacherMapper.selectTeacher("2","男");    

转载于:https://my.oschina.net/u/3672057/blog/1547221

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值