江陵:hibernate分页查询

Posted xjatj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了江陵:hibernate分页查询相关的知识,希望对你有一定的参考价值。

 public PageBean<VideoRmpPatrolJob> getPatrolJobPage(VideoRmpPatrolJobDTO dto, List<Integer> regionIDs, List<Map<String, Date>> dateMapList, Integer userId) {
        Integer pageNo = dto.getPageNo();
        Integer pageSize = dto.getPageSize();
        if (pageNo == 0) {
            pageNo = 1;
        }
        String listString = "(";
        if (regionIDs != null && regionIDs.size() > 0) {
            for (Integer regionID :
                    regionIDs) {
                listString += regionID + ",";
            }
            listString = listString.substring(0, listString.length() - 1) + ")";
        } else {
            listString = "";
        }

        StringBuffer querySql = new StringBuffer();
        querySql.append("SELECT
" +
                "	job.*
" +
                "FROM
" +
                "	video_rmp_patrol_job job
" +
                "LEFT JOIN s_region s ON job.region_id = s.region_id
" +
                "WHERE ");
        for (int i = 0; i < dateMapList.size(); i++) {
            if (i > 0) {
                querySql.append(" OR (");
            }
            querySql.append("	1 = 1
" +
                    "			AND job.circle_start_date >= ‘" +   RmpDateTimeUtil.dateToString( dateMapList.get(i).get(RmpReportConstant.START_TIME),RmpDateTimeUtil.DATA_FORMAT_yyyy_MM_dd_HH_mm_ss) + "‘
" +
                    "			AND job.circle_start_date <= ‘" + RmpDateTimeUtil.dateToString( dateMapList.get(i).get(RmpReportConstant.END_TIME),RmpDateTimeUtil.DATA_FORMAT_yyyy_MM_dd_HH_mm_ss) + "‘
" +
                    "			AND s.region_level = 0
" +
                    " and job.is_delete =0 ");
            if(userId!=null && userId!=4){
                querySql.append(" and job.patrol_person_id = "+userId);
            }
            if (StringUtils.isNotBlank(dto.getStoreName())) {
                querySql.append("   and s.name like ‘%" + dto.getStoreName() + "%‘ ESCAPE ‘/‘ ");
            }
            querySql.append("			AND job.region_id IN " + listString + "  " +
                    "  ");
            if (i > 0) {
                querySql.append("	) ");
            }

            querySql.append(" or(" +
                    "1=1" +
                    "			AND job.circle_end_date >= ‘" + RmpDateTimeUtil.dateToString( dateMapList.get(i).get(RmpReportConstant.START_TIME),RmpDateTimeUtil.DATA_FORMAT_yyyy_MM_dd_HH_mm_ss) + "‘
" +
                    "			AND job.circle_end_date <= ‘" + RmpDateTimeUtil.dateToString( dateMapList.get(i).get(RmpReportConstant.END_TIME),RmpDateTimeUtil.DATA_FORMAT_yyyy_MM_dd_HH_mm_ss) + "‘
" +
                    "			AND s.region_level = 0
" +
                    " and job.is_delete =0 ");
            if(userId!=null && userId!=4){
                querySql.append(" and job.patrol_person_id = "+userId);
            }
            if (StringUtils.isNotBlank(dto.getStoreName())) {
                querySql.append("  and s.name like ‘%" + dto.getStoreName() + "%‘ ESCAPE ‘/‘ ");
            }
            querySql.append("			AND job.region_id IN " + listString + "  " +
                    " ) ");


        }
        querySql.append(" order BY " +
                " circle_start_date LIMIT " + pageSize + " OFFSET " + (pageNo - 1) * pageSize);
        final String sql = querySql.toString();
        List<Object[]> list = (List<Object[]>) this.getHibernateTemplate().execute(new HibernateCallback() {
            @Override
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                SQLQuery query = session.createSQLQuery(sql);
                return query.list();
            }
        });

        ArrayList<VideoRmpPatrolJob> jobs = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (Object[] o : list) {
                VideoRmpPatrolJob job = new VideoRmpPatrolJob();
                job.setId((Integer) o[0]);
                job.setPatrolJobName((String) o[1]);
                job.setPlanId((Integer) o[2]);
                job.setCircleStartDate((Date) o[3]);
                job.setCircleEndDate((Date) o[4]);
                job.setPatrolPersonId((Integer) o[5]);
                job.setPatrolPersonName((String) o[6]);
                job.setStoreId((Integer) o[7]);
                job.setRegionId((Integer) o[8]);
                job.setStatus((Integer) o[9]);
                job.setCreateTime((Timestamp) o[10]);
                job.setUpdateTime((Timestamp) o[11]);
                job.setRemark((String) o[12]);
                job.setIsDelete((Integer) o[13]);
                jobs.add(job);
            }
        }

        //计算总数
        StringBuffer stringBuffer2 = new StringBuffer();
        stringBuffer2.append("SELECT
" +
                " count(*) " +
                "FROM
" +
                "	video_rmp_patrol_job job
" +
                "LEFT JOIN s_region s ON job.region_id = s.region_id
" +
                "WHERE ");
        for (int i = 0; i < dateMapList.size(); i++) {
            if (i > 0) {
                stringBuffer2.append(" OR (");
            }
            stringBuffer2.append("	1 = 1
" +
                    "			AND job.circle_start_date >= ‘" + RmpDateTimeUtil.dateToString( dateMapList.get(i).get(RmpReportConstant.START_TIME),RmpDateTimeUtil.DATA_FORMAT_yyyy_MM_dd_HH_mm_ss) + "‘
" +
                    "			AND job.circle_start_date <= ‘" + RmpDateTimeUtil.dateToString( dateMapList.get(i).get(RmpReportConstant.END_TIME),RmpDateTimeUtil.DATA_FORMAT_yyyy_MM_dd_HH_mm_ss) + "‘
" +
                    "			AND s.region_level = 0
" +
                    " and job.is_delete =0 ");
            if(userId!=null && userId!=4){
                stringBuffer2.append(" and job.patrol_person_id = "+userId);
            }
            if (StringUtils.isNotBlank(dto.getStoreName())) {
                stringBuffer2.append("  and s.name like ‘%" + dto.getStoreName() + "%‘ ESCAPE ‘/‘ ");
            }
            stringBuffer2.append("			AND job.region_id IN " + listString + "  " +
                    " ");
            if (i > 0) {
                stringBuffer2.append("	) ");
            }

            stringBuffer2.append(" or(" +
                    "1=1" +
                    "			AND job.circle_end_date >= ‘" + RmpDateTimeUtil.dateToString( dateMapList.get(i).get(RmpReportConstant.START_TIME),RmpDateTimeUtil.DATA_FORMAT_yyyy_MM_dd_HH_mm_ss) + "‘
" +
                    "			AND job.circle_end_date <= ‘" + RmpDateTimeUtil.dateToString( dateMapList.get(i).get(RmpReportConstant.END_TIME),RmpDateTimeUtil.DATA_FORMAT_yyyy_MM_dd_HH_mm_ss) + "‘
" +
                    "			AND s.region_level = 0
" +
                    " and job.is_delete =0 ");
            if(userId!=null && userId!=4){
                stringBuffer2.append(" and job.patrol_person_id = "+userId);
            }
            if (StringUtils.isNotBlank(dto.getStoreName())) {
                stringBuffer2.append("  and s.name like ‘%" + dto.getStoreName() + "%‘ ESCAPE ‘/‘ ");
            }
            stringBuffer2.append("			AND job.region_id IN " + listString + "  " +
                    " ) ");


        }


        final String sql2 = stringBuffer2.toString();
        Integer count = (Integer) this.getHibernateTemplate().execute(new HibernateCallback() {
            @Override
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                SQLQuery query = session.createSQLQuery(sql2);
                return Integer.valueOf(query.uniqueResult().toString());
            }
        });
        PageBean<VideoRmpPatrolJob> page = new PageBean<>(count, pageNo, pageSize, jobs);
        return page;

    }
  

 

以上是关于江陵:hibernate分页查询的主要内容,如果未能解决你的问题,请参考以下文章

springmvc4+hibernate4分页查询功能

hibernate的分页怎么用?

hibernate 的分页查询

hibernate_分页查询

hibernate做分页查询

Hibernate实现分页查询