NutzCN Logo
问答 多表查询分页问题总数问题
发布于 13天前 作者 franktrue 130 次浏览 复制 上一个帖子 下一个帖子
标签: nutzwk

<T> List<T> queryByJoin(Class<T> var1, String var2, Condition var3, Pager var4); <T> List<T> queryByJoin(Class<T> var1, String var2, Condition var3, Pager var4, Map<String, Condition> var5); <T> int countByJoin(Class<T> var1, String var2, Condition var3);

目前使用queryByJoin如下所示

@CacheResult
    public List<Food_detect_data> data (NutMap nutMap) {
        Cnd cnd = Cnd.NEW();
        //检测项目
        String testItemIds = nutMap.getString("testItemId[]");
        if(testItemIds!=null) {
            cnd.and("testItemId", "in", testItemIds);
        }

        //检测地区
        Map<String, Condition> map = new HashMap<String, Condition>();
        Integer districtId = nutMap.getInt("districtId");
        if(districtId!=null&&districtId>0) {
            map.put("market", Cnd.NEW().where("districtId", "=", districtId));
        }

        //检测类别
        Integer detectKind = nutMap.getInt("detectKind");
        if(detectKind!=null&&detectKind>0) {
            cnd.and("detectKind", "=", detectKind);
        }

        //检测单位
        String unitId = nutMap.getString("unitId");
        if(unitId!=null) {
            cnd.and("unitId", "=", unitId);
        }

        //时间段筛选
        String[] detectDate = nutMap.getArray("detectDate[]", String.class);
        if(detectDate!=null) {
            SqlExpressionGroup c = Cnd.exps(new Static("detectDate between '"+detectDate[0]+"' and '"+detectDate[1]+"'"))
                    .or("detectDate", "like", detectDate[1]+"%");
            cnd.and(c);
        }

        //消费者送检
        Integer isConsumerSent = nutMap.getInt("isConsumerSent");
        if(isConsumerSent!=null&&isConsumerSent>0) {
            cnd.and("isConsumerSent", "=", isConsumerSent);
        }
        //食品分类
        String[] parent = StringUtils.split(nutMap.getString("parent[]"), ",");
        if(parent !=null) {
            if(parent.length == 3) {
                cnd.and("kindId", "=", Integer.parseInt(parent[2]));
            }else if(parent.length == 2) {
                cnd.and("smallKindId", "=", Integer.parseInt(parent[1]));
            }else if(parent.length == 1) {
                cnd.and("bigKindId", "=", Integer.parseInt(parent[0]));
            }
        }

        //样品编号
        String sn = nutMap.getString("sn");
        if(sn!=null) {
            cnd.and("sn", "like", "%"+sn+"%");
        }

        //检测结果
        Integer detectResult = nutMap.getInt("detectResult");
        if(detectResult!=null&&detectResult>0) {
            cnd.and("detectResult", "=", detectResult);
        }

        //被检单位
        String byDetectUnit = nutMap.getString("byDetectUnit");
        if(sn!=null) {
            cnd.and("byDetectUnit", "like", "%"+byDetectUnit+"%");
        }
        Pager pager = new Pager(nutMap.getInt("pageNumber"), nutMap.getInt("pageSize"));
        cnd.orderBy("detectDate", "desc");
        return dao().queryByJoin(Food_detect_data.class, "^(unit|smallKind|kind|testItem|testStandard|market)$", cnd, pager, map);
    }

其中countByJoin没有Map<String, Condition> 参数,所以没有办法获得总数……还是要写SQL啊

22 回复

nutz最新版吗?

@wendal 对的1.r.68,nutzWk5.2 而且我发先queryByJoin好像没用……还是我的Map写的有问题啊

方案一

//检测地区
        Cnd oneCnd = Cnd.NEW();
        Integer districtId = nutMap.getInt("districtId");
        System.out.println(districtId);
        if(districtId!=null&&districtId>0) {
            oneCnd.and("districtId", "=", districtId);
        }
        foodDetectDataService.listPageLinks(nutMap.getInt("pageNumber"), nutMap.getInt("pageSize"), cnd, "^(market|unit|smallKind|kind|testItem|testStandard)$", oneCnd);

方案二

//检测地区
        Map<String, Condition> map = new HashMap<String, Condition>();
        Integer districtId = nutMap.getInt("districtId");
        if(districtId!=null&&districtId>0) {
            map.put("market", Cnd.NEW().where("districtId", "=", districtId));
        }
        Pager pager = new Pager(nutMap.getInt("pageNumber"), nutMap.getInt("pageSize"));
        cnd.orderBy("detectDate", "desc");
        return dao().queryByJoin(Food_detect_data.class, "^(unit|smallKind|kind|testItem|testStandard|market)$", cnd, pager, map);

其中方案一,自条件查询无效(oneCnd),如果使用方案二的话有没有办法获取到总数。最终只能写SQL

@wendal
方案一

//检测地区
Cnd oneCnd = Cnd.NEW();
Integer districtId = nutMap.getInt("districtId");
System.out.println(districtId);
if(districtId!=null&&districtId>0) {
oneCnd.and("districtId", "=", districtId);
}
foodDetectDataService.listPageLinks(nutMap.getInt("pageNumber"), nutMap.getInt("pageSize"), cnd, "^(market|unit|smallKind|kind|testItem|testStandard)$", oneCnd);
方案二

//检测地区
Map<String, Condition> map = new HashMap<String, Condition>();
Integer districtId = nutMap.getInt("districtId");
if(districtId!=null&&districtId>0) {
map.put("market", Cnd.NEW().where("districtId", "=", districtId));
}
Pager pager = new Pager(nutMap.getInt("pageNumber"), nutMap.getInt("pageSize"));
cnd.orderBy("detectDate", "desc");
return dao().queryByJoin(Food_detect_data.class, "^(unit|smallKind|kind|testItem|testStandard|market)$", cnd, pager, map);
其中方案一,自条件查询无效(oneCnd),如果使用方案二的话有没有办法获取到总数。最终只能写SQL

都是@One关联?

再说了,queryByJoin的数量只跟主表有关系吧?

@wendal

select count(*) from A a left join B b a.B_id=b.id where b.districtId = 1
select count(*) from A 

这两个的结果应该不一样吧……

少加了个on

匹配不上的只是为null,并不影响主表的输出数量

@wendal 谢谢。受教了,但是如果是我上面的要求是不是只能手写sql了

方案二,加个dao.count,用主表条件就能知道总数啦

@wendal 感觉有点矛盾啊

//检测地区
        Map<String, Condition> map = new HashMap<String, Condition>();
        Integer districtId = nutMap.getInt("districtId");
        if(districtId!=null&&districtId>0) {
            map.put("market", Cnd.NEW().where("districtId", "=", districtId));
        }
        Pager pager = new Pager(nutMap.getInt("pageNumber"), nutMap.getInt("pageSize"));
        cnd.orderBy("detectDate", "desc");
        List<Food_detect_data> list = dao().queryByJoin(Food_detect_data.class, "^(unit|smallKind|kind|testItem|testStandard|market)$", cnd, pager, map);
        Integer total = dao().countByJoin(Food_detect_data.class, "^(unit|smallKind|kind|testItem|testStandard|market)$", cnd);

我的list 是通过cnd,还有map这个关联查询筛选出来的,但是total数量仅仅是通过cnd过滤出来的,这样不对吧

left join 哦,不是join

@wendal 这个我明白,那下面这个Map就是没用的喽。

<T> List<T> queryByJoin(Class<T> var1, String var2, Condition var3, Pager var4, Map<String, Condition> var5);

对count没用

看来你没关联源码,原变量名都没出来

@wendal

Map<String, Condition> map = new HashMap<String, Condition>();
        Integer districtId = nutMap.getInt("districtId");
        if(districtId!=null&&districtId>0) {
            map.put("market", Cnd.NEW().where("districtId", "=", districtId));
        }

实际使用发现这个map并没有用……

market是关联属性名?@One的?

@One(field = "marketId")
    private Food_market market;

那得查查了

cnds只对@Many和@ManyMany有效

添加回复
请先登陆
回到顶部