MyBatis如何进行双重foreach循环
作者:harden_no1 时间:2022-11-24 22:54:17
进行双重foreach循环
mapname是一个Map<String,Map<String,Object>> 对象
<foreach collection="mapname" index="key1" item="value1" separator=",">
<foreach collection="value1" index="key2" item="value2" separator=",">
#{key1},
#{key2},
#{value2}
</foreach>
</foreach>
使用场景
比如说一个付款人下面对应的运单的金额,那么key1就是付款人编号,key2是运单编号,value2是分摊的金额
mybatis foreach循环,传入多个参数
上代码
controller:
@RequestMapping(value = "/findPage", method = RequestMethod.POST)
@ResponseBody
public Object findPage(@RequestParam(required=false) String jobCategory,@RequestParam(required=false) String ids,@RequestParam(required=false) String cities) {
try {
List<Integer> listJob = new ArrayList<Integer>();
List<Integer> listIds = new ArrayList<Integer>();
List<String> listCities = new ArrayList<String>();
//按照城市名称和工种查询
if(StringUtils.isNotBlank(jobCategory) && StringUtils.isNotBlank(cities)){
String[] temp = jobCategory.split(",");
String[] cityTemp = cities.split(",");
for(int i=0;i<temp.length;i++){
listJob.add(Integer.valueOf(temp[i]));
}
for(int i=0;i<cityTemp.length;i++){
listCities.add(cityTemp[i]);
}
List<WebsitesJob> list = jobService.findPage(listJob, listIds, listCities);
return new ExtGridReturn(list.size(), list);
}
//按照工种查询
if(StringUtils.isNotBlank(jobCategory)){
String[] temp = jobCategory.split(",");
for(int i=0;i<temp.length;i++){
listJob.add(Integer.valueOf(temp[i]));
}
}
//按照职位名称查询
if(StringUtils.isNotBlank(ids)){
String[] temp = ids.split(",");
for(int i=0;i<temp.length;i++){
listIds.add(Integer.valueOf(temp[i]));
}
}
//按照城市查询
if(StringUtils.isNotBlank(cities)){
String[] temp = cities.split(",");
for(int i=0;i<temp.length;i++){
listCities.add(temp[i]);
}
}
List<WebsitesJob> list = jobService.findPage(listJob, listIds, listCities);
return new ExtGridReturn(list.size(), list);
} catch (Exception e) {
LOGGER.error("分页获取信息出错", e);
return new ExceptionReturn(e);
}
}
mapper
/**
* 描述:根据工种查询列表
* @param jobCategorys
* @return
*/
List<WebsitesJob> findPage(@Param("jobCategorys") List<Integer> jobCategorys,@Param("ids") List<Integer> ids,@Param("workPlace") List<String> workPlace);
xml
<!-- 前台查询列表 -->
<select id="findPage" resultType="cn.edu.hbcf.plugin.websites.pojo.WebsitesJob">
select n.ID id,
n.NAME name,
n.WORKPLACE workPlace,
n.JOBCATEGORY jobCategory,
n.SALARY salary,
n.RESPONSIBILITIES responsibilities,
n.REQUIREMENTS requirements,
n.ISHOT isHot,
n.UPDATEDATE updateDate,
n.UPDATEUSER updateUser,
u.real_name updateName
from websites_job n
left join base_users u on n.updateUser = u.account
<where>
<if test="jobCategorys.size()!=0">
or n.jobCategory in
<foreach collection="jobCategorys" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</if>
<if test="ids.size()!=0">
or n.id in
<foreach collection="ids" item="item" index="index" open="(" separator="," close=")">
#{item}
</foreach>
</if>
<if test="workPlace.size()!=0">
or n.WORKPLACE in
<foreach collection="workPlace" item="item" index="index" open="(" separator="," close=")">
#{item}
</foreach>
</if>
</where>
order by n.ISHOT,n.ID desc
</select>
来源:https://blog.csdn.net/harden_no1/article/details/122442784
标签:MyBatis,双重,foreach,循环
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
一篇文章带你入门Java数据类型
2022-06-10 09:25:44
![](https://img.aspxhome.com/file/2023/8/62338_0s.jpg)
如何利用Spring把元素解析成BeanDefinition对象
2023-11-23 05:17:16
![](https://img.aspxhome.com/file/2023/9/59939_0s.png)
Java数组(Array)最全汇总(中篇)
2022-09-05 06:01:02
![](https://img.aspxhome.com/file/2023/8/63638_0s.jpg)
详解如何实现SpringBoot的底层注解
2023-11-23 06:21:04
![](https://img.aspxhome.com/file/2023/0/59040_0s.png)
java.math.BigDecimal的用法及加减乘除计算
2022-01-15 15:55:20
Java微信支付-微信红包
2023-11-01 01:07:05
![](https://img.aspxhome.com/file/2023/6/66166_0s.png)
JavaWeb Servlet实现文件上传与下载功能实例
2023-06-16 16:41:27
![](https://img.aspxhome.com/file/2023/2/62532_0s.png)
JAVA实现社会统一信用代码校验的方法
2023-04-10 08:38:21
Android 实现永久保存数据的方法详解
2023-06-23 22:13:23
解析Java内存分配和回收策略以及MinorGC、MajorGC、FullGC
2023-02-06 08:22:19
![](https://img.aspxhome.com/file/2023/3/62783_0s.png)
Spring Bean生命周期之BeanDefinition的合并过程详解
2023-11-29 02:50:35
关于idea更新到2020.2.3无法创建web项目原因 library is not specified
2022-11-24 10:13:28
![](https://img.aspxhome.com/file/2023/6/64006_0s.png)
Java二维数组与动态数组ArrayList类详解
2023-07-31 19:53:56
Mybatis与Jpa的区别和性能对比总结
2022-09-17 16:08:45
Java中的static关键字修饰属性和方法(推荐)
2021-09-29 05:46:20
![](https://img.aspxhome.com/file/2023/5/66035_0s.jpg)
使用Postman传递arraylist数据给springboot方式
2022-08-27 01:13:01
![](https://img.aspxhome.com/file/2023/9/69359_0s.jpg)
零基础写Java知乎爬虫之获取知乎编辑推荐内容
2023-11-29 04:33:39
![](https://img.aspxhome.com/file/2023/3/60783_0s.png)
安卓GreenDao框架一些进阶用法整理
2023-06-17 03:27:21
springboot 使用QQ邮箱发送邮件的操作方法
2022-03-03 14:36:22
![](https://img.aspxhome.com/file/2023/8/62218_0s.png)
解决spring.thymeleaf.cache=false不起作用的问题
2022-03-25 04:27:15
![](https://img.aspxhome.com/file/2023/5/63285_0s.png)