mybatis中的mapper.xml如何使用foreach标签呢?
下文笔者讲述mybatis中如何在xml中配置foreach标签的方法及示例分享,如下所示
foreach简介
foreach标签用于处理 集合 数组 map的参数 =============================== foreach中常见的属性: <foreach collection="list" index="index" item="user" open="(" close=")" separator=","> #{user} </foreach> item: 集合中元素迭代时的别名,该参数为必选,通过别名进行取值 index:在list和数组中,index是元素的序号,在map中,index是元素的key,非必填 open: foreach代码的开始符号,一般是(和close=")"合用。常用在in(),values()时 非必填 separator:元素之间的分隔符,例如在in()的时候,separator="," 会自动在元素中间用“,“隔开 避免手动输入逗号导致sql错误,如in(1,2,)这样。非必填 close: foreach代码的关闭符号,一般是)和open="("合用。常用在in(),values()时。非必填 collection: 要做foreach的对象,作为入参 传入是集合,也就是接口里面用的 List<String> nameList 那么 使用 collection = “list” 传入的是数组,接口里面 String[] namestrs ,那么 使用 collection = “array” 如果传入的是一个实体bean,实体bean里面有一个属性为 list<String> ids 那么collection = “ids ” 当实体bean中对应ids是一个对象,ids 里面有一个属性为 List<Stirng> usernames 则collection = “ids.usernames”
foreach的使用示例
int queryUserCount(List<String> ids); mapper映射xml: <select id="queryDiscDerateCount" resultType="Integer"> select count(*) from t_mchnt_disc_config where mchnt_cd in <foreach collection="list" index="index" item="id" open="(" close=")" separator=","> #{id} </foreach> </select> 实体类 list<User> userlist,实体类中有 list 属性 实体类: @Data public class UserDto { private String userId = ""; } mapper接口 List<UserDto> getDiscDerateList(List<UserDto> discDto); mapper映射xml: <select id="getDiscDerateList" parameterType="UserDto" resultType="UserDto"> select * users where id in <foreach collection="list" index="index" item="user" open="(" close=")" separator=","> #{user.userId} </foreach> </select> 数组String[] params 用array mapper 接口 int queryUserCount(String[] ids); mapper映射xml: <select id="queryUserCount" resultType="Integer"> select count(*) from users where userId in <foreach collection="array" index="index" item="id" open="(" close=")" separator=","> #{id} </foreach> </select> 传入参数为实体类 且实体中包含数组和集合 实体类 @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 bz_info <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> 传入多个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_info <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> map参数 当传入参数为 Map<String,Objject> 也可使用foreach List<SysUser> getUserByIds(Map<String,Object> map); // xml <select id="getUserByIds" resultMap="BaseResultMap"> select * from t_user_info where status = #{status} and id in <foreach collection="ids" item="id" open="(" close=")" separator=","> #{id} </foreach> </select> 代码测试 @Test public void testMapUsers() { Map<String,Object> params = new HashMap<>(); params.put("status", "1"); params.put("ids", Arrays.asList(1,2,3,4,5,6,7,8)); List<SysUser> all = sysUserDao.getUserByIds(params); try { System.out.println(new JsonMapper().writeValueAsString(all)); } catch (JsonProcessingException e) { e.printStackTrace(); } }版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。