【Mybatis】<foreach>标签在mybatis中的使用

mapper.xml如下:


<select id="selectCkspcb" parameterType="java.util.Map"
                resultType="java.util.Map">
                
        SELECT
            COALESCE (pg.goodsid, 0) spbm,
            COALESCE (pg.goodsname, '') spmc,
            COALESCE (
                (
                    SELECT
                        classname
                    FROM
                        pub_goodsclass vv
                    WHERE
                        vv.classcode = (
                            SELECT
                                CASE
                            WHEN lent.len &lt; 4 THEN
                                ''
                            ELSE
                                substr(lent.classcode, 0, 5)
                            END AS code
                            FROM
                                (
                                    SELECT
                                        LENGTH (ccc.classcode) len,
                                        ccc.classcode
                                ) lent
                            LIMIT 1 OFFSET 0
                        )
                ),
                ''
            ) zybm,
            COALESCE (pb.brandname, '') pp,
            COALESCE (pd.depotname, '') ck,
            COALESCE (pg.goodscode, '') spcode,
            COALESCE (ssgss.stockqty, 0) zmsl,
        
            COALESCE (
                (
                    SELECT
                        SUM (taxprice * ssgss.stockqty)
                    FROM
                        sp_st_batch
                    WHERE
                        stgoodsid = ssgg.stgoodsid
                    AND depotid = ssgss.depotid
                ),
                0
            ) zmcb,
            CASE
        WHEN ssgss.stockqty &gt; 0 THEN
            (
                SELECT
                    SUM (taxprice * ssgss.stockqty) / ssgss.stockqty
                FROM
                    sp_st_batch
                WHERE
                    stgoodsid = ssgg.stgoodsid
                AND depotid = ssgss.depotid
            )
        ELSE
            '0'
        END cbjj,
         ssgg.stgoodsid,
         ssgss.depotid,
         ssgg.stayinqty,
         ssgg.stayoutqty,
         ssgg.presaleqty
        FROM
            sp_st_goodsstock ssgss
        LEFT JOIN sp_st_goods ssgg ON ssgss.stgoodsid = ssgg.stgoodsid
        LEFT JOIN pub_depot pd ON ssgss.depotid = pd.depotid
        LEFT JOIN pub_goods pg ON ssgg.goodsid = pg.goodsid
        LEFT JOIN pub_brand pb ON pg.brandid = pb.brandid
        LEFT JOIN pub_goodsclass ccc ON pg.goodsclassid = ccc.classid
        WHERE
            1 = 1
        AND ssgss.enterpriseid = #{enterpriseid,jdbcType=NUMERIC}
        <!-- 商品搜索框的商品编码条件查询 -->
        <if test="spbm != null and spbm!='' ">
        AND ssgg.stgoodsid=#{spbm,jdbcType=NUMERIC}
        </if>
        
        <!-- 仓库编码查询 -->
        <!--<if test="ckbms != null and ckbms !='' ">不能进行空字符串判断,不然报错-->
        <if test="ckbms != null ">
        AND ssgss.depotid in 
        <foreach collection="ckbms" item="ckbm" index="index" open="(" close=")" separator=","> #{ckbm}</foreach>
        </if>
        
        <!-- 商品分类编码查询 -->
        <if test="zybms != null ">
        AND ccc.classid in
        <foreach collection="zybms" item="zybm" index="index" open="(" close=")" separator=",">#{zybm}</foreach>
        </if>
        
        <!-- 品牌编码查询 -->
        <if test="pp != null and pp!='' ">
        AND pb.brandid=#{pp,jdbcType=NUMERIC}
        </if>
        
        <!-- 包括保管账为0 -->
        <if test=" isZero!=0">
        AND ssgss.stockqty &lt;&gt; 0
        </if>
        
        <!-- 分页查询 -->
        limit #{pageSize,jdbcType=NUMERIC} offset #{start,jdbcType=NUMERIC}
        </select>

 

 

其中仓库编码和商品分类编码都使用了foreach进行迭代循环,页面jsp场景如下:

 

业务需求中需要查询多个仓库和商品分类的值,所以在页面将仓库和分类的id值用逗号隔开,以一串string传入后台,并将其拆解成List<Integer>或者List<String>,最后塞进map里面传到xml。

例如:

controller

@RequestMapping(value = { "reportform/queryData" }, method = { RequestMethod.GET,
            RequestMethod.POST })
    @ResponseBody
    public JsonResult queryData(
            ModelMap model,
            @RequestParam(value = "enterpriseid", required = false) Integer enterpriseid,
            @RequestParam(value = "pageSize", required = false,defaultValue ="4") Integer pageSize,
            @RequestParam(value = "pageNo", required = false,defaultValue="1") Integer pageNo,        
            @RequestParam(value = "spbm", required = false) Long spbm,
            @RequestParam(value = "ckbm", required = false) String ckbm,
            @RequestParam(value = "zybm", required = false) String zybm,
            @RequestParam(value = "pp", required = false) Long pp,
            @RequestParam(value = "spmc", required = false) String spmc,
            @RequestParam(value = "ck", required = false) String ck,
            @RequestParam(value = "zmsl", required = false) Integer zmsl,
            @RequestParam(value = "zmcb", required = false) String zmcb,
            @RequestParam(value = "cbjj", required = false) String cbjj,
            @RequestParam(value = "jyfs", required = false) String jyfs,
            @RequestParam(value = "jglx", required = false) String jglx,
            @RequestParam(value = "spsx", required = false) String spsx,
            @RequestParam(value = "isZero", required = false) Integer isZero
            ) {
    
        JsonResult jrs=null;
        try {
            List<Integer> ckbms = tranStrToListOfInter(ckbm);
            List<Integer> zybms = tranStrToListOfInter(zybm);
            
            PageRequest pageRequest  = new PageRequest(pageNo-1, pageSize);
            Page<Map<String, Object>> resultList=spStGoodFacade.selectCkspcb(enterpriseid,pageRequest,ckbms,spbm,zybms,pp,spmc,ck,spsx,jglx,jyfs,zybm,isZero,zmsl);         
            jrs=JsonResult.createSuccess();
            jrs.addData(resultList);
        } catch (Exception e) {
            e.printStackTrace();
        }    
        return jrs;
    }

/**
     * 将前台输入框的多选字符串转为List
     * @param str
     * @return
     */
    public List<Integer> tranStrToListOfInter(String str){
        if(str!=null && str!=""){
        String[] sx=str.split(",");
        List<Integer> listx=new ArrayList<Integer>();
        for(int i=0;i<sx.length;i++){
            listx.add(Integer.valueOf(sx[i]));
            }
        return listx;
        }else{
            return null;
        } 
}

 

service

public  Page<Map<String, Object>> selectCkspcb(Integer enterpriseid,PageRequest pageRequest,List ckbms,Long spbm,List zybms,Long pp,String spmc,String ck,String spsx,String jglx,String jyfs,String zybm,Integer isZero,Integer zmsl){
        
        Map<String, Object> dataMap= new HashMap<String, Object>();
        
        int offset = pageRequest.getOffset();
        int pageSize = pageRequest.getPageSize();

        dataMap.put("pageSize", pageSize);
        dataMap.put("start",offset);
        dataMap.put("enterpriseid", enterpriseid);
        
        dataMap.put("ckbms",ckbms);
        dataMap.put("spbm",spbm);
        dataMap.put("zybms", zybms);
        dataMap.put("pp",pp);
        
        
        dataMap.put("ck", ck);
        dataMap.put("spmc", spmc);
        dataMap.put("isZero",isZero);
        dataMap.put("zybm",zybm);
        dataMap.put("jyfs",jyfs);
        dataMap.put("jglx",jglx);
        dataMap.put("spsx",spsx);
        
    
        Integer  total= spStGoodDao.selectCkspcbTotalCount(dataMap);//获取总单记录数
        List<Map<String, Object>>  resultList= spStGoodDao.selectCkspcb(dataMap);//获取总单记录
        List<Map<String, Object>> sum = spStGoodDao.selectCkspcbTotalZMSL(dataMap);
        resultList.addAll(sum);

        
        Page<Map<String, Object>> page=null;
        
        if(resultList!=null && resultList.size()>0 && total!=null){
            page  = new PageImpl<Map<String, Object>>(resultList, pageRequest, total);
        }
        
    
        return page;
        
    }

 

posted @ 2017-04-12 16:13  多弗朗明哥  阅读(3543)  评论(0编辑  收藏  举报