String querySql = "select `depart`.`departname` AS `departname`,`shebei`.`sbname` AS `sbname`,`shebei`.`sbtype` AS `sbtype`,`shebei`.`iscall` AS `iscall`,`base`.`shebei_no` AS `shebei_no`,`base`.`work_no` AS `work_no`,`base`.`handlers` AS `handlers`,`base`.`project_name` AS `project_name`,`base`.`job_location` AS `job_location`,`base`.`poure_location` AS `poure_location`,`base`.`cement_variety` AS `cement_variety`,`base`.`additive_variety` AS `additive_variety`,`base`.`formula_no` AS `formula_no`,`base`.`strength_rank` AS `strength_rank`,`base`.`stir_datetime` AS `stir_datetime`,`base`.`save_datetime` AS `save_datetime`,`base`.`client_no` AS `client_no`,`base`.`status` AS `status`,`base`.`collect_datetime` AS `collect_datetime`,`base`.`estimate_number` AS `estimate_number`,`base`.`product_datetime` AS `product_datetime`,`base`.`id` AS `baseId`,`shebei`.`userdepartid` AS `userdepartid`,`base`.`over_level` AS `over_level`,`base`.`slump` AS `slump`,`base`.`batch_no` AS `batch_no`"; StringBuilder queryCtest = new StringBuilder(""); queryCtest.append(" from `userdepart` `depart` join `shebei_info` `shebei` join `bhz_cement_base` `base` where `depart`.`id` = `shebei`.`userdepartid` and `shebei`.`sbjno` = `base`.`shebei_no`"); if(StringUtil.isNotEmpty(departName)) { Record findFirst = Db.findFirst("SELECT lft,rgt FROM userdepart WHERE id =?", departName); if(findFirst==null) { throw new DataNotExistsException("组织机构不存在!"); } String lft = findFirst.getStr("lft"); String rgt = findFirst.getStr("rgt"); queryCtest.append(" and '"+lft+"'<=depart.lft and depart.rgt<='"+rgt+"'"); }else { queryCtest.append(" and '"+lfte+"'<=depart.lft and depart.rgt<='"+rgte+"'"); } if(StringUtil.isNotEmpty(sbname)) { queryCtest.append(" and shebei_no='"+sbname+"'"); } if(StringUtil.isNotEmpty(projectName)) { queryCtest.append(" and project_name='"+projectName+"'"); } if(StringUtil.isNotEmpty(jobLocation)) { queryCtest.append(" and job_location='"+jobLocation+"'"); } if(StringUtil.isNotEmpty(poureLocation)) { queryCtest.append(" and poure_location='"+poureLocation+"'"); }
发布评论
评论(9)
我找了一个解决方案,
public List<Map<String,Object>> map = new ArrayList<Map<String,Object>>();
/**
* 拌合站数据查询接口
* @param departName
* @param sbname
* @param projectName
* @param jobLocation
* @param poureLocation
* @param lfte
* @param rgte
* @param begin
* @param pageSize
* @return
*/
public Page<BhzCementBase> bhzQuery(String departName,String sbname,String projectName,String jobLocation,String poureLocation,String lfte,String rgte,Integer currPage,Integer pageSize) {
String querySql = "select `depart`.`departname` AS `departname`,`shebei`.`sbname` AS `sbname`,`shebei`.`sbtype` AS `sbtype`,`shebei`.`iscall` AS `iscall`,`base`.`shebei_no` AS `shebei_no`,`base`.`work_no` AS `work_no`,`base`.`handlers` AS `handlers`,`base`.`project_name` AS `project_name`,`base`.`job_location` AS `job_location`,`base`.`poure_location` AS `poure_location`,`base`.`cement_variety` AS `cement_variety`,`base`.`additive_variety` AS `additive_variety`,`base`.`formula_no` AS `formula_no`,`base`.`strength_rank` AS `strength_rank`,`base`.`stir_datetime` AS `stir_datetime`,`base`.`save_datetime` AS `save_datetime`,`base`.`client_no` AS `client_no`,`base`.`status` AS `status`,`base`.`collect_datetime` AS `collect_datetime`,`base`.`estimate_number` AS `estimate_number`,`base`.`product_datetime` AS `product_datetime`,`base`.`id` AS `baseId`,`shebei`.`userdepartid` AS `userdepartid`,`base`.`over_level` AS `over_level`,`base`.`slump` AS `slump`,`base`.`batch_no` AS `batch_no`";
StringBuilder queryCtest = new StringBuilder("");
queryCtest.append(" from `userdepart` `depart` join `shebei_info` `shebei` join `bhz_cement_base` `base` where `depart`.`id` = `shebei`.`userdepartid` and `shebei`.`sbjno` = `base`.`shebei_no`");
if(StringUtil.isNotEmpty(departName)) {
Record findFirst = Db.findFirst("SELECT lft,rgt FROM userdepart WHERE id =?", departName);
if(findFirst==null) {
throw new DataNotExistsException("组织机构不存在!");
}
String lft = findFirst.getStr("lft");
String rgt = findFirst.getStr("rgt");
queryCtest.append(" and '"+lft+"'<=depart.lft and depart.rgt<='"+rgt+"'");
}else {
queryCtest.append(" and '"+lfte+"'<=depart.lft and depart.rgt<='"+rgte+"'");
}
if(StringUtil.isNotEmpty(sbname)) {
queryCtest.append(" and shebei_no='"+sbname+"'");
}
if(StringUtil.isNotEmpty(projectName)) {
queryCtest.append(" and project_name='"+projectName+"'");
}
if(StringUtil.isNotEmpty(jobLocation)) {
queryCtest.append(" and job_location='"+jobLocation+"'");
}
if(StringUtil.isNotEmpty(poureLocation)) {
queryCtest.append(" and poure_location='"+poureLocation+"'");
}
//queryCtest.append(" limit "+begin+","+pageSize+"");
//List<Record> find = Db.find(queryCtest.toString());
if(currPage==null) {
throw new DataNotExistsException("请输入当前页数");
}
if(pageSize==null) {
throw new DataNotExistsException("请输入每页显示多少条!");
}
Page<BhzCementBase> page = BhzCementBase.dao.paginate(currPage, pageSize, querySql, queryCtest.toString());
if(page.getList().size()==0) {
throw new DataNotExistsException("您访问的数据不存在!");
}
List<BhzCementBase>list = page.getList();
for (BhzCementBase bhz : list) {
Map<String,Object> m1 =new LinkedHashMap<String,Object>();
m1.put("BhzCementBase", bhz);
String sql = "select*from bhz_cement_detail where batch_no = ?";
List<BhzCementDetail> find = BhzCementDetail.dao.find(sql,bhz.getStr("batch_no"));
m1.put("BhzCementDetail", find);
map.add(m1);
}
return page;
}
public void bhzQuery() {
String departName = getPara("departName");
String sbname = getPara("sbname");
String projectName = getPara("projectName");
String jobLocation = getPara("jobLocation");
String poureLocation = getPara("poureLocation");
//当前页数
Integer currPage = getParaToInt("currPage");
Integer pageSize = getParaToInt("pageSize");
User user = getSessionAttr("user");
Record s = user.get("depart");
String lft = (String)s.getStr("lft");
String rgt = (String)s.getStr("rgt");
ApiResult<List<Map<String,Object>>> api;
try {
Page<BhzCementBase> bhzQuery = service.bhzQuery(departName, sbname, projectName, jobLocation, poureLocation,lft,rgt,currPage,pageSize);
api = new ApiResult<List<Map<String,Object>>>(ApiResult.SUCCESS,service.getMap(),bhzQuery.getTotalRow());
}catch (ServiceException e) {
api = new ApiResult<List<Map<String,Object>>>(e);
}
renderJson(api);
}
直接写SQL就可以了啊,JF会直接返回你查询字段的数据,然后再页面该怎么处理就怎么处理
感谢,我找了一个解决方案
回复
啥方案啊,在这里问,自己解决了也不分享
回复
就是直接在Model里面再写个方法,文档里面有,不过,这个就是循环查询,效率太低。
不知道 jfinal 怎么实现的,只知道mybatis 在xml中这样做:
property 所对应的是 一的一方, column 对应是多的一方 就实现了一对多了
我现在的做法就是先查出所有的a,然后再for循环a,根据a.id查出所有的b,再a.setB,然后再返回aList。真心希望波总下次更新的时候把这一块支持加上,这个场景在开发的时候很常见。
@12叔 感谢您的回复
方法二 比较简单 先查a 数据 找查询a的一对多的数据 然后 a.put(b)
好像没有现成的实现
我的思路是 对sql 语句先进行处理
输入的sql 语句是这样的 select * from a join b
程序预处理成这样 就可以分辨出是哪个对象的 根据表的所有属性
select a.id a.name b.id b.name from a join b
然后对查询结果集进行封装成 model