Mybatis:传输多个参数

在XML文件中拼SQL语句时,用到了多个参数,开始为了省事。

public List<XXXBean> getXXXBeanList(String xxId, String xxCode);  

<select id="getXXXBeanList" resultType="XXBean">

  select t.* from tableName where id = #{0} and name = #{1}  

</select>  

由于是多参数那么就不能使用parameterType, 改用#{index}索引从0开始。

按道理来说应该是可以的,但不知道为啥,我这边还是报错,懒得找原因了,就直接把参数类型换成了Map,不用不知道,用了才知道,这个多好使。以后就用你了。

Map<String,Object> params = new HashMap<String,Object>();
params.put("params1",id);
params.put("params2",name); 

<select id="getXXXBeanList" parameterType="map" resultType="XXBean">

  select ... from XXX where id=#{params1} name = #{params2}  

</select> 

突然又发现一个方法:

在接口方法里面加上注解@Param,跟Map有点像

public List<XXXBean> getXXXBeanList(@Param("xxId")String xxId,@Param("xxCode") String xxCode);  
<select id="getXXXBeanList" parameterType="map" resultType="XXBean">

  select ... from XXX where id=#{xxId} name = #{xxCode}  

</select> 

猜你喜欢

转载自blog.csdn.net/wml00000/article/details/83242519