- 浏览: 36761 次
- 性别:
- 来自: 郑州
文章分类
最新评论
-
yekui:
把Liberarise 重新导,src等都从新倒 即可解决。
tomcat编译工程之后classes文件夹下没有文件或者缺失文件解决办法 -
w1113:
刚才也碰到你的这个问题,编译后的class文件发布不上去,最后 ...
tomcat编译工程之后classes文件夹下没有文件或者缺失文件解决办法 -
苏叶晚晚:
我也遇到了同样。。。无奈的问题。唉==
用楼主的奇思妙想的办法 ...
tomcat编译工程之后classes文件夹下没有文件或者缺失文件解决办法
public class Pager extends HibernateDaoSupport {
/** 日志加载* */
private static final Log log = LogFactory.getLog(Pager.class);
/**
* 父类初始化方法
*/
protected void initDao() {
// do nothing
}
/**
* 单个对象分页
*
* @param className
* 对象名
* @param pageHelper
* 分页属性
* @return List
* @throws Exception
* @since Jul 14, 2009
*/
@SuppressWarnings("unchecked")
public List findAllByPage(String className, PageHelper pageHelper)
throws Exception {
log.debug("finding Object instance by page");
try {
int firstResult = pageHelper.getFirstResult();
int maxResults = pageHelper.getMaxResults();
Criteria c = this.getSession().createCriteria(
Class.forName(className));
if (pageHelper.isAutoCount()) {
pageHelper.setTotalRows(this.findAllCount(className));
}
c.setFirstResult(firstResult);
c.setMaxResults(maxResults);
return c.list();
} catch (Exception e) {
log.error("find Objcet by page failed", e);
throw e;
}
}
/**
* 单个对象分页,单个属性排序
*
* @param className
* 对象名
* @param pageHelper
* 分页属性
* @return List
* @throws Exception
* @since Jul 14, 2009
*/
@SuppressWarnings("unchecked")
public List findAllByPageAndOrder(String className, PageHelper pageHelper,
String propertyName, boolean ascending) throws Exception {
log.debug("finding Object instance by page and order");
try {
int firstResult = pageHelper.getFirstResult();
int maxResults = pageHelper.getMaxResults();
Criteria c = this.getSession().createCriteria(
Class.forName(className)).addOrder(
ascending ? Order.asc(propertyName) : Order
.desc(propertyName));
if (pageHelper.isAutoCount()) {
pageHelper.setTotalRows(this.findAllCount(className));
}
c.setFirstResult(firstResult);
c.setMaxResults(maxResults);
return c.list();
} catch (Exception e) {
log.error("find Objcet by page and order failed", e);
throw e;
}
}
/**
* 单个对象分页,多个属性排序
*
* @param className
* 对象名
* @param pageHelper
* 分页属性
* @param orders
* Map名key为属性名,value为是否正序排名
* @return List
* @throws Exception
* @since Jul 17, 2009
*/
@SuppressWarnings("unchecked")
public List findAllByPageAndOrders(String className, PageHelper pageHelper,
Map<String, Boolean> orders) throws Exception {
log.debug("finding Object instance by page and orders");
try {
int firstResult = pageHelper.getFirstResult();
int maxResults = pageHelper.getMaxResults();
Criteria c = this.getSession().createCriteria(
Class.forName(className));
if (pageHelper.isAutoCount()) {
pageHelper.setTotalRows(this.findAllCount(className));
}
if (orders != null && !orders.isEmpty()) {
Iterator ordersItr = orders.keySet().iterator();
while (ordersItr.hasNext()) {
String propertyName = ordersItr.next().toString();
boolean ascending = orders.get(propertyName);
if (ascending) {
c.addOrder(Order.asc(propertyName));
} else {
c.addOrder(Order.desc(propertyName));
}
}
}
c.setFirstResult(firstResult);
c.setMaxResults(maxResults);
return c.list();
} catch (Exception e) {
log.error("find Objcet by page and orders failed", e);
throw e;
}
}
/**
* 单个对象分页,sql查询
*
* @param className
* 对象名
* @param pageHelper
* 分页属性
* @param where
* where条件
* @return List
* @throws Exception
* @since Jul 17, 2009
*/
@SuppressWarnings("unchecked")
public List findAllByPageAndWhere(String className, PageHelper pageHelper,
String where) throws Exception {
log.debug("finding Object instance by page and sql");
try {
int firstResult = pageHelper.getFirstResult();
int maxResults = pageHelper.getMaxResults();
Criteria c = this.getSession().createCriteria(
Class.forName(className));
c.add(Restrictions.sqlRestriction(where));
if (pageHelper.isAutoCount()) {
pageHelper.setTotalRows(this.findAllCountByWhere(className,
where));
}
c.setFirstResult(firstResult);
c.setMaxResults(maxResults);
return c.list();
} catch (Exception e) {
log.error("find Objcet by page and sql failed", e);
throw e;
}
}
/**
* 单个对象分页,�?��对象属性条件查询
*
* @param className
* 对象名
* @param pageHelper
* 分页属性
* @param example
* Object对象,与classsName对应的对象
* @return List
* @throws Exception
* @since Jul 17, 2009
*/
@SuppressWarnings("unchecked")
public List findAllByPageAndExample(String className,
PageHelper pageHelper, Object example) throws Exception {
log.debug("finding Object instance by page and example");
try {
int firstResult = pageHelper.getFirstResult();
int maxResults = pageHelper.getMaxResults();
Criteria c = this.getSession().createCriteria(
Class.forName(className));
c.add(Example.create(Class.forName(className).cast(example)));
if (pageHelper.isAutoCount()) {
pageHelper.setTotalRows(this.findAllCountByExample(className,
pageHelper, example));
}
c.setFirstResult(firstResult);
c.setMaxResults(maxResults);
return c.list();
} catch (Exception e) {
log.error("find Objcet by page and example failed", e);
throw e;
}
}
/**
* 单个对象分页,�?��对象属性条件查询,单属性排序
*
* @param className
* 对象名
* @param pageHelper
* 分页属性
* @param example
* Object对象,与classsName对应的对象
* @param propertyName
* 要排序的属性名
* @param ascending
* 是否正序排列
* @return List
* @throws Exception
* @since Jul 17, 2009
*/
@SuppressWarnings("unchecked")
public List findAllByPageAndExampleAndOrder(String className,
PageHelper pageHelper, Object example, String propertyName,
boolean ascending) throws Exception {
log.debug("finding Object instance by page and example and order");
try {
int firstResult = pageHelper.getFirstResult();
int maxResults = pageHelper.getMaxResults();
Criteria c = this.getSession().createCriteria(
Class.forName(className));
c.add(Example.create(Class.forName(className).cast(example)));
if (pageHelper.isAutoCount()) {
pageHelper.setTotalRows(this.findAllCountByExample(className,
pageHelper, example));
}
c.addOrder(ascending ? Order.asc(propertyName) : Order
.desc(propertyName));
c.setFirstResult(firstResult);
c.setMaxResults(maxResults);
return c.list();
} catch (Exception e) {
log.error("find Objcet by page and example and order failed", e);
throw e;
}
}
/**
* 单个对象分页,单个对象属性条件查询,多属性排序
*
* @param className
* 对象名
* @param pageHelper
* 分页属性
* @param example
* Object对象,与classsName对应的对象
* @param orders
* Map对象,key为要排序的属性名,value为是否正序排序
* @return List
* @throws Exception
* @since Jul 17, 2009
*/
@SuppressWarnings("unchecked")
public List findAllByPageAndExampleAndOrders(String className,
PageHelper pageHelper, Object example, Map<String, Boolean> orders)
throws Exception {
log.debug("finding Object instance by page and example and orders");
try {
int firstResult = pageHelper.getFirstResult();
int maxResults = pageHelper.getMaxResults();
Criteria c = this.getSession().createCriteria(
Class.forName(className));
c.add(Example.create(Class.forName(className).cast(example)));
if (pageHelper.isAutoCount()) {
pageHelper.setTotalRows(this.findAllCountByExample(className,
pageHelper, example));
}
if (orders != null && !orders.isEmpty()) {
Iterator ordersItr = orders.keySet().iterator();
while (ordersItr.hasNext()) {
String propertyName = ordersItr.next().toString();
boolean ascending = orders.get(propertyName);
if (ascending) {
c.addOrder(Order.asc(propertyName));
} else {
c.addOrder(Order.desc(propertyName));
}
}
}
c.setFirstResult(firstResult);
c.setMaxResults(maxResults);
return c.list();
} catch (Exception e) {
log.error("find Objcet by page and example and orders failed", e);
throw e;
}
}
/**
* 单个对象分页,hql查询,单属性排序
*
* @param className
* 对象名
* @param pageHelper
* 分页属性
* @param where
* where条件
* @param propertyName
* 要排序的属性名称
* @param ascending
* 是否正序排列
* @return List
* @throws Exception
* @since Jul 17, 2009
*/
@SuppressWarnings("unchecked")
public List findAllByPageAndWhereAndOrder(String className,
PageHelper pageHelper, String where, String propertyName,
boolean ascending) throws Exception {
log.debug("finding Object instance by page and sql and order");
try {
int firstResult = pageHelper.getFirstResult();
int maxResults = pageHelper.getMaxResults();
Criteria c = this.getSession().createCriteria(
Class.forName(className));
c.add(Restrictions.sqlRestriction(where));
if (pageHelper.isAutoCount()) {
pageHelper.setTotalRows(this.findAllCountByWhere(className,
where));
}
c.addOrder(ascending ? Order.asc(propertyName) : Order
.desc(propertyName));
c.setFirstResult(firstResult);
c.setMaxResults(maxResults);
return c.list();
} catch (Exception e) {
log.error("find Objcet by page and sql and order failed", e);
throw e;
}
}
/**
* 根据hql语句查询记录
*
* @param pageHelper
* @param queryString
* @return List
* @throws Exception
* @since Feb 26, 2010
*/
@SuppressWarnings("unchecked")
public List findAllByPageAndSql(PageHelper pageHelper, String queryString)
throws Exception {
log.debug("finding Object by page and sql");
try {
int firstResult = pageHelper.getFirstResult();
int maxResults = pageHelper.getMaxResults();
Query query = this.getSession().createQuery(queryString);
if (pageHelper.isAutoCount()) {
pageHelper.setTotalRows(this.findAllCountBySql(pageHelper,
queryString));
}
query.setFirstResult(firstResult);
query.setMaxResults(maxResults);
return query.list();
} catch (Exception e) {
log.error("find Objcet by page and sql failed", e);
throw e;
}
}
/**
* 单个对象分页,hql查询,多属性排序
*
* @param className
* 对象名
* @param pageHelper
* 分页属性
* @param where
* where条件
* @param orders
* Map对象,key要排序的属性名称,value是否正序排列
* @return List
* @throws Exception
* @since Jul 17, 2009
*/
@SuppressWarnings("unchecked")
public List findAllByPageAndWhereAndOrders(String className,
PageHelper pageHelper, String where, Map<String, Boolean> orders)
throws Exception {
log.debug("finding Object instance by page and sql and orders");
try {
int firstResult = pageHelper.getFirstResult();
int maxResults = pageHelper.getMaxResults();
Criteria c = this.getSession().createCriteria(
Class.forName(className));
c.add(Restrictions.sqlRestriction(where));
if (pageHelper.isAutoCount()) {
pageHelper.setTotalRows(this.findAllCountByWhere(className,
where));
}
if (orders != null && !orders.isEmpty()) {
Iterator ordersItr = orders.keySet().iterator();
while (ordersItr.hasNext()) {
String propertyName = ordersItr.next().toString();
boolean ascending = orders.get(propertyName);
if (ascending) {
c.addOrder(Order.asc(propertyName));
} else {
c.addOrder(Order.desc(propertyName));
}
}
}
c.setFirstResult(firstResult);
c.setMaxResults(maxResults);
return c.list();
} catch (Exception e) {
log.error("find Objcet by page and sql and orders failed", e);
throw e;
}
}
/**
* 条件查询分页
*
* @param pageHelper
* 分页属性
* @param c
* Criteria
* @return List
* @throws Exception
* @since Jul 14, 2009
*/
@SuppressWarnings("unchecked")
public List findByPageAndCriteria(PageHelper pageHelper, Criteria c)
throws Exception {
log.debug("finding Object instance by page and criteria");
try {
int firstResult = pageHelper.getFirstResult();
int maxResults = pageHelper.getMaxResults();
if (pageHelper.isAutoCount()) {
pageHelper.setTotalRows(this.findCountByCriteria(pageHelper, c));
}
c.setFirstResult(firstResult);
c.setMaxResults(maxResults);
return c.list();
} catch (Exception e) {
log.error("find Object by page and criteria failed", e);
throw e;
}
}
/**
* 查询记录总数
*
* @param className
* @return int
* @throws Exception
* @since Feb 26, 2010
*/
public int findAllCount(String className) throws Exception {
try {
Criteria c = this.getSession().createCriteria(
Class.forName(className));
c.setProjection(Projections.rowCount());
return Integer.parseInt(c.uniqueResult().toString());
} catch (Exception e) {
log.error("find all count failed", e);
throw e;
}
}
/**
* 根据where条件查询记录总数
*
* @param className
* @param where
* @return int
* @throws Exception
* @since Feb 26, 2010
*/
public int findAllCountByWhere(String className, String where)
throws Exception {
try {
Criteria c = this.getSession().createCriteria(
Class.forName(className));
c.add(Restrictions.sqlRestriction(where));
c.setProjection(Projections.rowCount());
return Integer.parseInt(c.uniqueResult().toString());
} catch (Exception e) {
log.error("find Objcet by page and sql failed", e);
throw e;
}
}
/**
* 根据对象查询记录总数
*
* @param className
* @param pageHelper
* @param example
* @return int
* @throws Exception
* @since Feb 26, 2010
*/
public int findAllCountByExample(String className, PageHelper pageHelper,
Object example) throws Exception {
try {
Criteria c = this.getSession().createCriteria(
Class.forName(className));
c.add(Example.create(Class.forName(className).cast(example)));
c.setProjection(Projections.rowCount());
return Integer.parseInt(c.uniqueResult().toString());
} catch (Exception e) {
log.error("find Objcet by page and example failed", e);
throw e;
}
}
/**
* 根据hql语句查询记录总数
*
* @param pageHelper
* @param queryString
* @return int
* @throws Exception
* @since Feb 26, 2010
*/
public int findAllCountBySql(PageHelper pageHelper, String queryString)
throws Exception {
log.debug("finding Object by page and sql");
try {
Query query = this.getSession().createQuery(queryString);
return query.list().size();
} catch (Exception e) {
log.error("find Objcet by page and sql failed", e);
throw e;
}
}
/**
* 根据Criteria查询记录总数
* @param pageHelper
* @param c
* @return int
* @throws Exception
* @since Feb 26, 2010
*/
@SuppressWarnings("unchecked")
public int findCountByCriteria(PageHelper pageHelper, Criteria c)
throws Exception {
try {
CriteriaImpl impl = (CriteriaImpl) c;
// 先把Projection、ResultTransformer、OrderBy取出�?清空三�?后再执行Count操作
Projection projection = impl.getProjection();
ResultTransformer transformer = impl.getResultTransformer();
List<CriteriaImpl.OrderEntry> orderEntries = null;
try {
orderEntries = (List) ReflectionUtils.getFieldValue(impl, "orderEntries");
ReflectionUtils.setFieldValue(impl, "orderEntries", new ArrayList());
} catch (Exception e) {
log.error("不可能抛出的异常:{}", e);
}
// 执行Count查询
int totalCount = (Integer) c.setProjection(Projections.rowCount()).uniqueResult();
// 将之前的Projection,ResultTransformer和OrderBy条件重新设回
c.setProjection(projection);
if (projection == null) {
c.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
}
if (transformer != null) {
c.setResultTransformer(transformer);
}
try {
ReflectionUtils.setFieldValue(impl, "orderEntries", orderEntries);
} catch (Exception e) {
log.error("不可能抛出的异常:{}", e);
}
return totalCount;
} catch (Exception e) {
log.error("find Object by page and criteria failed", e);
throw e;
}
}
/**
* 获取分页session中的Criteria对象
*
* @param className
* @return Criteria
* @since Jul 21, 2009
*/
public Criteria getCriteria(String className) {
log.debug("getting Criteria");
try {
return this.getSession().createCriteria(Class.forName(className));
} catch (DataAccessResourceFailureException e) {
log.error("get Criteria failed", e);
return null;
} catch (IllegalStateException e) {
log.error("get Criteria failed", e);
return null;
} catch (ClassNotFoundException e) {
log.error("get Criteria failed", e);
return null;
}
}
/**
* 通过xml获取注入的bean
*
* @param ctx
* @return Pager
* @since Jul 14, 2009
*/
public static Pager getFromApplicationContext(ApplicationContext ctx) {
return (Pager) ctx.getBean("Pager");
}
}
/** 日志加载* */
private static final Log log = LogFactory.getLog(Pager.class);
/**
* 父类初始化方法
*/
protected void initDao() {
// do nothing
}
/**
* 单个对象分页
*
* @param className
* 对象名
* @param pageHelper
* 分页属性
* @return List
* @throws Exception
* @since Jul 14, 2009
*/
@SuppressWarnings("unchecked")
public List findAllByPage(String className, PageHelper pageHelper)
throws Exception {
log.debug("finding Object instance by page");
try {
int firstResult = pageHelper.getFirstResult();
int maxResults = pageHelper.getMaxResults();
Criteria c = this.getSession().createCriteria(
Class.forName(className));
if (pageHelper.isAutoCount()) {
pageHelper.setTotalRows(this.findAllCount(className));
}
c.setFirstResult(firstResult);
c.setMaxResults(maxResults);
return c.list();
} catch (Exception e) {
log.error("find Objcet by page failed", e);
throw e;
}
}
/**
* 单个对象分页,单个属性排序
*
* @param className
* 对象名
* @param pageHelper
* 分页属性
* @return List
* @throws Exception
* @since Jul 14, 2009
*/
@SuppressWarnings("unchecked")
public List findAllByPageAndOrder(String className, PageHelper pageHelper,
String propertyName, boolean ascending) throws Exception {
log.debug("finding Object instance by page and order");
try {
int firstResult = pageHelper.getFirstResult();
int maxResults = pageHelper.getMaxResults();
Criteria c = this.getSession().createCriteria(
Class.forName(className)).addOrder(
ascending ? Order.asc(propertyName) : Order
.desc(propertyName));
if (pageHelper.isAutoCount()) {
pageHelper.setTotalRows(this.findAllCount(className));
}
c.setFirstResult(firstResult);
c.setMaxResults(maxResults);
return c.list();
} catch (Exception e) {
log.error("find Objcet by page and order failed", e);
throw e;
}
}
/**
* 单个对象分页,多个属性排序
*
* @param className
* 对象名
* @param pageHelper
* 分页属性
* @param orders
* Map名key为属性名,value为是否正序排名
* @return List
* @throws Exception
* @since Jul 17, 2009
*/
@SuppressWarnings("unchecked")
public List findAllByPageAndOrders(String className, PageHelper pageHelper,
Map<String, Boolean> orders) throws Exception {
log.debug("finding Object instance by page and orders");
try {
int firstResult = pageHelper.getFirstResult();
int maxResults = pageHelper.getMaxResults();
Criteria c = this.getSession().createCriteria(
Class.forName(className));
if (pageHelper.isAutoCount()) {
pageHelper.setTotalRows(this.findAllCount(className));
}
if (orders != null && !orders.isEmpty()) {
Iterator ordersItr = orders.keySet().iterator();
while (ordersItr.hasNext()) {
String propertyName = ordersItr.next().toString();
boolean ascending = orders.get(propertyName);
if (ascending) {
c.addOrder(Order.asc(propertyName));
} else {
c.addOrder(Order.desc(propertyName));
}
}
}
c.setFirstResult(firstResult);
c.setMaxResults(maxResults);
return c.list();
} catch (Exception e) {
log.error("find Objcet by page and orders failed", e);
throw e;
}
}
/**
* 单个对象分页,sql查询
*
* @param className
* 对象名
* @param pageHelper
* 分页属性
* @param where
* where条件
* @return List
* @throws Exception
* @since Jul 17, 2009
*/
@SuppressWarnings("unchecked")
public List findAllByPageAndWhere(String className, PageHelper pageHelper,
String where) throws Exception {
log.debug("finding Object instance by page and sql");
try {
int firstResult = pageHelper.getFirstResult();
int maxResults = pageHelper.getMaxResults();
Criteria c = this.getSession().createCriteria(
Class.forName(className));
c.add(Restrictions.sqlRestriction(where));
if (pageHelper.isAutoCount()) {
pageHelper.setTotalRows(this.findAllCountByWhere(className,
where));
}
c.setFirstResult(firstResult);
c.setMaxResults(maxResults);
return c.list();
} catch (Exception e) {
log.error("find Objcet by page and sql failed", e);
throw e;
}
}
/**
* 单个对象分页,�?��对象属性条件查询
*
* @param className
* 对象名
* @param pageHelper
* 分页属性
* @param example
* Object对象,与classsName对应的对象
* @return List
* @throws Exception
* @since Jul 17, 2009
*/
@SuppressWarnings("unchecked")
public List findAllByPageAndExample(String className,
PageHelper pageHelper, Object example) throws Exception {
log.debug("finding Object instance by page and example");
try {
int firstResult = pageHelper.getFirstResult();
int maxResults = pageHelper.getMaxResults();
Criteria c = this.getSession().createCriteria(
Class.forName(className));
c.add(Example.create(Class.forName(className).cast(example)));
if (pageHelper.isAutoCount()) {
pageHelper.setTotalRows(this.findAllCountByExample(className,
pageHelper, example));
}
c.setFirstResult(firstResult);
c.setMaxResults(maxResults);
return c.list();
} catch (Exception e) {
log.error("find Objcet by page and example failed", e);
throw e;
}
}
/**
* 单个对象分页,�?��对象属性条件查询,单属性排序
*
* @param className
* 对象名
* @param pageHelper
* 分页属性
* @param example
* Object对象,与classsName对应的对象
* @param propertyName
* 要排序的属性名
* @param ascending
* 是否正序排列
* @return List
* @throws Exception
* @since Jul 17, 2009
*/
@SuppressWarnings("unchecked")
public List findAllByPageAndExampleAndOrder(String className,
PageHelper pageHelper, Object example, String propertyName,
boolean ascending) throws Exception {
log.debug("finding Object instance by page and example and order");
try {
int firstResult = pageHelper.getFirstResult();
int maxResults = pageHelper.getMaxResults();
Criteria c = this.getSession().createCriteria(
Class.forName(className));
c.add(Example.create(Class.forName(className).cast(example)));
if (pageHelper.isAutoCount()) {
pageHelper.setTotalRows(this.findAllCountByExample(className,
pageHelper, example));
}
c.addOrder(ascending ? Order.asc(propertyName) : Order
.desc(propertyName));
c.setFirstResult(firstResult);
c.setMaxResults(maxResults);
return c.list();
} catch (Exception e) {
log.error("find Objcet by page and example and order failed", e);
throw e;
}
}
/**
* 单个对象分页,单个对象属性条件查询,多属性排序
*
* @param className
* 对象名
* @param pageHelper
* 分页属性
* @param example
* Object对象,与classsName对应的对象
* @param orders
* Map对象,key为要排序的属性名,value为是否正序排序
* @return List
* @throws Exception
* @since Jul 17, 2009
*/
@SuppressWarnings("unchecked")
public List findAllByPageAndExampleAndOrders(String className,
PageHelper pageHelper, Object example, Map<String, Boolean> orders)
throws Exception {
log.debug("finding Object instance by page and example and orders");
try {
int firstResult = pageHelper.getFirstResult();
int maxResults = pageHelper.getMaxResults();
Criteria c = this.getSession().createCriteria(
Class.forName(className));
c.add(Example.create(Class.forName(className).cast(example)));
if (pageHelper.isAutoCount()) {
pageHelper.setTotalRows(this.findAllCountByExample(className,
pageHelper, example));
}
if (orders != null && !orders.isEmpty()) {
Iterator ordersItr = orders.keySet().iterator();
while (ordersItr.hasNext()) {
String propertyName = ordersItr.next().toString();
boolean ascending = orders.get(propertyName);
if (ascending) {
c.addOrder(Order.asc(propertyName));
} else {
c.addOrder(Order.desc(propertyName));
}
}
}
c.setFirstResult(firstResult);
c.setMaxResults(maxResults);
return c.list();
} catch (Exception e) {
log.error("find Objcet by page and example and orders failed", e);
throw e;
}
}
/**
* 单个对象分页,hql查询,单属性排序
*
* @param className
* 对象名
* @param pageHelper
* 分页属性
* @param where
* where条件
* @param propertyName
* 要排序的属性名称
* @param ascending
* 是否正序排列
* @return List
* @throws Exception
* @since Jul 17, 2009
*/
@SuppressWarnings("unchecked")
public List findAllByPageAndWhereAndOrder(String className,
PageHelper pageHelper, String where, String propertyName,
boolean ascending) throws Exception {
log.debug("finding Object instance by page and sql and order");
try {
int firstResult = pageHelper.getFirstResult();
int maxResults = pageHelper.getMaxResults();
Criteria c = this.getSession().createCriteria(
Class.forName(className));
c.add(Restrictions.sqlRestriction(where));
if (pageHelper.isAutoCount()) {
pageHelper.setTotalRows(this.findAllCountByWhere(className,
where));
}
c.addOrder(ascending ? Order.asc(propertyName) : Order
.desc(propertyName));
c.setFirstResult(firstResult);
c.setMaxResults(maxResults);
return c.list();
} catch (Exception e) {
log.error("find Objcet by page and sql and order failed", e);
throw e;
}
}
/**
* 根据hql语句查询记录
*
* @param pageHelper
* @param queryString
* @return List
* @throws Exception
* @since Feb 26, 2010
*/
@SuppressWarnings("unchecked")
public List findAllByPageAndSql(PageHelper pageHelper, String queryString)
throws Exception {
log.debug("finding Object by page and sql");
try {
int firstResult = pageHelper.getFirstResult();
int maxResults = pageHelper.getMaxResults();
Query query = this.getSession().createQuery(queryString);
if (pageHelper.isAutoCount()) {
pageHelper.setTotalRows(this.findAllCountBySql(pageHelper,
queryString));
}
query.setFirstResult(firstResult);
query.setMaxResults(maxResults);
return query.list();
} catch (Exception e) {
log.error("find Objcet by page and sql failed", e);
throw e;
}
}
/**
* 单个对象分页,hql查询,多属性排序
*
* @param className
* 对象名
* @param pageHelper
* 分页属性
* @param where
* where条件
* @param orders
* Map对象,key要排序的属性名称,value是否正序排列
* @return List
* @throws Exception
* @since Jul 17, 2009
*/
@SuppressWarnings("unchecked")
public List findAllByPageAndWhereAndOrders(String className,
PageHelper pageHelper, String where, Map<String, Boolean> orders)
throws Exception {
log.debug("finding Object instance by page and sql and orders");
try {
int firstResult = pageHelper.getFirstResult();
int maxResults = pageHelper.getMaxResults();
Criteria c = this.getSession().createCriteria(
Class.forName(className));
c.add(Restrictions.sqlRestriction(where));
if (pageHelper.isAutoCount()) {
pageHelper.setTotalRows(this.findAllCountByWhere(className,
where));
}
if (orders != null && !orders.isEmpty()) {
Iterator ordersItr = orders.keySet().iterator();
while (ordersItr.hasNext()) {
String propertyName = ordersItr.next().toString();
boolean ascending = orders.get(propertyName);
if (ascending) {
c.addOrder(Order.asc(propertyName));
} else {
c.addOrder(Order.desc(propertyName));
}
}
}
c.setFirstResult(firstResult);
c.setMaxResults(maxResults);
return c.list();
} catch (Exception e) {
log.error("find Objcet by page and sql and orders failed", e);
throw e;
}
}
/**
* 条件查询分页
*
* @param pageHelper
* 分页属性
* @param c
* Criteria
* @return List
* @throws Exception
* @since Jul 14, 2009
*/
@SuppressWarnings("unchecked")
public List findByPageAndCriteria(PageHelper pageHelper, Criteria c)
throws Exception {
log.debug("finding Object instance by page and criteria");
try {
int firstResult = pageHelper.getFirstResult();
int maxResults = pageHelper.getMaxResults();
if (pageHelper.isAutoCount()) {
pageHelper.setTotalRows(this.findCountByCriteria(pageHelper, c));
}
c.setFirstResult(firstResult);
c.setMaxResults(maxResults);
return c.list();
} catch (Exception e) {
log.error("find Object by page and criteria failed", e);
throw e;
}
}
/**
* 查询记录总数
*
* @param className
* @return int
* @throws Exception
* @since Feb 26, 2010
*/
public int findAllCount(String className) throws Exception {
try {
Criteria c = this.getSession().createCriteria(
Class.forName(className));
c.setProjection(Projections.rowCount());
return Integer.parseInt(c.uniqueResult().toString());
} catch (Exception e) {
log.error("find all count failed", e);
throw e;
}
}
/**
* 根据where条件查询记录总数
*
* @param className
* @param where
* @return int
* @throws Exception
* @since Feb 26, 2010
*/
public int findAllCountByWhere(String className, String where)
throws Exception {
try {
Criteria c = this.getSession().createCriteria(
Class.forName(className));
c.add(Restrictions.sqlRestriction(where));
c.setProjection(Projections.rowCount());
return Integer.parseInt(c.uniqueResult().toString());
} catch (Exception e) {
log.error("find Objcet by page and sql failed", e);
throw e;
}
}
/**
* 根据对象查询记录总数
*
* @param className
* @param pageHelper
* @param example
* @return int
* @throws Exception
* @since Feb 26, 2010
*/
public int findAllCountByExample(String className, PageHelper pageHelper,
Object example) throws Exception {
try {
Criteria c = this.getSession().createCriteria(
Class.forName(className));
c.add(Example.create(Class.forName(className).cast(example)));
c.setProjection(Projections.rowCount());
return Integer.parseInt(c.uniqueResult().toString());
} catch (Exception e) {
log.error("find Objcet by page and example failed", e);
throw e;
}
}
/**
* 根据hql语句查询记录总数
*
* @param pageHelper
* @param queryString
* @return int
* @throws Exception
* @since Feb 26, 2010
*/
public int findAllCountBySql(PageHelper pageHelper, String queryString)
throws Exception {
log.debug("finding Object by page and sql");
try {
Query query = this.getSession().createQuery(queryString);
return query.list().size();
} catch (Exception e) {
log.error("find Objcet by page and sql failed", e);
throw e;
}
}
/**
* 根据Criteria查询记录总数
* @param pageHelper
* @param c
* @return int
* @throws Exception
* @since Feb 26, 2010
*/
@SuppressWarnings("unchecked")
public int findCountByCriteria(PageHelper pageHelper, Criteria c)
throws Exception {
try {
CriteriaImpl impl = (CriteriaImpl) c;
// 先把Projection、ResultTransformer、OrderBy取出�?清空三�?后再执行Count操作
Projection projection = impl.getProjection();
ResultTransformer transformer = impl.getResultTransformer();
List<CriteriaImpl.OrderEntry> orderEntries = null;
try {
orderEntries = (List) ReflectionUtils.getFieldValue(impl, "orderEntries");
ReflectionUtils.setFieldValue(impl, "orderEntries", new ArrayList());
} catch (Exception e) {
log.error("不可能抛出的异常:{}", e);
}
// 执行Count查询
int totalCount = (Integer) c.setProjection(Projections.rowCount()).uniqueResult();
// 将之前的Projection,ResultTransformer和OrderBy条件重新设回
c.setProjection(projection);
if (projection == null) {
c.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
}
if (transformer != null) {
c.setResultTransformer(transformer);
}
try {
ReflectionUtils.setFieldValue(impl, "orderEntries", orderEntries);
} catch (Exception e) {
log.error("不可能抛出的异常:{}", e);
}
return totalCount;
} catch (Exception e) {
log.error("find Object by page and criteria failed", e);
throw e;
}
}
/**
* 获取分页session中的Criteria对象
*
* @param className
* @return Criteria
* @since Jul 21, 2009
*/
public Criteria getCriteria(String className) {
log.debug("getting Criteria");
try {
return this.getSession().createCriteria(Class.forName(className));
} catch (DataAccessResourceFailureException e) {
log.error("get Criteria failed", e);
return null;
} catch (IllegalStateException e) {
log.error("get Criteria failed", e);
return null;
} catch (ClassNotFoundException e) {
log.error("get Criteria failed", e);
return null;
}
}
/**
* 通过xml获取注入的bean
*
* @param ctx
* @return Pager
* @since Jul 14, 2009
*/
public static Pager getFromApplicationContext(ApplicationContext ctx) {
return (Pager) ctx.getBean("Pager");
}
}
发表评论
-
经典sql语句大全
2011-03-07 17:42 902一、基础 1、说明:创建数据库CREATE DATABA ... -
Java面向对象设计最佳实践
2011-03-07 17:40 813Java面向对象设计最佳实践 内置类设计 从这篇文 ... -
Spring AOP处理日志
2011-03-07 17:39 1246Spring AOP处理日志 AOP正 ... -
验证码相关2
2011-03-07 16:57 691登录的jsp页面: <%@ page language= ... -
验证码相关
2011-03-07 16:55 846public class RandomNumUtil { ... -
LoginInterceptor (用户登录拦截器类)
2011-03-07 16:54 1868public class LoginInterceptor e ... -
PageHelper (分页导航类,根据当前页计算导航链接)
2011-03-07 16:54 2653public class PageHelper { ... -
FileTool (文件工具类)
2011-03-07 16:51 1652public class FileTool { ... -
CustomActionSupport (继承ActionSupport的类)
2011-03-07 16:50 813public class CustomActionSuppor ... -
StringTool(字符串工具类)
2011-03-07 16:49 1400public class StringTool { ... -
DateFormatTool(日期格式和字符串格之间转换类,日期转换器用到此类)
2011-03-07 16:49 1959public class DateFormatTool { ... -
LongConvert(Long类型转换器)
2011-03-07 16:48 967public class LongConvert extend ... -
IntConvert(整数类型转换器)
2011-03-07 16:47 829public class IntConvert extends ... -
DateConvert(日期类型转换器)
2011-03-07 16:47 1749public class DateConvert extend ... -
Xwork-conversion.properties资源文件全文
2011-03-07 16:46 1147java.sql.Timestamp=com.wyt.comm ... -
Struts.priperties配置文件全文
2011-03-07 16:46 790struts.objectFactory=spring str ... -
SSH2整合过程中出现的错误及解决方法
2011-03-07 16:45 1109老是报,ERROR [main] (Conte ... -
SSH2整合的顺序
2011-03-07 16:44 10361. 添加Spring 2.0的Libraries 选择 ... -
log4j.properties资源文件全文
2011-03-07 16:43 689log4j.rootLogger=INFO,ERROR,con ... -
init.properties资源文件全文
2011-03-07 16:42 1532datasource.type=mssql datasourc ...
相关推荐
"公共的Pager分页实体类"是一个设计用于简化分页操作的Java类,它封装了处理分页逻辑所需的一些基本功能,使得开发者能够快速集成到自己的项目中,而无需从头编写这些基础代码。 Pager类通常包含以下关键组成部分:...
标题中的“Pager(通过的分页工具类)”显然指的是一个用于实现分页功能的Java类。在这个类中,开发者通常会封装分页操作的相关逻辑,以便在各种场景下复用。现在我们来详细探讨一下分页的基本概念、设计原则以及...
在处理大量数据时,为提高性能,Kin_Db_Pager类可能会采用“分页查询”技术,即只从数据库中取出当前页所需的数据,而不是一次性获取所有数据。此外,还可以通过缓存总记录数来避免频繁计算,从而减少数据库交互...
"Kin_Db_Pager"是一个通用的ASP分页类,它为开发者提供了便捷的方式来实现数据的分页显示。 首先,让我们了解一下什么是分页类。在编程中,分页类是一个预先编写好的代码模块,它可以处理数据库查询、计算总页数、...
这通常涉及到数据库查询,如使用Spring Data JPA或MyBatis进行分页查询。例如,你可以创建一个服务方法,返回带有当前页码、每页大小、总记录数和实际数据的Page对象。 在JSP页面上使用`pager:pagelist`和`pager:...
1. 将Pager-taglib的JAR包添加到项目的类路径中。 2. 在JSP页面中引入Pager-taglib的TLD文件,例如`<%@ taglib prefix="pg" uri="http://pager.taglib.java.com/pager"%>`。 3. 设置分页参数,如总记录数、每页记录...
`pager-taglib`还支持自定义样式和属性,例如,你可以设置分页链接的CSS类,或者添加额外的参数到分页链接中。通过这些自定义选项,可以轻松地调整分页的外观和行为以适应不同的项目需求。 6. **与后端交互** 在...
对于分页查询,我们可以创建一个名为`<condition-pager>`的自定义标签,它接受查询条件和分页参数,然后生成对应的SQL语句并执行查询。 下面是一些关键步骤和知识点: 1. **创建自定义标签类**:你需要创建一个...
1. **安装和配置**:首先,你需要将`pager-taglib`的JAR文件添加到项目的类路径中。然后,在JSP页面的开头引入标签库,通常在`<jsp:root>`标签内添加`<jsp:taglib>`,指定URI和标签文件的位置。 2. **使用分页标签*...
1. **配置依赖**:首先需要在项目中引入Pager Tag Lib的相关库,这通常通过Maven或Gradle来完成,确保项目的类路径中有对应的jar文件。 2. **引入标签库**:在JSP页面中,通过`<%@ taglib %> `指令引入Pager Tag ...
特别是实现分页查询的功能,通过传入偏移量(offset)和每页记录数(pagesize),返回一个`PageModel`对象。 ```java public interface UserDao { public void addUser(User user); /** * 分页查询所有用户 * ...
5. **获取当前页数据**:可能有一个方法用于获取当前页的数据,这通常涉及到数据库查询的分页处理。 6. **自定义配置**:可能允许开发者通过设置属性来自定义分页的样式、页码间隔、是否显示第一页和最后一页等。 ...
ASP 分页类 Kin_Db_Pager 是一种在 ASP (Active Server Pages) 开发环境中实现数据库查询结果分页显示的工具。这个类库可以帮助开发者更高效、更便捷地处理大量的数据,避免一次性加载所有记录导致页面加载速度慢...
通过理解Action类的职责、Hibernate的数据访问机制以及Pager-taglib的使用方法,我们可以轻松地在实际项目中实现更复杂的分页需求。记住,实践是检验真理的唯一标准,动手尝试这个小例子,将有助于更好地理解和掌握...
使用`pager`标签库,可以快速地为查询结果添加分页功能。 **三、 pager标签库的使用步骤** 1. **引入依赖**:首先,你需要在项目中引入`pager`相关的jar包,这通常包含了`struts2-pager-plugin`和可能的依赖,如`...
6. **自定义样式**:jQuery.pager可能提供默认的CSS样式,但为了与网站设计保持一致,开发者可能需要根据需求定制分页的样式,通过修改或扩展插件提供的CSS类。 7. **响应式设计**:现代网页开发强调响应式布局,...
我们可以创建一个服务或仓库类,提供获取分页数据的方法。例如,一个`GetPagedData(int pageIndex, int pageSize)`方法,它会根据传入的页码和每页大小从数据库中查询相应数据。 2. **视图模型(ViewModel)**: ...
`pager-taglib`是一个专为JSP页面设计的分页标签库,它简化了在Web应用中实现分页的过程。在这个“pager-taglib分页例子”中,我们将探讨如何使用`pager-taglib`进行高效且简洁的分页实现。 首先,`pager-taglib`...
它提供了Criteria、HQL(Hibernate Query Language)等查询方式,以及分页查询的支持。 4. **Pager-Taglib分页库**: Pager-Taglib是JSP标签库,专门用于实现分页功能。在JSP页面中,通过引入Pager-Taglib的标签,...
分页是Web应用程序中常见的功能,它允许用户在大量数据中进行导航,通常用于数据库查询结果的展示,如电商网站的商品列表或论坛的帖子列表。 **描述分析:**"pager-taglib-2.0 JSP分页组件 包括jar包和安装使用说明...