mybatis中foreach使用

一、foreach 属性使用

   #{mchntCd}

  1. item:  集合中元素迭代时的别名,该参数为必选,通过别名进行取值
  2. index:在list和数组中,index是元素的序号,在map中,index是元素的key,非必填
  3. open: foreach代码的开始符号,一般是(和close=”)”合用。常用在in(),values()时。非必填
  4. separator:元素之间的分隔符,例如在in()的时候,separator=”,”会自动在元素中间用“,“隔开,避免手动输入逗号导致sql错误,如in(1,2,)这样。非必填
  5. close: foreach代码的关闭符号,一般是)和open=”(“合用。常用在in(),values()时。非必填
  6. collection: 要做foreach的对象,作为入参
    1. 传入是集合,也就是接口里面用的  List nameList    那么 使用 collection = “list”
    2. 传入的是数组,接口里面 String[] namestrs ,那么 使用 collection = “array”
    3. 如果传入的是一个实体bean,实体bean里面有一个属性为 list ids 那么collection = “ids ”,如果实体bean中对应ids是一个对象,ids 里面有一个属性为 List usernames,那么collection = “ids.usernames”

二、代码使用

        1、实体类 list  mchntCds
 mapper接口  —》 list方式
<span class="hljs-type">int</span> <span class="hljs-title function_">queryDiscDerateCount</span>
<span class="hljs-params">(List<String> mchntCds)</span>;
mapper映射xml
  1. <select id=“queryDiscDerateCount” resultType=“Integer”>
  2. select count (*) from t_mchnt_disc_config where mchnt_cd in
  3. <foreach collection=“list” index=“index” item=“mchntCd” open=“(“ close=“)” separator=“,”>
  4. #{mchntCd}
 
   2、实体类 list  userlist,实体类中有 list 属性
实体类:
  1. @Data
  2. public class MchntDiscDerateDto {
  3. private String mchntCd = “”;
  4. }
mapper接口
List<TMchntDerateInfoDto> <span class="hljs-title function_">getDiscDerateList</span>
<span class="hljs-params">(List<MchntDiscDerateDto> discDto)</span>;
  mapper映射xml:
  1. <select id=“getDiscDerateList” parameterType=“MchntDiscDerateDto” resultType=“TMchntDerateInfoDto”>
  2. select
  3. t_mchnt_disc_derate_config
  4. where
  5. mchnt_cd in
  6. <foreach collection=“list” index=“index” item=“user” open=“(“ close=“)” separator=“,”>
  7. #{user.mchntCd}
 
3、数组  String[] params      用 array
mapper 接口  
<span class="hljs-type">int</span> <span class="hljs-title function_">queryDiscDerateCount</span>
<span class="hljs-params">(String[] mchntCds)</span>;
  mapper映射xml:
  1. <select id=“queryDiscDerateCount” resultType=“Integer”>
  2. select count (*) from t_mchnt_disc_derate_config where mchnt_cd in
  3. <foreach collection=“array” index=“index” item=“mchntCd” open=“(“ close=“)” separator=“,”>
  4. #{mchntCd}
 
4、传入的参数是实体类,并且实体中包含数组和集合
实体类:
  1. @Data
  2. public class UserVo {
  3. private Long id;
  4. private Long supplierId;
  5. private Long[] ids;
  6. private List clientIdList;
  7. }
mapper接口
List<UserVo> <span class="hljs-title function_">queryList</span>
<span class="hljs-params">(UserVo select)</span>;
mapper映射文件xml
  1. <select id=“queryList” resultType=“UserVo” parameterType=“UserVo”>
  2. select *
  3. from bms_bills_memo
  4. and id in
  5. <foreach collection=“ids” open=“(“ close=“)” item=“id” separator=“,”>
  6. #{id}
  7. and
  8. client_id in
  9. <foreach collection=“clientIdList” separator=“,” item=“detail” open=“(“ close=“)” >
  10. #{detail}
 
5、传入多个list或者array,不使用实体进行封装。用注解@Params, collection使用到Param中定义的别名
mapper接口
List<UserVo> <span class="hljs-title function_">queryList</span>
<span class="hljs-params">(<span class="hljs-meta">@Param("idArray")</span> Long[] array, <span class="hljs-meta">@Param("clientIdList")</span> List<Long> list)</span>;
mapper映射文件xml
  1. <select id=“queryList” resultType=“UserVo”>
  2. select *
  3. from t_user_inf
  4. and id in
  5. <foreach collection=“idArray” open=“(“ close=“)” item=“id” separator=“,”>
  6. #{id}
  7. and
  8. client_id in
  9. <foreach collection=“clientIdList” separator=“,” item=“detail” open=“(“ close=“)” >
  10. #{detail}

 

6、map参数
当我们传入的参数为 Map的时候,那么能不能正常使用 foreach呢,答案是肯定的,因为对象也是类似于map结构啊
  1. /**
  2. * map 获取数据
  3. * @param map
  4. * @return
  5. */
  6. List getUserByIds (Map map);
  1. // xml
  2. <select id=“getUserByIds” resultMap=“BaseResultMap”>
  3. select
  4. <include refid=“Base_Column_List” />
  5. from t_sys_user
  6. where status = #{status}
  7. and id in
  8. <foreach collection=“ids” item=“id” open=“(“ close=“)” separator=“,”>
  9. #{id}
  10. </foreach>
  11. </select>

实际调用:

  1. @Test
  2. public void testMapUsers() {
  3. Map params = new HashMap();
  4. params.put(“status”, “1”);
  5. params.put(“ids”, Arrays.asList(1,2,3,4,5,6,7,8));
  6. List<SysUser> all = sysUserDao.getUserByIds(params);
  7. try {
  8. System.out.println(new JsonMapper().writeValueAsString(all));
  9. } catch (JsonProcessingException e) {
  10. e.printStackTrace();
  11. }
  12. }

调用结果:

三、总结

1、mapper 接口中添加 @Param注解的场合,list,array将会失效;

2、使用了 @Param注解 collection的内容需要使用Param中的别名来指定你需要用到的list或者array

发表回复