JPA entityManager 实现 分页查询

@Override
	public PageModel queryForPager(int pageNumber, int pageSize,
			Class entityClass, String condition, String sort,HttpServletRequest request, Object... params) {
		EntityManager entityManager = getEntityManager(request,1);
		String schemaName = entityManager.getProperties().get("id")+"";
		
		PageModel page = new PageModel();
		String sqllist = SqlHelper.generateSelectSqlForPager(pageNumber,
				pageSize, entityClass, condition, sort,schemaName);
		
		String sqlcount = SqlHelper.generateSelectSqlForCount(entityClass,
				condition);
		
		if(params instanceof Object[]){
			sqllist = formatSql(sqllist,params);
			sqlcount = formatSql(sqlcount,params);
		}
		Query createNativeQuery = entityManager.createNativeQuery(SqlParserUtil.sqlParserForSelect(sqllist, schemaName));
		List resultList = createNativeQuery.getResultList();
		
		Query createNativeQuery2 = entityManager.createNativeQuery(SqlParserUtil.sqlParserForSelect(sqlcount, schemaName));
		List resultList2 = createNativeQuery2.getResultList();
		
		page.setList(resultList);
		page.setTotalRows(resultList2.size());
		page.setPageSize(pageSize);
		page.setPageNumber(pageNumber);
		page.setTotalPages((int) Math.ceil((double) page.getTotalRows()
				/ (double) pageSize));
		return page;
	}
	
	/*
	 * 参数转换
	 */
	private String formatSql(String sql, Object[] params) {
		Object[] arr = params;
		int i  =0;
		while(sql.contains("?")){
			sql = sql.replaceFirst("\\?", "'"+arr[i]+"'");;
			i++;
		}
		if(i!=arr.length){
			throw new RuntimeException("参数与替换参数 数目不相等");
		}
		return sql;
	}

	@Override
	public PageModel queryForPagerSQL(int pageNumber, int pageSize,
			Class entityClass, String condition, String sort, String sql,
			String sql1, HttpServletRequest request,Object... params) {
		
		EntityManager entityManager = getEntityManager(request,1);
		String schemaName = entityManager.getProperties().get("id")+"";
		
		
		PageModel page = new PageModel();
		String sqllist = sql + " where " + condition + " order by " + sort +" limit " + ((pageNumber - 1) * pageSize) + "," + pageSize;
		sqllist = SqlParserUtil.sqlParserForSelect(sqllist, schemaName);
		sqllist = "select (@i:=@i+1)AS i,t.* from ("+sqllist+") as t,(SELECT   @i:=0) AS it";
		
		String sqlcount = sql1 + " where " + condition;
		
		if(params instanceof Object[]){
			sqllist = formatSql(sqllist,params);
			sqlcount = formatSql(sqlcount, params);
		}
		Query createNativeQuery = entityManager.createNativeQuery(sqllist);
		List resultList = createNativeQuery.getResultList();
		
		
		Query createNativeQuery2 = entityManager.createNativeQuery(SqlParserUtil.sqlParserForSelect(sqlcount, schemaName));
		List resultList2 = createNativeQuery2.getResultList();
		
		page.setList(resultList);
		page.setTotalRows(resultList2.size());
		page.setPageSize(pageSize);
		page.setPageNumber(pageNumber);
		page.setTotalPages((int) Math.ceil((double) page.getTotalRows()
				/ (double) pageSize));
		return page;
	}

	@Override
	public PageModel queryForPagerSQLGroup(int pageNumber, int pageSize,
			Class entityClass, String condition, String sort, String sql,
			String sql1, HttpServletRequest request, Object... params) {
		EntityManager entityManager = getEntityManager(request,1);
		String schemaName = entityManager.getProperties().get("id")+"";
		
		PageModel page = new PageModel();
		String sqllist = sql + " where " + condition + " order by " + sort +" limit " + ((pageNumber - 1) * pageSize) + "," + pageSize;
		sqllist = SqlParserUtil.sqlParserForSelect(sqllist, schemaName);
		int a=((pageNumber - 1) * pageSize);
		sqllist = "select (@i:=@i+1)AS i,t.* from ("+sqllist+") as t,(SELECT   @i:="+a+") AS it";
		

		String sqlcount = sql1 + " where " + condition;
		sqlcount="Select Count(*) from ("+sqlcount+") t ";
		

		if(params instanceof Object[]){
			sqllist = formatSql(sqllist,params);
			sqlcount = formatSql(sqlcount, params);
		}
		Query createNativeQuery = entityManager.createNativeQuery(sqllist);
		List resultList = createNativeQuery.getResultList();
		
		
		Query createNativeQuery2 = entityManager.createNativeQuery(SqlParserUtil.sqlParserForSelect(sqlcount, schemaName));
		List resultList2 = createNativeQuery2.getResultList();
		page.setList(resultList);
		page.setTotalRows(resultList2.size());
		page.setPageSize(pageSize);
		page.setPageNumber(pageNumber);
		page.setTotalPages((int) Math.ceil((double) page.getTotalRows()
				/ (double) pageSize));
		return page;
	}


 
 

你可能感兴趣的:(JPA entityManager 实现 分页查询)