在网上看到一个不错的S2SH分页方案,原地址忘了,把代码贴出来备用。
/*省略get(),set()*/
}
TbUser.hbm.xml
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}
return session;
}
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}
public static Configuration getConfiguration() {
return configuration;
}
}
public TbUser findByUsernamePassword(String username,String password);
public Page findByPageNo(int pageNo,int pageSize);
}
query = session.createQuery("from TbUser");
query.setMaxResults(pageSize);
query.setFirstResult((pageNo-1)*pageSize);
page.setList(query.list());
page.setTotal(count);
page.setPageNo(pageNo);
page.setPageSize(pageSize);
return page;
}
public TbUser findByUsernamePassword(String username, String password) {
String hql = "from TbUser where username=? and password=?";
Query query = session.createQuery(hql);
query.setParameter(0, username);
query.setParameter(1, password);
return (TbUser) query.uniqueResult();
}
}
public TbUser findByUsernamePassword(String username,String password);
public Page findByPageNo(int pageNo,int pageSize);
}
public Page findByPageNo(int pageNo, int pageSize) {
return userDAO.findByPageNo(pageNo, pageSize);
}
public TbUser findByUsernamePassword(String username, String password) {
return userDAO.findByUsernamePassword(username, password);
}
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
public UserDAO getUserDAO() {
return userDAO;
}
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalPage() {
if(total%pageSize == 0){
totalPage = total/pageSize;
}else{
totalPage = total/pageSize + 1;
}
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
public int getPrePage() {
if(pageNo == 1){
prePage = pageNo;
}else{
prePage = pageNo - 1;
}
return prePage;
}
public void setPrePage(int prePage) {
this.prePage = prePage;
}
public int getNextPage() {
if(pageNo == totalPage){
nextPage = pageNo;
}else{
nextPage = pageNo + 1;
}
return nextPage;
}
public void setNextPage(int nextPage) {
this.nextPage = nextPage;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
}
applicationContext.xml
<bean id="userService" class="service.UserServiceImpl">
<property name="userDAO">
<ref local="userDAO"/>
</property>
</bean>
<bean id="loginAction" class="action.UserAction">
<property name="userService">
<ref local="userService"/>
</property>
</bean>
</beans>
hibernate.cfg.xml
<mapping resource="domain/TbUser.hbm.xml"/>
</session-factory>
</hibernate-configuration>
native_zh_CN.properties
struts.xml
login.jsp
index.jsp