81,092
社区成员
发帖
与我相关
我的任务
分享
/**
* fileName: Page.java
* description: 分页使用Bean
*/
package com.sofmit.sd.framework.util;
import java.util.List;
/**
* 分页使用Bean
*
* @author zenggh
* @date June 1, 2010
*/
public class Page {
/**
* 默认的每页显示数据条数
*/
private static int DEFAULT_PAGE_SIZE = 10;
/**
* 每页显示数据
*/
private int pageSize = DEFAULT_PAGE_SIZE;
/**
* 总的数据数
*/
private int totalCount; // �ܼ�¼��
/**
* 当前页数
*/
private int currentPage;
/**
* 总页数
*/
private int pageCount;
/**
* 是否存在上一页
*/
private Boolean previous;
/**
* 是否存在下一页
*/
private Boolean next;
/**
* 分页显示的数据集合
*/
private List date;
/**
* 是否存在上一页
* @return
*/
public Boolean getPrevious() {
return previous;
}
/**
* 获取当前第几页��ȡ��ǰ
* @return
*/
public int getCurrentPage() {
return currentPage;
}
/**
* 是否存在下一页
* @return
*/
public Boolean getNext() {
return next;
}
/**
* 默认构造函数����Ϲ�����
*/
public Page(){
}
/**
* ����ÿ带每页数据条数的构造函数��С�Ĺ�����
*/
public void page(int pageSize){
this.pageSize = pageSize;
}
/**
* 获取总页数
* @return
*/
public int getPageCount() {
return pageCount;
}
/**
* ȡ�ܼ�¼��获取同的数据条数
*/
public int getTotalCount() {
return this.totalCount;
}
/**
* 设置当前页数
* @param currentPage
*/
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
/**
* 设置总页数
* @param pageCount
*/
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
/**
* 设置分页对象的属性值
* @param currentPage
* @param totalCount
*/
public void calculatePageInfo(int currentPage,int totalCount){
this.totalCount = totalCount;
this.currentPage = currentPage;
this.pageCount = this.getTotalPageCount();
this.previous = this.hasPreviousPage();
this.next = this.hasNextPage();
}
/**
* ȡ��计算总页数
*/
public int getTotalPageCount() {
if (totalCount % pageSize == 0)
return totalCount / pageSize;
else
return totalCount / pageSize + 1;
}
/**
* 设置每页数据条数
*/
public int getPageSize() {
return pageSize;
}
/**
* ��判断是否存在下一页
*/
public boolean hasNextPage() {
return this.currentPage < this.pageCount ;
}
/**
* 判断是否存在上一页
*/
public boolean hasPreviousPage() {
return this.currentPage > 1;
}
/**
*
*
* @see #getStartOfPage(int,int)
*/
protected int getStartOfPage(int pageNo) {
return getStartOfPage(pageNo, DEFAULT_PAGE_SIZE);
}
/**
*
* @param pageNo
* @param pageSize
* @return
*/
public int getStartOfPage(int pageNo, int pageSize) {
return (pageNo - 1) * pageSize;
}
/**
* 获取分页的数据集合
* @return
*/
public List getResult() {
return date;
}
/**
* 设置分页数据集合
* @param date
*/
public void setResult(List date) {
this.date = date;
}
/**
* 设置数据总条数
* @param totalCount
*/
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
}
/**
* 采用Criteria方式分页查询
* @param criteria Criteria对象
* @param page 传入的Page对象
* @return Page 分页对象
*/
@SuppressWarnings("unchecked")
public Page getPage(Criteria criteria, Page page) {
Assert.notNull(criteria);
CriteriaImpl impl = (CriteriaImpl) criteria;
Projection projection = impl.getProjection();
List<CriteriaImpl.OrderEntry> orderEntries;
try {
orderEntries = (List) BeanUtils.forceGetProperty(impl,
"orderEntries");
BeanUtils.forceSetProperty(impl, "orderEntries", new ArrayList());
} catch (Exception e) {
throw new InternalError(" Runtime Exception impossibility throw ");
}
// 总的数据行数
int totalCount = (Integer) criteria.setProjection(
Projections.rowCount()).uniqueResult();
criteria.setProjection(projection);
if (projection == null) {
criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
}
try {
BeanUtils.forceSetProperty(impl, "orderEntries", orderEntries);
} catch (Exception e) {
throw new InternalError(" Runtime Exception impossibility throw ");
}
List list = criteria.setFirstResult(
page.getStartOfPage(page.getCurrentPage(), page.getPageSize()))
.setMaxResults(page.getPageSize()).list();
page.setTotalCount(totalCount);
page.setResult(list);
log
.debug("total count:" + totalCount + " return count:"
+ list.size());
return page;
}
/**
* 采用HQL方式分页查询
* @param hql 查询HQL语句
* @param params 查询参数
* @param page 分页Page对象
* @return Page 分页对象
*/
@SuppressWarnings("deprecation")
public Page getPage(final String hql, final Object[] params, final Page page) {
return (Page) getHibernateTemplate().execute(new HibernateCallback() {
@SuppressWarnings("unchecked")
public Object doInHibernate(Session session) {
int count = 0;
String subHql = interceptive(hql);
if (subHql != null) {
log.debug("HQL = " + hql);
log.debug("SubHQL = " + subHql);
Query query = session.createQuery(subHql);
setParamForQuery(query, params);
if (query.uniqueResult() != null)
count = Integer.parseInt(query.uniqueResult()
.toString());
}
Query query = session.createQuery(hql);
setParamForQuery(query, params);
List result = query.setFirstResult(
page.getStartOfPage(page.getCurrentPage(), page
.getPageSize())).setMaxResults(
page.getPageSize()).list();
page.setTotalCount(count);
page.setResult(result);
page.calculatePageInfo(page.getCurrentPage(), page
.getTotalCount());
return page;
}
}, true);
}
public Page getPages(String userID){
String hql ="from LoginResource where userID like ? order by userID";
String strparm=null;
if(null!=userID)
{
strparm= "%"+userID+"%";
}
else
{
strparm= "%%";
}
Object[] params=new Object[]{strparm};
Page p=new Page();
p.setCurrentPage(1);
p.setPageSize(10);
Page page=this.getPage(hql, params, p);
return page;
}
Servlet.service() for servlet framework threw exception
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '10 loginresou0_.ID as ID0_, loginresou0_.userID as userID0_, loginresou0_.pwd as' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3515)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3447)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1951)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2101)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2554)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1761)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1912)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:76)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1808)
at org.hibernate.loader.Loader.doQuery(Loader.java:697)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
at org.hibernate.loader.Loader.doList(Loader.java:2228)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2125)
at org.hibernate.loader.Loader.list(Loader.java:2120)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:361)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1148)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at com.sofmit.sd.framework.common.dao.HibernateCommonImpDAO$1.doInHibernate(HibernateCommonImpDAO.java:310)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:387)
at com.sofmit.sd.framework.common.dao.HibernateCommonImpDAO.getPage(HibernateCommonImpDAO.java:291)
at com.sofmit.sd.login.dao.UserDAO.getPages(UserDAO.java:71)
at com.sofmit.sd.login.service.UserServiceImpl.getUserPage(UserServiceImpl.java:90)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy22.getUserPage(Unknown Source)
at com.sofmit.sd.login.web.UserController.pageUser(UserController.java:92)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:421)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:136)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:326)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:313)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:852)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:584)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1508)
at java.lang.Thread.run(Thread.java:619)
public Page getPages(String userID){
String hql ="from LoginResource";
String strparm=null;
//if(null!=userID)
//{
// strparm= "%"+userID+"%";
//}
//else
//{
// strparm= "%a%";
//}
Object[] params=new Object[]{strparm};
Page p=new Page();
p.setCurrentPage(0);
Page page=this.getPage(hql, params, p);
return page;
}
String hql = "from LoginResource where userID like:userID";