hibernate 分页问题 谁给我发一个分页的例子,struts2+hibernate 结合的。在网上没寻找到合适的……
515368056@QQ.com或者page-taglib的那种,不要在jsp出现大量java代码的。好的再奖励200!jsp+action+dao我想理清分页逻辑……...
515368056@QQ.com 或者page-taglib的那种,不要在jsp出现大量java代码的。好的再奖励200!
jsp+action+dao 我想理清分页逻辑…… 展开
jsp+action+dao 我想理清分页逻辑…… 展开
2个回答
展开全部
package cn.edie.ams.bulletin.action;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.Query;
import org.hibernate.Session;
import cn.edie.ams.db.ConnectionDB;
import cn.edie.ams.dept.model.Dept;
import cn.edie.ams.utils.ExceptionHandle;
import cn.edie.ams.utils.JsonWriter;
import cn.edie.ams.utils.PagingHandle;
import cn.edie.ams.utils.StringHandle;
import com.opensymphony.xwork.ActionSupport;
/**
* 公告管理-查询
* @author Edie.gq
*
*/
@SuppressWarnings("serial")
public class SearchBulletinList extends ActionSupport {
private String json = "";
private int page = -1;
private int size = -1;
private int totalRecords = -1;
//--------查询参数-----------
private String startTime;
private String endTime;
private String title;
private String content;
private String userId;
private String sign;
//-------------------------
@SuppressWarnings("unchecked")
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
Session session = null;
Query query = null;
try {
session = ConnectionDB.getSession();
Map<String, String> sMap = new HashMap<String, String>();
String sqlbase = this.getSqlbase(sMap);
String sqlpage="select count(b.id) "+sqlbase;
/*分页*/
query = session.createQuery(sqlpage);
query.setProperties(sMap);
totalRecords=Integer.parseInt(query.uniqueResult().toString());
/*查询数据*/
query = session.createQuery(sqlbase);
query.setProperties(sMap);
Map<String, Integer> pMap = PagingHandle.getPagingParams(page, size, totalRecords);
query.setFirstResult(pMap.get("first"));
query.setMaxResults(pMap.get("size"));
List<Dept> list = query.list();
Map<String, Object> jsonMap =PagingHandle.getResultMap(pMap, "bulletins", list);
jsonMap.put("rows", pMap.get("rows"));
jsonMap.put("count", pMap.get("count"));
jsonMap.put("first", pMap.get("first")+1);
jsonMap.put("last", pMap.get("last")+1);
JsonWriter j = new JsonWriter();
json = j.write(jsonMap);
} catch (Exception e) {
e.printStackTrace();
json=ExceptionHandle.handle(e);
} finally{
ConnectionDB.closeSession(session);
}
return SUCCESS;
}
public String getSqlbase(Map<String, String> sMap){
StringBuffer sqlbase = new StringBuffer();
if("incept".equals(sign)){
sqlbase.append("from BulletinIncept b where b.inceptId='"+userId+"' ");
}else if("send".equals(sign)){
sqlbase.append("from BulletinSend b where b.sendId='"+userId+"' ");
}else if("draft".equals(sign)){
sqlbase.append("from BulletinDraft b where b.sendId='"+userId+"' ");
}else if("del".equals(sign)){
sqlbase.append("from BulletinDel b where b.delUser='"+userId+"' ");
}
if(!StringHandle.isNull(startTime)){
sqlbase.append("and b.sendTime>='"+startTime+"' ");
}
if(!StringHandle.isNull(endTime)){
sqlbase.append("and b.sendTime<='"+endTime+"' ");
}
if(!StringHandle.isNull(title)){
sqlbase.append("and b.title like '"+title+"' ");
}
if(!StringHandle.isNull(content)){
sqlbase.append("and b.content like '"+content+"' ");
}
sqlbase.append("order by b.sendTime desc");
return sqlbase.toString();
}
public String getJson() {
return json;
}
public void setJson(String json) {
this.json = json;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public int getTotalRecords() {
return totalRecords;
}
public void setTotalRecords(int totalRecords) {
this.totalRecords = totalRecords;
}
public String getStartTime() {
return startTime;
}
public void setStartTime(String startTime) {
this.startTime = startTime;
}
public String getEndTime() {
return endTime;
}
public void setEndTime(String endTime) {
this.endTime = endTime;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getSign() {
return sign;
}
public void setSign(String sign) {
this.sign = sign;
}
}
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.Query;
import org.hibernate.Session;
import cn.edie.ams.db.ConnectionDB;
import cn.edie.ams.dept.model.Dept;
import cn.edie.ams.utils.ExceptionHandle;
import cn.edie.ams.utils.JsonWriter;
import cn.edie.ams.utils.PagingHandle;
import cn.edie.ams.utils.StringHandle;
import com.opensymphony.xwork.ActionSupport;
/**
* 公告管理-查询
* @author Edie.gq
*
*/
@SuppressWarnings("serial")
public class SearchBulletinList extends ActionSupport {
private String json = "";
private int page = -1;
private int size = -1;
private int totalRecords = -1;
//--------查询参数-----------
private String startTime;
private String endTime;
private String title;
private String content;
private String userId;
private String sign;
//-------------------------
@SuppressWarnings("unchecked")
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
Session session = null;
Query query = null;
try {
session = ConnectionDB.getSession();
Map<String, String> sMap = new HashMap<String, String>();
String sqlbase = this.getSqlbase(sMap);
String sqlpage="select count(b.id) "+sqlbase;
/*分页*/
query = session.createQuery(sqlpage);
query.setProperties(sMap);
totalRecords=Integer.parseInt(query.uniqueResult().toString());
/*查询数据*/
query = session.createQuery(sqlbase);
query.setProperties(sMap);
Map<String, Integer> pMap = PagingHandle.getPagingParams(page, size, totalRecords);
query.setFirstResult(pMap.get("first"));
query.setMaxResults(pMap.get("size"));
List<Dept> list = query.list();
Map<String, Object> jsonMap =PagingHandle.getResultMap(pMap, "bulletins", list);
jsonMap.put("rows", pMap.get("rows"));
jsonMap.put("count", pMap.get("count"));
jsonMap.put("first", pMap.get("first")+1);
jsonMap.put("last", pMap.get("last")+1);
JsonWriter j = new JsonWriter();
json = j.write(jsonMap);
} catch (Exception e) {
e.printStackTrace();
json=ExceptionHandle.handle(e);
} finally{
ConnectionDB.closeSession(session);
}
return SUCCESS;
}
public String getSqlbase(Map<String, String> sMap){
StringBuffer sqlbase = new StringBuffer();
if("incept".equals(sign)){
sqlbase.append("from BulletinIncept b where b.inceptId='"+userId+"' ");
}else if("send".equals(sign)){
sqlbase.append("from BulletinSend b where b.sendId='"+userId+"' ");
}else if("draft".equals(sign)){
sqlbase.append("from BulletinDraft b where b.sendId='"+userId+"' ");
}else if("del".equals(sign)){
sqlbase.append("from BulletinDel b where b.delUser='"+userId+"' ");
}
if(!StringHandle.isNull(startTime)){
sqlbase.append("and b.sendTime>='"+startTime+"' ");
}
if(!StringHandle.isNull(endTime)){
sqlbase.append("and b.sendTime<='"+endTime+"' ");
}
if(!StringHandle.isNull(title)){
sqlbase.append("and b.title like '"+title+"' ");
}
if(!StringHandle.isNull(content)){
sqlbase.append("and b.content like '"+content+"' ");
}
sqlbase.append("order by b.sendTime desc");
return sqlbase.toString();
}
public String getJson() {
return json;
}
public void setJson(String json) {
this.json = json;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public int getTotalRecords() {
return totalRecords;
}
public void setTotalRecords(int totalRecords) {
this.totalRecords = totalRecords;
}
public String getStartTime() {
return startTime;
}
public void setStartTime(String startTime) {
this.startTime = startTime;
}
public String getEndTime() {
return endTime;
}
public void setEndTime(String endTime) {
this.endTime = endTime;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getSign() {
return sign;
}
public void setSign(String sign) {
this.sign = sign;
}
}
展开全部
//DAO层
public List<User> findAll(String hsql) {
Session session = getHibernateTemplate().getSessionFactory().openSession();
Query query = session.createQuery(hsql);
query.setFirstResult(0);
query.setMaxResults(10);
List<User> list = (List<User>) query.list();
session.close();
return list;
//server层
public List<User> findAllUser(User user) {
String hsql="from User";
return userDao.findAll(hsql);
}
//ACTION
public String findAll(){
List<User> list = userServer.findAllUser(user);
ServletActionContext.getRequest().setAttribute("list", list);
return "list";
}
加入这三个就分页了,页面用struts标签迭代输出就可以了
我发了个demo给你
public List<User> findAll(String hsql) {
Session session = getHibernateTemplate().getSessionFactory().openSession();
Query query = session.createQuery(hsql);
query.setFirstResult(0);
query.setMaxResults(10);
List<User> list = (List<User>) query.list();
session.close();
return list;
//server层
public List<User> findAllUser(User user) {
String hsql="from User";
return userDao.findAll(hsql);
}
//ACTION
public String findAll(){
List<User> list = userServer.findAllUser(user);
ServletActionContext.getRequest().setAttribute("list", list);
return "list";
}
加入这三个就分页了,页面用struts标签迭代输出就可以了
我发了个demo给你
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询