public static void findList( String where, String orderby, List<Object> params, Page<Tuserinfo> page) { TypedQuery<Tuserinfo> q = Tuserinfo.entityManager() .createQuery("SELECT o FROM Tuserinfo o " + where + orderby, Tuserinfo.class); if (null != params && !params.isEmpty()) { int index = 1; for (Object param : params) { q.setParameter(index, param); index = index + 1; } } page.setList(q.getResultList()); TypedQuery<Long> totalQ = Tuserinfo.entityManager() .createQuery("select count(o) from Tuserinfo o " + where, Long.class); if (null != params && !params.isEmpty()) { int index = 1; for (Object param : params) { totalQ.setParameter(index, param); index = index + 1; } } page.setTotalResult(totalQ.getSingleResult().intValue()); }
public static Page<Tuserinfo> findTuserinfoesByPage(String queryParam, Page<Tuserinfo> page) { EntityManager em = Tuserinfo.entityManager(); String sql = "SELECT o FROM Tuserinfo o "; String countSql = "SELECT count(*) FROM Tuserinfo o "; StringBuilder whereSql = new StringBuilder(" WHERE 1=1 "); if (StringUtils.isNotBlank(queryParam)) { whereSql.append(" AND (userno like :queryParam1 OR username like :queryParam2 )"); } StringBuilder orderSql = new StringBuilder(" ORDER BY "); if (page.isOrderBySetted()) { orderSql.append(" " + page.getOrderBy() + " " + page.getOrderDir()); } else { orderSql.append(" userno ASC"); } String tsql = sql + whereSql.toString() + orderSql.toString(); String tCountSql = countSql + whereSql.toString(); TypedQuery<Tuserinfo> q = em.createQuery(tsql, Tuserinfo.class); TypedQuery<Long> total = em.createQuery(tCountSql, Long.class); if (StringUtils.isNotBlank(queryParam)) { q.setParameter("queryParam1", "%" + queryParam + "%"); total.setParameter("queryParam1", "%" + queryParam + "%"); q.setParameter("queryParam2", "%" + queryParam + "%"); total.setParameter("queryParam2", "%" + queryParam + "%"); } q.setFirstResult(page.getPageIndex()).setMaxResults(page.getMaxResult()); List<Tuserinfo> resultList = q.getResultList(); int count = total.getSingleResult().intValue(); page.setList(resultList); page.setTotalResult(count); return page; }