单个参数
可以接收基本类型,对象类型,集合类型,都可以接收,mybatis可以直接使用这个参数,不用经过任何处理,同时#{}这里面的名称可以随便写。
<!--根据cust_id查询客户-->
<select id="queryCustomerById" parameterType="Int" resultType="com.ctbu.domain.Customer">
SELECT * FROM `customer` WHERE cust_id = #{cust_id} //看,就是这里的cust_id名称其实是可以随便写的,不必和传进来的参数名称保持一致
</select>
多个参数(原理是重新封装成了map,所以取值应该根据key来取)
1.mybatis提供了两种名称的key,你可以再以下这里使用 arg0、arg1...的方式来取 或者使用param1、param2....的方式来取,
<select id="queryCustomerByLike" resultType="com.ctbu.domain.Customer">
select * from `customer` where cust_name like #{param1} and cust_id = #{param2}
</select>
上面这个查询就是条件中有多个参数的,使用的param1.2.3这种形式来取的
2.就是想要按照自己取得名字来传参
这也是可以的,只需要在使用方法的形参的前面添加一个注解即可,就像下面这个样子
public Customer queryCustomerByLike(@Param("name") String cust_name,@Param("id") Integer cust_id);
现在看映射配置文件中就是使用你命名的参数名称来传参的
<select id="queryCustomerByLike" resultType="com.ctbu.domain.Customer">
select * from `customer` where cust_name like #{name} and cust_id = #{id}
</select>
注意点:如果使用了这种形式后,arg1.2...这种形式就不能再使用了,但是param1.2...这种形式仍然可以使用