赞
踩
第一种:hql分页(不推荐)需要手动关闭session连接
//PageBean.java
@SuppressWarnings("hiding")
public class PageBean<T> {
private int currPage;//当前页数
private int pageSize;//每页显示的记录数
private int total; //总计录数
private int totalPage;//总页数
private List<T> rows;//每页显示的数据
public int getCurrPage() {
return currPage;
}
public void setCurrPage(int currPage) {
this.currPage = currPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public List<T> getRows() {
return rows;
}
public void setRows(List<T> rows) {
this.rows = rows;
}
}
//DaoImpl
//分页展示User
@SuppressWarnings("unchecked")
@Override
public List<User> getUserByPage(User user, int begin, int pageSize) {
String hql = "from User where isdeleted=0 ";
//拼装查询条件
hql = getHqlWhere(user, hql);
hql+=" order by modifytime desc";
Session session = this.getHibernateTemplate().getSessionFactory().openSession();
Query query = session.createQuery(hql);
query.setFirstResult(begin);
query.setMaxResults(pageSize);
List<User> userList = query.list();
releaseSession(session);
return userList;
}
public int getUserCount(User user){
String hql = "select count(*) from User where isdeleted=0 ";
//拼装查询条件
hql = getHqlWhere(user, hql);
List<Long> list = this.getHibernateTemplate().find(hql);
if(list.size()>0){
return list.get(0).intValue();
}
return 0;
}
//hql查询条件拼装方法
private String getHqlWhere(User user,String hql){
if(user!=null){
if(user.getUsername()!=null&&!user.getUsername().isEmpty()){
hql+=" and username like '%"+user.getUsername()+"%'";
}
if(user.getZsxm()!=null&&!user.getZsxm().isEmpty()){
hql+=" and zsxm like '%"+user.getZsxm()+"%'";
}
if(user.getFjid()!=null&&!user.getFjid().isEmpty()&&!user.getFjid().equals("0")){
hql+=" and fjid='"+user.getFjid()+"'";
}
}
return hql;
}
//serverImpl
@Override
/**
* user分页查询方法
* @param user 存储查询条件
* @param currPage 当前页数
* @param pageSize 每页显示多少行
* @return
*/
public PageBean<User> getUserByPage(User user, Integer currPage, Integer pageSize) {
PageBean<User> pageBean = new PageBean<User>();
pageBean.setCurrPage(currPage);//当前页数
pageBean.setPageSize(pageSize);//每页显示多少行
//总记录数
int totalCount = userDao.getUserCount(user);
pageBean.setTotal(totalCount);
//总页数
double tc = totalCount;
Double num = Math.ceil(tc/pageSize);//存在小数位直接进一
pageBean.setTotalPage(num.intValue());
//封装每页显示的数据
int begin = (currPage-1)*pageSize;
List<User> userList = userDao.getUserByPage(user, begin, pageSize);
pageBean.setRows(userList);
return pageBean;
}
//Action();
public void list() {
PageBean<User> pageBean = userService.getUserByPage(user, page, rows);
Gson gson = new Gson();
try {
System.out.println(gson.toJson(pageBean));
//json格式输出分页查询数据
Utils.sendMsg(gson.toJson(pageBean));
} catch (IOException e) {
e.printStackTrace();
}
}
第二种:DetachedCriteria (推荐使用)
//分页展示User
@SuppressWarnings("unchecked")
@Override
public List<User> getUserByPage(User user, int begin, int pageSize) {
DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
if(user!=null){
if(user.getUsername()!=null&&!user.getUsername().isEmpty()){
DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
if(user!=null){
if(user.getUsername()!=null&&!user.getUsername().isEmpty()){
//criteria.add(Restrictions.like("username", "%"+user.getUsername()+"%"));
criteria.add(Restrictions.like("username", user.getUsername(),MatchMode.ANYWHERE));
}
if(user.getZsxm()!=null&&!user.getZsxm().isEmpty()){
criteria.add(Restrictions.like("zsxm", "%"+user.getZsxm()+"%"));
}
if(user.getFjid()!=null&&!user.getFjid().isEmpty()&&!user.getFjid().equals("0")){
criteria.add(Restrictions.eq("fjid", user.getFjid()));
}
}
List<User> userList = this.getHibernateTemplate().findByCriteria(criteria, begin, pageSize);
return userList;
}
备注:若DetachedCriteria where 后面多个条件是or
/*
* Hibernate 查询MatchMode的四种模式
MatchMode.START:字符串在最前面的位置.相当于"like 'key%'"
MatchMode.END:字符串在最后面的位置.相当于"like '%key'"
MatchMode.ANYWHERE:字符串在中间匹配.相当于"like '%key%'"
MatchMode.EXACT:字符串精确匹配.相当于"like 'key'"
*/
//sql where or
Disjunction disjunction = Restrictions.disjunction();
disjunction.add(Restrictions.like("tel",contacts.getSearchTel(),MatchMode.ANYWHERE));
disjunction.add(Restrictions.like("mphone",contacts.getSearchTel(),MatchMode.ANYWHERE));
disjunction.add(Restrictions.like("ephone",contacts.getSearchTel(),MatchMode.ANYWHERE));
criteria.add(disjunction);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。