JdbcTemplate的使用

在网上看了很多教程,不是很明白多表查询究竟是怎么搞的:

/**根据数量来统计*/
	@Override
	public List<InStockDetailPage> combobox2Sl( InStockDetailPage isp) {
		List<InStockDetailPage> nl = new ArrayList<InStockDetailPage>();
		StringBuffer sql = new StringBuffer();
		sql.append( "select d.incode from drp_instock d where d.status=1 ");
		if(isp.getCkid()!=null&&!isp.getCkid().equals("")){
			sql.append( " and d.rhck='"+isp.getCkid()+"' ");
		}
		if(isp.getJsr()!=null&&!isp.getJsr().equals("")){
			sql.append(" and d.jsr='"+isp.getJsr()+"' ");
		}
		
		if(isp.getRkstart()!=null&&!isp.getRkstart().equals("")){
			sql.append(" and d.indate >='"+isp.getRkstart()+"' ");
		}
		
		if(isp.getRkend()!=null&&!isp.getRkend().equals("")){
			sql.append(" and d.indate <='"+isp.getRkend()+"' ");
		}
		
		if(isp.getGys()!=null&&!isp.getGys().equals("")){
			sql.append( " and d.gys >='"+isp.getGys()+"' ");
		}
		String hql = "select t.gid gid,sum(t.num) tsum from drp_instockdetail t where 1=1 and t.inid in ("+sql.toString()+") group by t.gid";
		JdbcTemplate jt = goodsDao.getJdbcTemplate();
		List<Map<String, Object>> list=jt.queryForList(hql, new Object[]{});
		for (Map<String,Object> m :list) {
			InStockDetailPage isdp = new InStockDetailPage();
			isdp.setGid(m.get("gid").toString());
			int num = 0;
			try {
				num =Integer.parseInt(m.get("tsum").toString());
			} catch (Exception e) {
				num = 0;
			}
			isdp.setGname(goodsDao.get(Goods.class, isdp.getGid()).getName());
			isdp.setGcode(goodsDao.get(Goods.class, isdp.getGid()).getCode());
			isdp.setDid(UUID.randomUUID().toString());
			isdp.setTnum(num);
			nl.add(isdp);
		}
		return nl;
	}
@Override
	public DataGrid datagrid2TabJe(InStockDetailPage isp) {
		DataGrid dg = new DataGrid();
		List<InStockDetailPage> nl = new ArrayList<InStockDetailPage>();
		StringBuffer sql = new StringBuffer();
		sql.append( "select d.incode from drp_instock d where d.status=1 ");
		if(isp.getCkid()!=null&&!isp.getCkid().equals("")){
			sql.append( " and d.rhck='"+isp.getCkid()+"' ");
		}
		if(isp.getJsr()!=null&&!isp.getJsr().equals("")){
			sql.append(" and d.jsr='"+isp.getJsr()+"' ");
		}
		
		if(isp.getRkstart()!=null&&!isp.getRkstart().equals("")){
			sql.append(" and d.indate >='"+isp.getRkstart()+"' ");
		}
		
		if(isp.getRkend()!=null&&!isp.getRkend().equals("")){
			sql.append(" and d.indate <='"+isp.getRkend()+"' ");
		}
		
		if(isp.getGys()!=null&&!isp.getGys().equals("")){
			sql.append( " and d.gys >='"+isp.getGys()+"' ");
		}
		String hql = "select t.gid gid,sum(t.lsum) msum from drp_instockdetail t where 1=1 and t.inid in ("+sql.toString()+") group by t.gid";
		JdbcTemplate jt = goodsDao.getJdbcTemplate();
		List<Map<String, Object>> list=jt.queryForList(hql, new Object[]{});
		for (Map<String,Object> m :list) {
			InStockDetailPage isdp = new InStockDetailPage();
			isdp.setGid(m.get("gid").toString());
			BigDecimal num = new BigDecimal(0);
			try {
				num =new BigDecimal(m.get("msum").toString());
			} catch (Exception e) {
				num =new BigDecimal(0);
			}
			isdp.setDid(UUID.randomUUID().toString());
			isdp.setMsum(num);
			nl.add(isdp);
		}
		double d = 0;
		for(InStockDetailPage i : nl){
			d += i.getMsum().doubleValue();
		}
		List<InStockDetailPage> nl2 = new ArrayList<InStockDetailPage>();
		for(InStockDetailPage i : nl){
			if(i.getMsum().doubleValue()>0){
				i.setSzbl(String.format("%.2f", i.getMsum().doubleValue()/d*100)+"%");
			}else{
				i.setSzbl("0%");
			}
			Goods g = goodsDao.get(Goods.class, i.getGid());
			i.setGname(g.getName());
			i.setGcode(g.getCode());
			
			nl2.add(i);
		}
		dg.setTotal(Long.valueOf(nl.size()));
		dg.setRows(nl2);
		return dg;
	}
@Override
	public DataGrid datagrid2TabSl(InStockDetailPage isp) {
		DataGrid dg = new DataGrid();
		List<InStockDetailPage> nl = new ArrayList<InStockDetailPage>();
		StringBuffer sql = new StringBuffer();
		sql.append( "select d.incode from drp_instock d where d.status=1 ");
		if(isp.getCkid()!=null&&!isp.getCkid().equals("")){
			sql.append( " and d.rhck='"+isp.getCkid()+"' ");
		}
		if(isp.getJsr()!=null&&!isp.getJsr().equals("")){
			sql.append(" and d.jsr='"+isp.getJsr()+"' ");
		}
		
		if(isp.getRkstart()!=null&&!isp.getRkstart().equals("")){
			sql.append(" and d.indate >='"+isp.getRkstart()+"' ");
		}
		
		if(isp.getRkend()!=null&&!isp.getRkend().equals("")){
			sql.append(" and d.indate <='"+isp.getRkend()+"' ");
		}
		
		if(isp.getGys()!=null&&!isp.getGys().equals("")){
			sql.append( " and d.gys >='"+isp.getGys()+"' ");
		}
		String hql = "select t.gid gid,sum(t.num) tsum from drp_instockdetail t where 1=1 and t.inid in ("+sql.toString()+") group by t.gid";
		JdbcTemplate jt = goodsDao.getJdbcTemplate();
		List<Map<String, Object>> list=jt.queryForList(hql, new Object[]{});
		for (Map<String,Object> m :list) {
			InStockDetailPage isdp = new InStockDetailPage();
			isdp.setGid(m.get("gid").toString());
			int num = 0;
			try {
				num =Integer.parseInt(m.get("tsum").toString());
			} catch (Exception e) {
				num = 0;
			}
			isdp.setGname(goodsDao.get(Goods.class, isdp.getGid()).getName());
			isdp.setGcode(goodsDao.get(Goods.class, isdp.getGid()).getCode());
			isdp.setDid(UUID.randomUUID().toString());
			isdp.setTnum(num);
			nl.add(isdp);
		}
		double d = 0;
		for(InStockDetailPage i : nl){
			d += i.getTnum();
		}
		List<InStockDetailPage> nl2 = new ArrayList<InStockDetailPage>();
		for(InStockDetailPage i : nl){
			if(i.getTnum()>0){
				i.setSzbl(String.format("%.2f", i.getTnum()/d*100)+"%");
			}else{
				i.setSzbl("0%");
			}
			Goods g = goodsDao.get(Goods.class, i.getGid());
			i.setGname(g.getName());
			i.setGcode(g.getCode());
			
			nl2.add(i);
		}
		dg.setTotal(Long.valueOf(nl.size()));
		dg.setRows(nl2);
		return dg;
	}
好了,就这样吧,你若能懂,那就懂了,你若不能,我就自言自语了!




你可能感兴趣的:(JdbcTemplate的使用)