一般情况下,我们用ibatis都是传入一个参数,这个参数可以为一个类,一个字符串,一个整型等等,例如:
<select id="selectpw" parameterClass="String" resultClass="String"> select pwd from userinfo where userid=#userid# </select>
在方法体里可以用:password = (String)sqlMapClient.queryForObject("selectpw", userid)得到password。
但是有时候我们却想从前面传入两个或多个参数,这时候怎么办呢?
一.用Map,在方法体里,我们把多个参数存放在map里,然后在前面获得它
Map map = new HashMap(); map.put("userid", userid); map.put("name", name); cardList = (List)sqlMapClient.queryForList("findByName", map);
在SQL语句中:
<select id="findByName" parameterType="java.util.Map" resultClass="Card"> select * from cardinfo where userid=#userid# and name like '$name$' </select>
这样就可以将多个参数传过去了。
二.用ibatis中的@Param
DAO层的方法:
public int deleteDataByCreateTime(@Param(value = "endTimeStr") String endTimeStr, @Param(value = "maxBatchNum") int maxBatchNum);
对应的Mapper.xml:
<delete id="deleteDataByCreateTime"> <![CDATA[ delete from table_name where create_time < #{endTimeStr,jdbcType=VARCHAR} limit #{maxBatchNum,jdbcType=INTEGER} ]]> </delete>
个人觉得这种方法比较好,能让开发者看到dao层方法就知道该传什么样的参数,比较直观,个人推荐用此种方案。
扫描二维码关注公众号,回复:
622267 查看本文章