MyBatis传入多个参数的问题

来源:互联网 发布:淘宝信用度怎么看 编辑:程序博客网 时间:2024/06/05 21:06

一、单个参数:

public List<XXBean> getXXBeanList(String xxCode);  <select id="getXXXBeanList" parameterType="java.lang.String" resultType="XXBean">  select t.* from tableName t where t.id= #{id}  </select>  其中方法名和ID一致,#{}中的参数名与方法中的参数名一直, 我这里采用的是XXXBean是采用的短名字,select 后的字段列表要和bean中的属性名一致, 如果不一致的可以用 as 来补充。

二、多参数:

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封装多参数:  

public List<XXXBean> getXXXBeanList(HashMap map);  <select id="getXXXBeanList" parameterType="hashmap" resultType="XXBean">  select 字段... from XXX where id=#{xxId} code = #{xxCode}  </select>  其中hashmap是mybatis自己配置好的直接使用就行。map中key的名字是那个就在#{}使用那个

 四、List封装in:

public List<XXXBean> getXXXBeanList(List<String> list);  <select id="getXXXBeanList" resultType="XXBean">  select 字段... from XXX where id in  <foreach item="item" index="index" collection="list" open="(" separator="," close=")">      #{item}    </foreach>  </select>  foreach 最后的效果是select 字段... from XXX where id in ('1','2','3','4') 
水电费水电费
原创粉丝点击