- 浏览: 687389 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (254)
- java分布式应用架构 (22)
- SSH框架整合 (6)
- java web 学习笔记 (49)
- java 学习笔记 (56)
- struts 2 学习 (6)
- Hibernate学习 (10)
- spring 学习 (2)
- 客户端编程(javascript) (4)
- IDE使用 (13)
- 生命 人生 (6)
- 系统维护 (3)
- 技术篇 (10)
- MySql (2)
- J2ME (1)
- java网络编程 (4)
- 数据库 (5)
- C/C++ (8)
- Oracle (7)
- 软件测试 (0)
- 软件的安装和部署 (0)
- Java快讯 (1)
- swt (1)
- Flex (1)
- 软件工程 (1)
- PostgreSQL (1)
- sql server2000 (2)
- 嵌入式数据库sqlite (5)
- J2EE (1)
- XML (1)
- ibatis3(MyBatis) (6)
- Linux&Unix (1)
- velocity (1)
- 回报社会 (4)
- 软件项目管理 (3)
- android研究 (3)
- C# (2)
- Objective-C (1)
- 音乐 (0)
- webx (1)
- JMS (1)
- maven软件项目管理 (1)
- 分布式服务 (0)
- 云平台 (0)
- 分布式存储 (1)
- 分布式系统架构 (0)
- 移动互联网 (1)
- ZooKeeper (1)
最新评论
-
liyys:
楼主,可不可以发这个项目的源码工程出来分享一下,少了几个类。楼 ...
仿照Hibernate实现一个SQLite的ORM框架 -
liyys:
少了一些类的源码没有粘贴出来
仿照Hibernate实现一个SQLite的ORM框架 -
honglei0412:
我使用的是这种方式获取db文件的目录但是 URL p = Fi ...
使用sqlite注意事项 -
honglei0412:
大侠 能不能说明下DbFile您是怎么做的吗?
使用sqlite注意事项 -
ahack:
刚写完mapping才发现早就有人写好了。仔细一看还都是针对的 ...
仿照Hibernate实现一个SQLite的ORM框架
前一段时间在做一个作业,用到分页,再网上看里许多例子,发现都很繁琐,于是自己写了一个分页静态类,很好用!因为全在同一个类里,直接调用不同的方法就可以得到不同的List<?>结果集,大家指教一下。
分页类:
package com.liufei.hibernate.domain;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
/**
* 分页并按条件查询的操作类
*
* @author 刘飞
*
*/
public class PageUtil {
private static Configuration configuration;
private static SessionFactory sessionFactory;
private static Session session;
private static Class<?> clazz; // 为该类设置映射的实体类
private static DetachedCriteria detachedCriteria; //全局共享的<tt>DetachedCriteria</tt>对象
private static int indexPage = 1; // 当前页
private static boolean isHomePage ;
private static boolean isLastPage ;
private static int countPage; // 一共多少页
private static final int pageCount = 6; // 每页显示的记录条数
private static int allCounts; // 记录总数
public static List<?> listNeed; // List结果集
public static int[] indexs = new int[0] ; //分页索引数组
private static String queryCondition ;
/**
* 指定该类不可以被继承
*/
private PageUtil() {
}
static {
configuration = new Configuration().configure();
}
/**
* 打开<tt>SessionFactory</tt>对象
*/
public static void openSessionFactory() {
try {
sessionFactory = configuration.buildSessionFactory();
} catch (HibernateException sfe) {
sfe.printStackTrace();
}
}
/**
* 打开<tt>Session</tt>对象
*/
public static void openSession() {
try {
session = sessionFactory.openSession();
} catch (HibernateException se) {
se.printStackTrace();
}
}
/**
* 关闭当前<tt>SessionFactory</tt>对象
*/
public static void closeSessionFactory() {
if (sessionFactory.isClosed()) {
sessionFactory.close();
}
}
/**
* 关闭当前<tt>Session</tt>对象
*/
public static void closeSession() {
if (session.isOpen()) {
session.close();
}
}
/**
* 获取SessionFactory对象
*
* @return SessionFactory
*/
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* 获取Session对象
*
* @return Session
*/
public static Session getSession() {
return session;
}
/**
* 得到该类设置映射的实体类
*
* @return Class<?>
*/
public static Class<?> getClazz() {
return clazz;
}
/**
* 为该类设置映射的实体类
*
* @param clazz
*/
public static void setClazz(Class<?> clazz) {
PageUtil.clazz = clazz;
}
/**
* 获取查询条件
* @return String
*/
public static String getQueryCondition() {
return queryCondition;
}
/**
* 设置查询条件
* @param queryCondition
*/
public static void setQueryCondition(String queryCondition) {
PageUtil.queryCondition = queryCondition;
}
/**
* 将<tt>DetachedCriteria</tt>对象绑定到当前打开的<tt>Session</tt>
* 对象上,(可以使用add()方法在绑定前加入查询条件)
* 建立Session后,通过getExecutableCriteria(getSession())方法获取Criteria实例
* 在当前打开的Session对象关闭之后,DetachedCriteria对象依然存在,可以继续访问和操作 该方法依据具体的实体类对象为该类提供一个
* <tt>DetachedCriteria</tt>对象
*
* @param clazz
* @return List
*/
public static DetachedCriteria getDetachedCriteriaList() {
detachedCriteria = DetachedCriteria.forClass(getClazz());
return detachedCriteria;
}
/**
* 依据getDetachedCriteriaList(Class<?> clazz)方法所返回的<tt>DetachedCriteria</tt>
* 对象得到<tt>Criteria</tt>对象
*
* @return Criteria
*/
public static Criteria getCriteria() {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
criteria.setFirstResult((getIndexPage() - 1) * getPagecount()); // 设置当前页
criteria.setMaxResults(getPagecount()); // 设置当前页要显示的记录数
return criteria;
}
/**
* 获取当前页
*
* @return int
*/
public static int getIndexPage() {
return indexPage;
}
/**
* 设置当前页
*
* @param indexPage
*/
public static void setIndexPage(int indexPage) {
PageUtil.indexPage = indexPage;
}
/**
* 获取分页索引数组
* @return int[]
*/
public static int[] getIndexs() {
int[] index = new int[0] ;
for(int i = 0 ; i < getCountPage() ; i++ ){
index[i] = i + 1 ;
}
PageUtil.indexs = index;
return PageUtil.indexs;
}
/**
* 获取由getCriteria()方法返回的Criteria对象生成的List<?>结果集
*
* @return List<?>
*/
public static List<?> getListNeed() {
setListNeed();
return listNeed;
}
/**
* 由getCriteria()方法返回的Criteria对象获取List<?>结果集
*/
public static void setListNeed() {
List<?> listNeed;
listNeed = getCriteria().list();
PageUtil.listNeed = listNeed;
}
/**
* 设置上一页
*/
public static void setPreviouePage() {
int index = 1;
if(isHomePage()){
index = getIndexPage() - 1 ;
}else{
setIndexPage(1) ;
System.out.println("已经是首页了") ;
}
setIndexPage(index) ;
}
/**
* 设置下一页
*
* @param nextPage
*/
public static void setNextPage() {
int index = 0 ;
if(isLastPage()){
index = getIndexPage() + 1 ;
}else{
setIndexPage(getCountPage()) ;
System.out.println("已经是最后一页了") ;
}
setIndexPage(index) ;
}
/**
* 是否是首页
*
* @return boolean
* true 表示是首页 false 表示不是首页
*/
public static boolean isHomePage() {
boolean HomePage = false;
if (getIndexPage() == 1){
HomePage = true ;
}
isHomePage = HomePage ;
return isHomePage;
}
/**
* 是否是最后一页
*
* @return boolean
* true 表示是最后一页 false 表示不是最后一页
*/
public static boolean isLastPage() {
boolean lastPage = false ;
if (getIndexPage() == getCountPage()){
lastPage = true ;
}
isLastPage = lastPage ;
return isLastPage;
}
/**
* 获取总页数
*
* @return int
*/
public static int getCountPage() {
int countPage = 1;
countPage = ((getAllCounts() / pageCount) + 1);
PageUtil.countPage = countPage;
return PageUtil.countPage;
}
/**
* 获取记录总数
*
* @return int
*/
public static int getAllCounts() {
int allCounts = 0;
Criteria criteria;
criteria = getSession().createCriteria(getClazz()) ;
allCounts = ((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
PageUtil.allCounts = allCounts;
return PageUtil.allCounts;
}
/**
* 设置首页
*
*/
public static void setHomePage() {
setIndexPage(1);
}
/**
* 设置尾页
*
* @param lastPage
*/
public static void setLastPage() {
setIndexPage(getCountPage()) ;
}
/**
* 每页显示的记录条数(常量)
*
* @return int
*/
public static int getPagecount() {
return pageCount;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PageUtil.openSessionFactory();
PageUtil.openSession();
PageUtil.setClazz(InsertDB.class);
PageUtil.getDetachedCriteriaList() ;
//setNextPage();
//setNextPage();
//setPreviouePage();
//setPreviouePage();
setHomePage() ;
//setLastPage() ;
List<?> list = PageUtil.getListNeed();
Iterator<?> iterator = list.iterator();
System.out
.println("验证查询************************************************");
System.out.println("主键编号" + "\t" + "姓名" + "\t" + "Email地址");
while (iterator.hasNext()) {
InsertDB user = (InsertDB) iterator.next();
System.out.println(user.getId() + "\t" + user.getName() + "\t" + user.getEmail());
}
System.out
.println("验证记录条数**********************************************");
System.out.println("总记录条数为:\t" + getAllCounts());
System.out
.println("验证页数***************************************************");
System.out.println("总页数为:\t" + getCountPage());
System.out
.println("验证是否首页***************************************************");
System.out.println(isHomePage()) ;
System.out
.println("验证是否最后一页***************************************************");
System.out.println(isLastPage()) ;
System.out
.println("验证当前页***************************************************");
System.out.println(getIndexPage()) ;
System.out
.println("验证分页数组***************************************************");
for(int i = 1 ; i <= getCountPage() ; i++){
System.out.println(i) ;
}
/*int[] indexget = PageUtil.getIndexs() ;
for(int i = 0 ; i < indexget.length ; i++){
System.out.println(indexget[i]) ;
}*/
PageUtil.closeSession() ;
PageUtil.closeSessionFactory() ;
}
}
Action:
package com.liufei.actions;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Iterator;
import org.hibernate.Query;
import com.liufei.hibernate.domain.HibernateUtil;
import com.liufei.hibernate.domain.InsertDB;
import com.liufei.hibernate.domain.PageUtil;
import com.opensymphony.xwork2.ActionSupport;
public class QueryDelete extends BaseAction {
/**
*
*/
private static final long serialVersionUID = 1L;
private String QueryID = "liufei" ;
private static List<?> cList ;
public String getQueryID() {
return QueryID;
}
public void setQueryID(String queryID) {
QueryID = queryID;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
PageUtil.openSessionFactory();
PageUtil.openSession();
PageUtil.setClazz(InsertDB.class);
PageUtil.getDetachedCriteriaList() ;
List<?> users = PageUtil.getListNeed() ;
return ActionSupport.SUCCESS;
}
public String select(){
HibernateUtil.openSessionFactory() ;
HibernateUtil.openSession() ;
String hql = "from InsertDB as user where user.name like :name" ;
// 调用session的获得数据列表方法,传递HQL查询语句
String value = "%" + getQueryID() + "%";
Query query = HibernateUtil.getSession().createQuery(hql);
query.setString("name",value);
cList = query.list() ;
//HttpSession session = session() ;
//session.setAttribute("listInfo", cList) ;
HibernateUtil.closeSession() ;
HibernateUtil.closeSessionFactory() ;
System.out.println("select");
return "selected" ;
}
public String delete(){
System.out.println("delete");
return "deleted" ;
}
public String next(){
PageUtil.setNextPage() ;
System.out.println("next");
return "next" ;
}
public String previous(){
PageUtil.setPreviouePage() ;
System.out.println("previous");
return "previous" ;
}
public String home(){
PageUtil.setHomePage() ;
System.out.println("home");
return "home" ;
}
public String last(){
PageUtil.setLastPage() ;
System.out.println("last");
return "last" ;
}
public static void main(String[] args){
QueryDelete test = new QueryDelete() ;
System.out.println(test.select()) ;
Iterator<?> iterat = cList.iterator() ;
while(iterat.hasNext()){
InsertDB insertdb = (InsertDB)iterat.next() ;
System.out.println(insertdb.getAddress()) ;
}
}
}
jsp页面:
<%@ page language="java" import="java.util.*,org.hibernate.Criteria,com.liufei.hibernate.domain.*,org.hibernate.Query,org.hibernate.SessionFactory,org.hibernate.cfg.Configuration,org.hibernate.Session" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>名片浏览与查询</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="styles.css">
<style type="text/css">
<!--
body {
background-color: #ccdddd
}
.containe {
background: #ccddef;
width: 70%;
height: 700px;
position: absolute;
left: 50%;
margin: 0px 0 0 -30%;
border-style: inset;
}
.containe .top {
margin: 1px;
background-color: #ddddff;
border: 1px outset #ccddee;
dashed: #111111;
border-style: outset;
text-align: right;
}
#time{
margin: 1px;
background-color: #ddddff;
border: 1px outset #ccddee;
dashed: #111111;
border-style: outset;
text-align: right;
}
#queryID{
margin: 1px;
background-color: #ddddee;
border: 1px outset #ccddee;
dashed: #111111;
border-style: outset;
text-align: right;
float:left ;
z-index : 0 ;
}
#show{
margin: 1px;
background-color: #dddffe;
border: 1px outset #ccddee;
dashed: #111111;
border-style: outset;
text-align: right;
z-index : 1 ;
}
#search{
background:url("images/search_but.gif") no-repeat;
width:80px;
height:25px;
}
#ttop{
text-align:center;
padding-bottom:10px;
}
-->
</style>
</head>
<body>
<%
java.util.Date now = new java.util.Date();
pageContext.setAttribute("now", now);
%>
<div class="containe">
<div id = "time"><s:label>本次操作时间是:</s:label><s:date name="#attr.now" nice="false" />
<s:date name="unCarInsModificationInfo.createTime" format="yyyy-MM-dd" nice="false"/>
</div>
<p align="center">
<s:label>
<font size="8">名片浏览与查询</font>
</s:label>
</p>
<div class="top">
<s:form name = "sform" method="post" action = "QueryDelete.action">
<div id = "queryID">
<s:textfield name = "QueryID" label = "名片搜索(请键入姓名相关信息)"></s:textfield>
<s:submit id = "search" name = "action:select" type = "button" value = "" align = "right"></s:submit>
</div><hr/>
<table align="center">
<caption id = "ttop">查询的名片信息</caption>
<thead>
<tr>
<th align="center" valign="middle" width="50px" height="5px" rowspan="1" colspan="1" bgColor="#808000" borderColor="#ff8040">编号</th>
<th align="center" valign="middle" width="50px" height="5px" rowspan="1" colspan="1" bgColor="#808000" borderColor="#ff8040">姓名</th>
<th align="center" valign="middle" width="50px" height="5px" rowspan="1" colspan="1" bgColor="#808000" borderColor="#ff8040">性别</th>
<th align="center" valign="middle" width="50px" height="5px" rowspan="1" colspan="1" bgColor="#808000" borderColor="#ff8040">Email</th>
<th align="center" valign="middle" width="50px" height="5px" rowspan="1" colspan="1" bgColor="#808000" borderColor="#ff8040">涉足行业</th>
<th align="center" valign="middle" width="50px" height="5px" rowspan="1" colspan="1" bgColor="#808000" borderColor="#ff8040">手机号码</th>
<th align="center" valign="middle" width="50px" height="5px" rowspan="1" colspan="1" bgColor="#808000" borderColor="#ff8040">QQ号码</th>
<th align="center" valign="middle" width="50px" height="5px" rowspan="1" colspan="1" bgColor="#808000" borderColor="#ff8040">MSN</th>
<th align="center" valign="middle" width="150px" height="5px" rowspan="1" colspan="1" bgColor="#808000" borderColor="#ff8040">公司地址</th>
<th align="center" valign="middle" width="150px" height="5px" rowspan="1" colspan="1" bgColor="#808000" borderColor="#ff8040">个人简介</th>
<th align="center" valign="middle" width="100px" height="5px" rowspan="1" colspan="1" bgColor="#808000" borderColor="#ff8040">操作选择</th>
</tr>
</thead>
<tbody>
<%
PageUtil.openSessionFactory();
PageUtil.openSession();
PageUtil.setClazz(InsertDB.class);
PageUtil.getDetachedCriteriaList() ;
List<?> users = PageUtil.getListNeed() ;
Iterator<?> iterator = users.iterator() ;
while(iterator.hasNext()){
InsertDB user = (InsertDB)iterator.next() ;
%>
<s:iterator status="status">
<tr <s:if test = "#status.odd">style="background-color:#c0c0d0"</s:if>>
<td align="center" valign="bottom" rowspan="1" colspan="1"><input type = "checkbox" name = "number" value = "<%=user.getName() %>"/><%=user.getId() %></td>
<td align="center" valign="bottom" borderColor="#000000" rowspan="1" colspan="1"><%=user.getName() %></td>
<td align="center" valign="bottom" borderColor="#000000" rowspan="1" colspan="1"><%=user.getSex() %></td>
<td align="center" valign="bottom" borderColor="#000000" rowspan="1" colspan="1"><%=user.getEmail() %></td>
<td align="center" valign="bottom" borderColor="#000000" rowspan="1" colspan="1"><%=user.getHangye() %></td>
<td align="center" valign="bottom" borderColor="#000000" rowspan="1" colspan="1"><%=user.getSellphone() %></td>
<td align="center" valign="bottom" borderColor="#000000" rowspan="1" colspan="1"><%=user.getQQnum() %></td>
<td align="center" valign="bottom" borderColor="#000000" rowspan="1" colspan="1"><%=user.getMSN() %></td>
<td align="center" valign="bottom" borderColor="#000000" rowspan="1" colspan="1"><%=user.getCompanyaddress() %></td>
<td align="center" valign="bottom" borderColor="#000000" rowspan="1" colspan="1"><%=user.getInformation() %></td>
<td align="center" valign="bottom" borderColor="#000000" rowspan="1" colspan="1">
<a href='<s:url value="AlterInfo.jsp" >
<s:param name="num" value="#{ user.getId()}" />
<s:param name="insertdb.id" value="#{ user.id}"/></s:url>'>
[修改名片]
</a>
</td>
</tr>
</s:iterator>
<%} %>
</tbody>
<tfoot align = "center"><tr><td align="center" valign="middle" colspan="12">
<a href = "home.action" >首页</a>
<a href = "previous.action" >上一页</a>
<a href = "next.action" >下一页</a>
<a href = "last.action" >尾页</a>
</td></tr></tfoot>
</table>
<center>
<s:submit id = "delete" name = "action:delete" type = "button" value = "移入回收站" align = "right"></s:submit>
</center>
</s:form>
</div>
</div>
</body>
</html>
配置文件:
<action class="com.liufei.actions.QueryDelete" name="QueryDelete">
<result name="success">/QueryDeleteConn.jsp</result>
</action>
<action class="com.liufei.actions.QueryDelete" name="select" method = "select">
<result name="selected">/QueryDeleteConn.jsp</result>
</action>
<action class="com.liufei.actions.QueryDelete" name="delete" method = "delete">
<result name="deleted">/QueryDeleteConn.jsp</result>
</action>
<action class="com.liufei.actions.QueryDelete" name="next" method = "next">
<result name="next">/QueryDeleteConn.jsp</result>
</action>
<action class="com.liufei.actions.QueryDelete" name="previous" method = "previous">
<result name="previous">/QueryDeleteConn.jsp</result>
</action>
<action class="com.liufei.actions.QueryDelete" name="home" method = "home">
<result name="home">/QueryDeleteConn.jsp</result>
</action>
<action class="com.liufei.actions.QueryDelete" name="last" method = "last">
<result name="last">/QueryDeleteConn.jsp</result>
</action>
分页类:
package com.liufei.hibernate.domain;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
/**
* 分页并按条件查询的操作类
*
* @author 刘飞
*
*/
public class PageUtil {
private static Configuration configuration;
private static SessionFactory sessionFactory;
private static Session session;
private static Class<?> clazz; // 为该类设置映射的实体类
private static DetachedCriteria detachedCriteria; //全局共享的<tt>DetachedCriteria</tt>对象
private static int indexPage = 1; // 当前页
private static boolean isHomePage ;
private static boolean isLastPage ;
private static int countPage; // 一共多少页
private static final int pageCount = 6; // 每页显示的记录条数
private static int allCounts; // 记录总数
public static List<?> listNeed; // List结果集
public static int[] indexs = new int[0] ; //分页索引数组
private static String queryCondition ;
/**
* 指定该类不可以被继承
*/
private PageUtil() {
}
static {
configuration = new Configuration().configure();
}
/**
* 打开<tt>SessionFactory</tt>对象
*/
public static void openSessionFactory() {
try {
sessionFactory = configuration.buildSessionFactory();
} catch (HibernateException sfe) {
sfe.printStackTrace();
}
}
/**
* 打开<tt>Session</tt>对象
*/
public static void openSession() {
try {
session = sessionFactory.openSession();
} catch (HibernateException se) {
se.printStackTrace();
}
}
/**
* 关闭当前<tt>SessionFactory</tt>对象
*/
public static void closeSessionFactory() {
if (sessionFactory.isClosed()) {
sessionFactory.close();
}
}
/**
* 关闭当前<tt>Session</tt>对象
*/
public static void closeSession() {
if (session.isOpen()) {
session.close();
}
}
/**
* 获取SessionFactory对象
*
* @return SessionFactory
*/
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* 获取Session对象
*
* @return Session
*/
public static Session getSession() {
return session;
}
/**
* 得到该类设置映射的实体类
*
* @return Class<?>
*/
public static Class<?> getClazz() {
return clazz;
}
/**
* 为该类设置映射的实体类
*
* @param clazz
*/
public static void setClazz(Class<?> clazz) {
PageUtil.clazz = clazz;
}
/**
* 获取查询条件
* @return String
*/
public static String getQueryCondition() {
return queryCondition;
}
/**
* 设置查询条件
* @param queryCondition
*/
public static void setQueryCondition(String queryCondition) {
PageUtil.queryCondition = queryCondition;
}
/**
* 将<tt>DetachedCriteria</tt>对象绑定到当前打开的<tt>Session</tt>
* 对象上,(可以使用add()方法在绑定前加入查询条件)
* 建立Session后,通过getExecutableCriteria(getSession())方法获取Criteria实例
* 在当前打开的Session对象关闭之后,DetachedCriteria对象依然存在,可以继续访问和操作 该方法依据具体的实体类对象为该类提供一个
* <tt>DetachedCriteria</tt>对象
*
* @param clazz
* @return List
*/
public static DetachedCriteria getDetachedCriteriaList() {
detachedCriteria = DetachedCriteria.forClass(getClazz());
return detachedCriteria;
}
/**
* 依据getDetachedCriteriaList(Class<?> clazz)方法所返回的<tt>DetachedCriteria</tt>
* 对象得到<tt>Criteria</tt>对象
*
* @return Criteria
*/
public static Criteria getCriteria() {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
criteria.setFirstResult((getIndexPage() - 1) * getPagecount()); // 设置当前页
criteria.setMaxResults(getPagecount()); // 设置当前页要显示的记录数
return criteria;
}
/**
* 获取当前页
*
* @return int
*/
public static int getIndexPage() {
return indexPage;
}
/**
* 设置当前页
*
* @param indexPage
*/
public static void setIndexPage(int indexPage) {
PageUtil.indexPage = indexPage;
}
/**
* 获取分页索引数组
* @return int[]
*/
public static int[] getIndexs() {
int[] index = new int[0] ;
for(int i = 0 ; i < getCountPage() ; i++ ){
index[i] = i + 1 ;
}
PageUtil.indexs = index;
return PageUtil.indexs;
}
/**
* 获取由getCriteria()方法返回的Criteria对象生成的List<?>结果集
*
* @return List<?>
*/
public static List<?> getListNeed() {
setListNeed();
return listNeed;
}
/**
* 由getCriteria()方法返回的Criteria对象获取List<?>结果集
*/
public static void setListNeed() {
List<?> listNeed;
listNeed = getCriteria().list();
PageUtil.listNeed = listNeed;
}
/**
* 设置上一页
*/
public static void setPreviouePage() {
int index = 1;
if(isHomePage()){
index = getIndexPage() - 1 ;
}else{
setIndexPage(1) ;
System.out.println("已经是首页了") ;
}
setIndexPage(index) ;
}
/**
* 设置下一页
*
* @param nextPage
*/
public static void setNextPage() {
int index = 0 ;
if(isLastPage()){
index = getIndexPage() + 1 ;
}else{
setIndexPage(getCountPage()) ;
System.out.println("已经是最后一页了") ;
}
setIndexPage(index) ;
}
/**
* 是否是首页
*
* @return boolean
* true 表示是首页 false 表示不是首页
*/
public static boolean isHomePage() {
boolean HomePage = false;
if (getIndexPage() == 1){
HomePage = true ;
}
isHomePage = HomePage ;
return isHomePage;
}
/**
* 是否是最后一页
*
* @return boolean
* true 表示是最后一页 false 表示不是最后一页
*/
public static boolean isLastPage() {
boolean lastPage = false ;
if (getIndexPage() == getCountPage()){
lastPage = true ;
}
isLastPage = lastPage ;
return isLastPage;
}
/**
* 获取总页数
*
* @return int
*/
public static int getCountPage() {
int countPage = 1;
countPage = ((getAllCounts() / pageCount) + 1);
PageUtil.countPage = countPage;
return PageUtil.countPage;
}
/**
* 获取记录总数
*
* @return int
*/
public static int getAllCounts() {
int allCounts = 0;
Criteria criteria;
criteria = getSession().createCriteria(getClazz()) ;
allCounts = ((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
PageUtil.allCounts = allCounts;
return PageUtil.allCounts;
}
/**
* 设置首页
*
*/
public static void setHomePage() {
setIndexPage(1);
}
/**
* 设置尾页
*
* @param lastPage
*/
public static void setLastPage() {
setIndexPage(getCountPage()) ;
}
/**
* 每页显示的记录条数(常量)
*
* @return int
*/
public static int getPagecount() {
return pageCount;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PageUtil.openSessionFactory();
PageUtil.openSession();
PageUtil.setClazz(InsertDB.class);
PageUtil.getDetachedCriteriaList() ;
//setNextPage();
//setNextPage();
//setPreviouePage();
//setPreviouePage();
setHomePage() ;
//setLastPage() ;
List<?> list = PageUtil.getListNeed();
Iterator<?> iterator = list.iterator();
System.out
.println("验证查询************************************************");
System.out.println("主键编号" + "\t" + "姓名" + "\t" + "Email地址");
while (iterator.hasNext()) {
InsertDB user = (InsertDB) iterator.next();
System.out.println(user.getId() + "\t" + user.getName() + "\t" + user.getEmail());
}
System.out
.println("验证记录条数**********************************************");
System.out.println("总记录条数为:\t" + getAllCounts());
System.out
.println("验证页数***************************************************");
System.out.println("总页数为:\t" + getCountPage());
System.out
.println("验证是否首页***************************************************");
System.out.println(isHomePage()) ;
System.out
.println("验证是否最后一页***************************************************");
System.out.println(isLastPage()) ;
System.out
.println("验证当前页***************************************************");
System.out.println(getIndexPage()) ;
System.out
.println("验证分页数组***************************************************");
for(int i = 1 ; i <= getCountPage() ; i++){
System.out.println(i) ;
}
/*int[] indexget = PageUtil.getIndexs() ;
for(int i = 0 ; i < indexget.length ; i++){
System.out.println(indexget[i]) ;
}*/
PageUtil.closeSession() ;
PageUtil.closeSessionFactory() ;
}
}
Action:
package com.liufei.actions;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Iterator;
import org.hibernate.Query;
import com.liufei.hibernate.domain.HibernateUtil;
import com.liufei.hibernate.domain.InsertDB;
import com.liufei.hibernate.domain.PageUtil;
import com.opensymphony.xwork2.ActionSupport;
public class QueryDelete extends BaseAction {
/**
*
*/
private static final long serialVersionUID = 1L;
private String QueryID = "liufei" ;
private static List<?> cList ;
public String getQueryID() {
return QueryID;
}
public void setQueryID(String queryID) {
QueryID = queryID;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
PageUtil.openSessionFactory();
PageUtil.openSession();
PageUtil.setClazz(InsertDB.class);
PageUtil.getDetachedCriteriaList() ;
List<?> users = PageUtil.getListNeed() ;
return ActionSupport.SUCCESS;
}
public String select(){
HibernateUtil.openSessionFactory() ;
HibernateUtil.openSession() ;
String hql = "from InsertDB as user where user.name like :name" ;
// 调用session的获得数据列表方法,传递HQL查询语句
String value = "%" + getQueryID() + "%";
Query query = HibernateUtil.getSession().createQuery(hql);
query.setString("name",value);
cList = query.list() ;
//HttpSession session = session() ;
//session.setAttribute("listInfo", cList) ;
HibernateUtil.closeSession() ;
HibernateUtil.closeSessionFactory() ;
System.out.println("select");
return "selected" ;
}
public String delete(){
System.out.println("delete");
return "deleted" ;
}
public String next(){
PageUtil.setNextPage() ;
System.out.println("next");
return "next" ;
}
public String previous(){
PageUtil.setPreviouePage() ;
System.out.println("previous");
return "previous" ;
}
public String home(){
PageUtil.setHomePage() ;
System.out.println("home");
return "home" ;
}
public String last(){
PageUtil.setLastPage() ;
System.out.println("last");
return "last" ;
}
public static void main(String[] args){
QueryDelete test = new QueryDelete() ;
System.out.println(test.select()) ;
Iterator<?> iterat = cList.iterator() ;
while(iterat.hasNext()){
InsertDB insertdb = (InsertDB)iterat.next() ;
System.out.println(insertdb.getAddress()) ;
}
}
}
jsp页面:
<%@ page language="java" import="java.util.*,org.hibernate.Criteria,com.liufei.hibernate.domain.*,org.hibernate.Query,org.hibernate.SessionFactory,org.hibernate.cfg.Configuration,org.hibernate.Session" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>名片浏览与查询</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="styles.css">
<style type="text/css">
<!--
body {
background-color: #ccdddd
}
.containe {
background: #ccddef;
width: 70%;
height: 700px;
position: absolute;
left: 50%;
margin: 0px 0 0 -30%;
border-style: inset;
}
.containe .top {
margin: 1px;
background-color: #ddddff;
border: 1px outset #ccddee;
dashed: #111111;
border-style: outset;
text-align: right;
}
#time{
margin: 1px;
background-color: #ddddff;
border: 1px outset #ccddee;
dashed: #111111;
border-style: outset;
text-align: right;
}
#queryID{
margin: 1px;
background-color: #ddddee;
border: 1px outset #ccddee;
dashed: #111111;
border-style: outset;
text-align: right;
float:left ;
z-index : 0 ;
}
#show{
margin: 1px;
background-color: #dddffe;
border: 1px outset #ccddee;
dashed: #111111;
border-style: outset;
text-align: right;
z-index : 1 ;
}
#search{
background:url("images/search_but.gif") no-repeat;
width:80px;
height:25px;
}
#ttop{
text-align:center;
padding-bottom:10px;
}
-->
</style>
</head>
<body>
<%
java.util.Date now = new java.util.Date();
pageContext.setAttribute("now", now);
%>
<div class="containe">
<div id = "time"><s:label>本次操作时间是:</s:label><s:date name="#attr.now" nice="false" />
<s:date name="unCarInsModificationInfo.createTime" format="yyyy-MM-dd" nice="false"/>
</div>
<p align="center">
<s:label>
<font size="8">名片浏览与查询</font>
</s:label>
</p>
<div class="top">
<s:form name = "sform" method="post" action = "QueryDelete.action">
<div id = "queryID">
<s:textfield name = "QueryID" label = "名片搜索(请键入姓名相关信息)"></s:textfield>
<s:submit id = "search" name = "action:select" type = "button" value = "" align = "right"></s:submit>
</div><hr/>
<table align="center">
<caption id = "ttop">查询的名片信息</caption>
<thead>
<tr>
<th align="center" valign="middle" width="50px" height="5px" rowspan="1" colspan="1" bgColor="#808000" borderColor="#ff8040">编号</th>
<th align="center" valign="middle" width="50px" height="5px" rowspan="1" colspan="1" bgColor="#808000" borderColor="#ff8040">姓名</th>
<th align="center" valign="middle" width="50px" height="5px" rowspan="1" colspan="1" bgColor="#808000" borderColor="#ff8040">性别</th>
<th align="center" valign="middle" width="50px" height="5px" rowspan="1" colspan="1" bgColor="#808000" borderColor="#ff8040">Email</th>
<th align="center" valign="middle" width="50px" height="5px" rowspan="1" colspan="1" bgColor="#808000" borderColor="#ff8040">涉足行业</th>
<th align="center" valign="middle" width="50px" height="5px" rowspan="1" colspan="1" bgColor="#808000" borderColor="#ff8040">手机号码</th>
<th align="center" valign="middle" width="50px" height="5px" rowspan="1" colspan="1" bgColor="#808000" borderColor="#ff8040">QQ号码</th>
<th align="center" valign="middle" width="50px" height="5px" rowspan="1" colspan="1" bgColor="#808000" borderColor="#ff8040">MSN</th>
<th align="center" valign="middle" width="150px" height="5px" rowspan="1" colspan="1" bgColor="#808000" borderColor="#ff8040">公司地址</th>
<th align="center" valign="middle" width="150px" height="5px" rowspan="1" colspan="1" bgColor="#808000" borderColor="#ff8040">个人简介</th>
<th align="center" valign="middle" width="100px" height="5px" rowspan="1" colspan="1" bgColor="#808000" borderColor="#ff8040">操作选择</th>
</tr>
</thead>
<tbody>
<%
PageUtil.openSessionFactory();
PageUtil.openSession();
PageUtil.setClazz(InsertDB.class);
PageUtil.getDetachedCriteriaList() ;
List<?> users = PageUtil.getListNeed() ;
Iterator<?> iterator = users.iterator() ;
while(iterator.hasNext()){
InsertDB user = (InsertDB)iterator.next() ;
%>
<s:iterator status="status">
<tr <s:if test = "#status.odd">style="background-color:#c0c0d0"</s:if>>
<td align="center" valign="bottom" rowspan="1" colspan="1"><input type = "checkbox" name = "number" value = "<%=user.getName() %>"/><%=user.getId() %></td>
<td align="center" valign="bottom" borderColor="#000000" rowspan="1" colspan="1"><%=user.getName() %></td>
<td align="center" valign="bottom" borderColor="#000000" rowspan="1" colspan="1"><%=user.getSex() %></td>
<td align="center" valign="bottom" borderColor="#000000" rowspan="1" colspan="1"><%=user.getEmail() %></td>
<td align="center" valign="bottom" borderColor="#000000" rowspan="1" colspan="1"><%=user.getHangye() %></td>
<td align="center" valign="bottom" borderColor="#000000" rowspan="1" colspan="1"><%=user.getSellphone() %></td>
<td align="center" valign="bottom" borderColor="#000000" rowspan="1" colspan="1"><%=user.getQQnum() %></td>
<td align="center" valign="bottom" borderColor="#000000" rowspan="1" colspan="1"><%=user.getMSN() %></td>
<td align="center" valign="bottom" borderColor="#000000" rowspan="1" colspan="1"><%=user.getCompanyaddress() %></td>
<td align="center" valign="bottom" borderColor="#000000" rowspan="1" colspan="1"><%=user.getInformation() %></td>
<td align="center" valign="bottom" borderColor="#000000" rowspan="1" colspan="1">
<a href='<s:url value="AlterInfo.jsp" >
<s:param name="num" value="#{ user.getId()}" />
<s:param name="insertdb.id" value="#{ user.id}"/></s:url>'>
[修改名片]
</a>
</td>
</tr>
</s:iterator>
<%} %>
</tbody>
<tfoot align = "center"><tr><td align="center" valign="middle" colspan="12">
<a href = "home.action" >首页</a>
<a href = "previous.action" >上一页</a>
<a href = "next.action" >下一页</a>
<a href = "last.action" >尾页</a>
</td></tr></tfoot>
</table>
<center>
<s:submit id = "delete" name = "action:delete" type = "button" value = "移入回收站" align = "right"></s:submit>
</center>
</s:form>
</div>
</div>
</body>
</html>
配置文件:
<action class="com.liufei.actions.QueryDelete" name="QueryDelete">
<result name="success">/QueryDeleteConn.jsp</result>
</action>
<action class="com.liufei.actions.QueryDelete" name="select" method = "select">
<result name="selected">/QueryDeleteConn.jsp</result>
</action>
<action class="com.liufei.actions.QueryDelete" name="delete" method = "delete">
<result name="deleted">/QueryDeleteConn.jsp</result>
</action>
<action class="com.liufei.actions.QueryDelete" name="next" method = "next">
<result name="next">/QueryDeleteConn.jsp</result>
</action>
<action class="com.liufei.actions.QueryDelete" name="previous" method = "previous">
<result name="previous">/QueryDeleteConn.jsp</result>
</action>
<action class="com.liufei.actions.QueryDelete" name="home" method = "home">
<result name="home">/QueryDeleteConn.jsp</result>
</action>
<action class="com.liufei.actions.QueryDelete" name="last" method = "last">
<result name="last">/QueryDeleteConn.jsp</result>
</action>
发表评论
-
Hibernate与iBATIS的较量浅析
2010-11-21 22:18 1240hibernate与ibatis的比较,首先我们从他们的简介开 ... -
Spring中的四种声明式事务的配置
2010-11-03 20:57 1089Spring中的四种声明式事 ... -
在spring中同时使用jdbcTemplate和hibernateTemplate
2010-11-01 11:34 2897就像我们知道的一样,Hib ... -
spring的hibernateTemplate
2010-10-31 18:19 22451.管理SessionFactory 使用Spring ... -
用过滤器管理hibernate的session及Transaction事务
2010-06-06 19:16 1894过滤器: package org.liufei.filters ...
相关推荐
Struts作为MVC(Model-View-Controller)架构的一部分,主要用于处理用户请求和控制应用程序流程,而Hibernate则是一个对象关系映射(ORM)工具,它简化了数据库操作,使得Java对象可以直接与数据库进行交互。...
Struts、Hibernate和DisplayTag是Java Web开发中的三个重要组件,它们共同构成了一个强大的MVC(Model-View-Controller)架构。在这个实例中,我们将会深入探讨如何将它们整合在一起,实现一个具备分页功能的Web应用...
通过理解Struts、Spring和Hibernate的原理及其相互关系,开发者可以将它们高效地组合起来,形成一个以Spring为核心,Struts为表现层,Hibernate为数据访问层的架构。这个案例的实现有助于深入理解SSH整合的实际运用...
这是一个struts2+hibernate+spring整合的实例,带有分页功能,希望对刚接触struts2的朋友有些帮助。spring2.0,hibernate3.1分页用的是自字义标签,不要少了这个包standard.jar
本人用struts+hibernate写的分页,有详细的注解,方便新手学习,在jsp中实现首页下一页上一页末页功能.index.jsp控制showlist.jsp页面中每页显示的信息条数,本实例在xp+tomcat5.5验证通过.数据库使用的sql2000中的pubs...
在这个“关于Struts+Hibernate分页的例子”中,我们可能看到一个集成这两个框架的Web应用,用于展示如何在Struts中利用Hibernate进行数据的分页显示。分页是Web应用中常见的功能,能够帮助用户更有效地浏览大量数据...
Struts1是一个MVC(Model-View-Controller)框架,用于处理用户请求并管理应用程序逻辑,而Hibernate则是一个对象关系映射(ORM)工具,简化了数据库操作。在大型Web应用中,为了提高用户体验,分页技术通常被用来...
Struts的Action类会接收到用户的请求,然后根据请求参数(如当前页数)创建一个`PageBean`实例,并调用服务层的分页接口方法,将结果传递给视图层(View层)。 6. **在视图层展示**: 视图层(通常是JSP页面)...
这个“经调过的Struts+Hibernate分页演示程序”是一个完整的示例,展示了如何在实际项目中结合这两个框架实现数据的分页展示。 1. **Struts框架**: - Struts是一个基于Model-View-Controller (MVC) 设计模式的...
根据所提供的信息来看,本案例涉及到了一个基于Struts2与Hibernate技术栈的分页查询系统开发。项目构建所需的主要软件及版本包括: 1. **Eclipse 3.2**:主要用作项目的开发集成环境。 2. **MyEclipse 5.5**:作为...
总的来说,"struts+hibernate实例(分页)"是一个实用的学习资源,可以帮助开发者掌握如何在Java Web应用中整合Struts和Hibernate,实现高效、便捷的数据分页功能。通过学习和实践这个实例,开发者不仅可以提升技术...
总结起来,"Struts+Hibernate+Spring+JS 分页程序"实例展示了四个框架如何协同工作,实现Web应用程序的分页功能。Struts处理请求和响应,Hibernate负责数据库操作,Spring作为整体的协调者,JavaScript则为用户提供...
Struts是一个MVC(Model-View-Controller)框架,主要用于处理Web应用的业务逻辑和用户交互,而Hibernate则是一个对象关系映射(ORM)框架,它简化了数据库操作,使得开发者可以使用面向对象的方式来管理数据。...
### Struts+Hibernate 分页实例详解 #### 一、引言 在Web开发中,分页是提升用户体验的重要手段之一。对于大型应用而言,合理的分页处理不仅能减轻服务器压力,还能提高系统的响应速度。本文将详细介绍如何利用...
总的来说,这个"Struts2+Spring2+Hibernate3注册查询搜索分页实例"是一个很好的学习资源,涵盖了Java Web开发中的基础和核心部分。通过学习这个实例,开发者不仅可以掌握三大框架的基本用法,还能了解到如何将它们...
Hibernate是一个强大的对象关系映射(ORM)框架,简化了数据库操作;Spring则作为一个综合的轻量级框架,支持依赖注入和面向切面编程,促进了组件的解耦。 在这个"Struts+Hibernate+Spring的分页"项目中,开发者...
在给定的文件名"StrutsHibernate"中,我们可以推测这是一个包含Struts和Hibernate整合示例的工程,可能包含了配置文件、Action类、DAO(数据访问对象)类以及相关的JSP页面。通过分析这个项目,开发者可以学习如何在...
### Struts + Hibernate + Spring 应用下的实用分页实例详解 #### 一、背景介绍 在基于Struts + Hibernate + Spring(SSH)框架的Web应用程序开发中,数据展示是必不可少的一部分,特别是在需要处理大量数据的情况...