jfinal 一对多关联查询 结果集封装

rylpngy 发布于 2016/04/28 13:52
阅读 2K+
收藏 0

【开源中国 APP 全新上线】“动弹” 回归、集成大模型对话、畅读技术报告”

 麻烦问个问题,之前做springmvc+mybatis的时候,在做一对多关联查询的时候,返回结果可以直接封装到model里面。jfinal对这块是怎么处理的,对结果集的封装是怎么来实现的。

就是实现mybatis的collection的效果。

ps:@jfinal jfinal cms 啥时候出来啊,很期待啊

加载中
0
12叔
12叔

好像没有现成的实现

我的思路是 对sql 语句先进行处理  

输入的sql 语句是这样的 select * from a join b

程序预处理成这样  就可以分辨出是哪个对象的 根据表的所有属性

select  a.id a.name b.id b.name from a join b 

然后对查询结果集进行封装成 model  



12叔
12叔
方法二 比较简单 先查a 数据 找查询a的一对多的数据 然后 a.put(b)
0
r
rylpngy
我现在的做法就是先查出所有的a,然后再for循环a,根据a.id查出所有的b,再a.setB,然后再返回aList。真心希望波总下次更新的时候把这一块支持加上,这个场景在开发的时候很常见。 @12叔 感谢您的回复
0
天上飞的鱼
天上飞的鱼

不知道 jfinal  怎么实现的,只知道mybatis 在xml中这样做:

<resultMap id="**" type="****" extends="BaseResultMap">
  	<collection property="itemRelation" column="GROUP_ID"  select="" />
  </resultMap>

select里面所对应的是另外一个xml里面写的select路径,比如:com.cctv.XXXMapper.get


property 所对应的是 一的一方, column 对应是多的一方 就实现了一对多了



mavenjay
mavenjay
该评论暂时无法显示,详情咨询 QQ 群:点此入群
r
rylpngy
回复 @错觉 : 就是直接在Model里面再写个方法,文档里面有,不过,这个就是循环查询,效率太低。
晨儿哥1984
晨儿哥1984
回复 @rylpngy : 啥方案啊,在这里问,自己解决了也不分享
r
rylpngy
感谢,我找了一个解决方案
0
tonysb
tonysb
直接写SQL就可以了啊,JF会直接返回你查询字段的数据,然后再页面该怎么处理就怎么处理
0
拉风的凤吹哥
拉风的凤吹哥
该评论暂时无法显示,详情咨询 QQ 群:点此入群
0
JaneLove
JaneLove

我找了一个解决方案,

 

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);
        
        
    }

OSCHINA
登录后可查看更多优质内容
返回顶部
顶部