foreach用法java,【Mybatis】foreach標簽在mybatis中的使用

 2023-10-05 阅读 27 评论 0

摘要:mapper.xml如下: <select id="selectCkspcb" parameterType="java.util.Map"resultType="java.util.Map">SELECTCOALESCE (pg.goodsid, 0) spbm,COALESCE (pg.goodsname, '') spmc,COALESCE ((SELECTclassnameFROMp

mapper.xml如下:

 
<select id="selectCkspcb" parameterType="java.util.Map"resultType="java.util.Map">SELECTCOALESCE (pg.goodsid, 0) spbm,COALESCE (pg.goodsname, '') spmc,COALESCE ((SELECTclassnameFROMpub_goodsclass vvWHEREvv.classcode = (SELECTCASEWHEN lent.len &lt; 4 THEN''ELSEsubstr(lent.classcode, 0, 5)END AS codeFROM(SELECTLENGTH (ccc.classcode) len,ccc.classcode) lentLIMIT 1 OFFSET 0)),'') zybm,COALESCE (pb.brandname, '') pp,COALESCE (pd.depotname, '') ck,COALESCE (pg.goodscode, '') spcode,COALESCE (ssgss.stockqty, 0) zmsl,COALESCE ((SELECTSUM (taxprice * ssgss.stockqty)FROMsp_st_batchWHEREstgoodsid = ssgg.stgoodsidAND depotid = ssgss.depotid),0) zmcb,CASEWHEN ssgss.stockqty &gt; 0 THEN(SELECTSUM (taxprice * ssgss.stockqty) / ssgss.stockqtyFROMsp_st_batchWHEREstgoodsid = ssgg.stgoodsidAND depotid = ssgss.depotid)ELSE'0'END cbjj,ssgg.stgoodsid,ssgss.depotid,ssgg.stayinqty,ssgg.stayoutqty,ssgg.presaleqtyFROMsp_st_goodsstock ssgssLEFT JOIN sp_st_goods ssgg ON ssgss.stgoodsid = ssgg.stgoodsidLEFT JOIN pub_depot pd ON ssgss.depotid = pd.depotidLEFT JOIN pub_goods pg ON ssgg.goodsid = pg.goodsidLEFT JOIN pub_brand pb ON pg.brandid = pb.brandidLEFT JOIN pub_goodsclass ccc ON pg.goodsclassid = ccc.classidWHERE1 = 1AND 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場景如下:

?

foreach用法java?業務需求中需要查詢多個倉庫和商品分類的值,所以在頁面將倉庫和分類的id值用逗號隔開,以一串string傳入后臺,并將其拆解成List<Integer>或者List<String>,最后塞進map里面傳到xml。

例如:

controller

@RequestMapping(value = { "reportform/queryData" }, method = { RequestMethod.GET,RequestMethod.POST })@ResponseBodypublic 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;}

?

mybatis標簽?轉載于:https://www.cnblogs.com/dflmg/p/6699722.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/118109.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息