zoukankan      html  css  js  c++  java
  • 【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;
            
        }
  • 相关阅读:
    了解 NoSQL 的必读资料
    关于什么时候用assert(断言)的思考
    这次见到了一些大侠
    NetBeans 时事通讯(刊号 # 87 Jan 12, 2010)
    动态链接库dll,静态链接库lib, 导入库lib
    新女性十得 写得了代码,查得出异常
    记录系统乱谈
    新女性十得 写得了代码,查得出异常
    fullpage.js禁止滚动
    RunningMapReduceExampleTFIDF hadoopclusternet This document describes how to run the TFIDF MapReduce example against ascii books. This project is for those who wants to experiment hadoop as a skunkworks in a small cluster (110 nodes) Google Pro
  • 原文地址:https://www.cnblogs.com/dflmg/p/6699722.html
Copyright © 2011-2022 走看看