JPA entityManagerによるページングクエリーの実装
4893 ワード
@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;
}