播放框架RawSql我怎样才能获得TotalTotalPage

时间:2016-08-13 09:59:14

标签: java web-applications playframework playframework-2.2

我使用Play 2.2.6。我想用RawSql得到'getTotalPageCount',但是 我总是得到一页记录。 这是代码:

   String sql = "select "
            + "a.id as id ,"
            + "b.name as computerType ,"
            + "a.buy_time as buyTime ,"
            + "a.user_name as userName, "
            + "a.if_use as ifUse from computer a left join computer_type b on a.computer_type = b.code where 1=1 ";

    if(StringUtil.StringNoEmpty(vo.userName)){
        sql += " and a.user_name like '%"+vo.userName+"%'";
    }

    if(StringUtil.StringNoEmpty(vo.computerType)){
        sql += " and a.computer_type = '"+vo.computerType+"'";
    }
    if(null!=vo.startTime){         
        String startTime = DateUtil.formate.format(vo.startTime);
        sql += " and a.buy_time >= '"+startTime+"'";
    }

    if(null!=vo.endTime){
        String endTime = DateUtil.formate.format(vo.endTime);
        sql += " and a.buy_time <= '"+endTime+"'";
    }

    if(StringUtil.StringNoEmpty(vo.ifUse)){
        if("on".equals(vo.ifUse)){
            sql += " and a.if_use ='1' ";
        }else if("off".equals(vo.ifUse)){
            sql += " and a.if_use = '0'";
        }
    }

    RawSql rawSql = RawSqlBuilder.parse(sql).create();

    Query<ResultComputer> query = Ebean.find(ResultComputer.class).order("id");
    query.setRawSql(rawSql);

    Page<ResultComputer> page = query.findPagingList(10).setFetchAhead(false).getPage(0);
    Logger.debug(page.getTotalPageCount()+""); // result  1.

但我的数据库有一千条记录。我认为“page.getTotalPageCount()”应该是100页记录。我的错误在哪里?

1 个答案:

答案 0 :(得分:0)

public int getTotalPageCount();

返回总页数。由于您已经给出getPage(0),它将返回1. [getPage(0) - 返回给定页面位置的页面(从0开始)]。如果您想要总页数:请在getTotalPageCount();提取的pagingList上执行。