温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MyBatis怎么进行双重foreach循环

发布时间:2022-02-08 14:54:36 来源:亿速云 阅读:316 作者:小新 栏目:开发技术

这篇文章主要介绍MyBatis怎么进行双重foreach循环,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

进行双重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>

以上是“MyBatis怎么进行双重foreach循环”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI