package com.xdf.dao;
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.servlet.http.*;
import org.hibernate.*;
import org.hibernate.criterion.Projections;
import com.xdf.hibernate.HibernateSessionFactory;
public class Dao {
HttpServletRequest request;
HttpServletResponse response;
Session session;
//定义List集合
private List list;
public int intCountic;
public int PageSize;
public int intPageCount;
public int intPage;
public String nowPage;
public String HttpFile;
//取得网址是的参数
String PageParameter;
public Dao(HttpServletRequest request, HttpServletResponse response) {
this.request = request;
this.response = response;
}
/*
*******得到Session**********
*/
public Session getSession() {
session = HibernateSessionFactory.getSession();
return session;
}
//添加数据
public void add(Object obj) {
}
//修改数据
public void mod(Object obj) {
}
//普通查询
public List list() {
return null;
}
public List jlist(Object query, String countsql)
{
Query q=null;
Criteria cri=null;
List li=null;
boolean bool=false;;
//得到记录总数
if(query instanceof Query) {
q=(Query)query;
bool=true;
intCountic = getCountJsql(countsql);
}
else if(query instanceof Criteria) {
cri=(Criteria)query;
li=cri.list();
if(li.size()==0)
intCountic=0;
else
intCountic=li.size();
}
else
{
System.out.println("出错提示:参数不正确");
return null;
}
try
{
//每页记录数
if (PageSize == 0)
PageSize = 20;
//获得url,如 /user/china.jsp
HttpFile = request.getRequestURI();
//获得当前页数
nowPage = request.getParameter("pages");
//取得网址是的参数
PageParameter = this.urlPath(request);
//如果没有pages参数,则让页数为1
if (nowPage == null) {
intPage = 1;
}
else {
intPage = Integer.parseInt(nowPage);
if (intPage < 1) {
intPage = 1;
}
}
//得到分页总数
intPageCount = ((intCountic + PageSize) - 1) / PageSize;
if (intPage > intPageCount) {
intPage = intPageCount;
}
// 得到list集合
if(bool)
list = q.setFirstResult((intPage - 1) * PageSize).setMaxResults(PageSize).list();
else
list =cri.setFirstResult((intPage - 1) * PageSize).setMaxResults(PageSize).list();
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
}
//将分页和页脚传到request中
request.setAttribute("list", list);
request.setAttribute("foot", PageFooter());
return list;
}
public List jlist(Query query)
{
String[] str = query.getQueryString().split("from");
String countsql ="select count(*) from "+str[1].trim();
return jlist(query,countsql);
}
public List jlist(Criteria query)
{
return jlist(query,"");
}
//汇总语句,得到记录总数
public int getCountJsql(String countsql) {
Query q = getSession().createQuery(countsql);
List cc = q.list();
Integer a = (Integer) cc.get(0);
return a.intValue();
}
//页脚显示
/**
* 显示分页内容
* @return
*/
public String PageFooter() {
if (list.size() <= 0) {
return "对不起,暂无记录!";
}
String style = " /n";
String str = "";
int prev = intPage - 1;
int next = intPage + 1;
str = str
+ "
共计:[" + intCountic + "]条 [" + intPageCount + "]页"; str = str + " 第[" + getIntPage() + "]页 | "; str = str + " str+=" |
/**
* 对有参数的网址进行改造。。
* 如 index.jsp?id=23&class=23
*/
public String urlPath(HttpServletRequest request) {
String path = "";
String pagepath = "pages=";
String url = request.getQueryString();
//如果无参数
if (url == null || url.equals("")) {
return pagepath;
}
List lista = new ArrayList();
StringTokenizer ss = new StringTokenizer(url, "&");
while (ss.hasMoreTokens()) {
String s = ss.nextToken();
if (s.indexOf("pages") == -1)
lista.add(s);
}
for (int i = 0; i < lista.size(); i++) {
String param = "";
try {
param = new String(lista.get(i).toString().getBytes(
"iso-8859-1"), "gb2312");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
path += param + "&";
}
return path + pagepath;
}
public int getIntCountic() {
return intCountic;
}
public void setIntCountic(int intCountic) {
this.intCountic = intCountic;
}
public int getIntPage() {
return intPage;
}
public void setIntPage(int intPage) {
this.intPage = intPage;
}
public int getIntPageCount() {
return intPageCount;
}
public void setIntPageCount(int intPageCount) {
this.intPageCount = intPageCount;
}
public String getNowPage() {
return nowPage;
}
public void setNowPage(String nowPage) {
this.nowPage = nowPage;
}
public int getPageSize() {
return PageSize;
}
public void setPageSize(int pageSize) {
PageSize = pageSize;
}
}
/**
* 注意事项:
* 1、可传递Query或Critera参数
* 2、传递Query对象时,不能使用包括?号的HQL语句。
* 3、使用Critera时,是将所有数据读到List中,从而获得记录数,经测试5万条记录内,差别不明显
*
*/
产品dao类(具体dao):
package com.xdf.dao;