mybatis中foreach使用

一、foreach 属性使用

<foreach collection="list" index="index" item="mchntCd" open="(" close=")" separator=",">
   #{mchntCd}
</foreach>
  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<String> nameList    那么 使用 collection = “list”
    2. 传入的是数组,接口里面 String[] namestrs ,那么 使用 collection = “array”
    3. 如果传入的是一个实体bean,实体bean里面有一个属性为 list<String> ids 那么collection = “ids ”,如果实体bean中对应ids是一个对象,ids 里面有一个属性为 List<Stirng> usernames,那么collection = “ids.usernames”

二、代码使用

        1、实体类 list<String>  mchntCds

 mapper接口  ---》 list方式

int queryDiscDerateCount(List<String> mchntCds);

mapper映射xml:

<select id="queryDiscDerateCount"  resultType="Integer">
   select count(*) from t_mchnt_disc_config  where mchnt_cd in
   <foreach collection="list" index="index" item="mchntCd" open="(" close=")"  separator=",">
      #{mchntCd}
   </foreach>
</select>

   2、实体类 list<User>  userlist,实体类中有 list 属性

实体类:

@Data
public class MchntDiscDerateDto {

   private String mchntCd = "";
}
mapper接口
List<TMchntDerateInfoDto> getDiscDerateList(List<MchntDiscDerateDto> discDto);

mapper映射xml:

<select id="getDiscDerateList" parameterType="MchntDiscDerateDto" resultType="TMchntDerateInfoDto">
   select
    t_mchnt_disc_derate_config
      where
         mchnt_cd in
         <foreach collection="list" index="index" item="user" open="(" close=")" separator=",">
            #{user.mchntCd}
         </foreach>
</select>

3、数组  String[] params      用 array 

mapper 接口

int queryDiscDerateCount(String[] mchntCds);

mapper映射xml:

<select id="queryDiscDerateCount"  resultType="Integer">
   select count(*) from t_mchnt_disc_derate_config where mchnt_cd in
   <foreach collection="array" index="index" item="mchntCd" open="(" close=")" separator=",">
      #{mchntCd}
   </foreach>
</select>

4、传入的参数是实体类,并且实体中包含数组和集合

实体类:

@Data
public class UserVo {
 
    private Long id;
 
    private Long supplierId;
 
    private Long[] ids;
 
    private List<Long> clientIdList;
 
}

mapper接口

List<UserVo> queryList(UserVo select);

mapper映射文件xml

    <select id="queryList" resultType="UserVo" parameterType="UserVo">
        select *
        from bms_bills_memo
        <where>
        and id in
        <foreach collection="ids" open="(" close=")" item="id" separator=",">
            #{id}
        </foreach>
        and
        client_id in
        <foreach collection="clientIdList" separator="," item="detail" open="(" close=")" >
            #{detail}
        </foreach>
        </where>
    </select>

5、传入多个list或者array,不使用实体进行封装。用注解@Params, collection使用到Param中定义的别名

mapper接口

List<UserVo> queryList(@Param("idArray") Long[] array, @Param("clientIdList") List<Long> list);

mapper映射文件xml

    <select id="queryList" resultType="UserVo">
        select *
        from t_user_inf
        <where>
            and id in
            <foreach collection="idArray" open="(" close=")" item="id" separator=",">
                #{id}
            </foreach>
            and
            client_id in
            <foreach collection="clientIdList" separator="," item="detail" open="(" close=")" >
                #{detail}
            </foreach>
        </where>
    </select>

三、总结

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

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

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值