/**
* 按条件查询用户数量
* @param accountQueryDTO
* @return
*/
public Integer selectAccountQueryCount(AccountQueryDTO accountQueryDTO) {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
Account.class);
// 判断用户查询
if (accountQueryDTO.getUserName() != null
&& accountQueryDTO.getUserName().trim().length() > 0) {
criteria.add(Restrictions.like("commonName", "%"
+ accountQueryDTO.getUserName().trim() + "%"));
}
// 判断用户邮箱查询
if (accountQueryDTO.getEmail() != null
&& accountQueryDTO.getEmail().trim().length() > 0) {
criteria.add(Restrictions.like("email", "%"
+ accountQueryDTO.getEmail().trim() + "%"));
}
int size = ((Integer)criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
return size;
}
/**
* 按条件查询用户列表
* @param accountQueryDTO
* @return
*/
@SuppressWarnings("unchecked")
public List<Account> selectAccountQuery(AccountQueryDTO accountQueryDTO) {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
Account.class);
// 判断用户查询
if (accountQueryDTO.getUserName() != null
&& accountQueryDTO.getUserName().trim().length() > 0) {
criteria.add(Restrictions.like("commonName", "%"
+ accountQueryDTO.getUserName().trim() + "%"));
}
// 判断用户邮箱查询
if (accountQueryDTO.getEmail() != null
&& accountQueryDTO.getEmail().trim().length() > 0) {
criteria.add(Restrictions.like("email", "%"
+ accountQueryDTO.getEmail().trim() + "%"));
}
criteria.setFirstResult(accountQueryDTO.getPageNo()
* accountQueryDTO.getPageSize());
criteria.setMaxResults(accountQueryDTO.getPageSize());
List<Account> list = criteria.list();
return list;
}
存储数据的Page.java
import java.util.List;
/**
* 分页工具类
*
* @version 1.0
*/
public class Page {
/**
* 当前页码(已知)
*/
private int pageNo = 0;
/**
* 每页记录数(已知)
*/
private int pageSize;
/**
* 指定查询条件下 的记录数(已知)
*/
private int totalCount = 0;
/**
* 指定查询下的总页数(未知)
*/
private int totalPage = 1;
private List<?> list;
public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
/**
* totalCount pageSize totalPage 0 10 1 95 10 10 100 10 10
*
*
*
* @return
*/
public int getTotalPage() {
totalPage = totalCount / pageSize;
if (totalCount == 0 || totalCount % pageSize != 0) {
totalPage++;
}
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public List<?> getList() {
return list;
}
public void setList(List<?> list) {
this.list = list;
}
}
获取数据封装
public class AccountQueryDTO {
private String userName; //用户名
private String cellPhone; //手机号
private String email; //邮箱
private int pageSize; //每页显示条数
private Integer pageNo; //当前页
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getCellPhone() {
return cellPhone;
}
public void setCellPhone(String cellPhone) {
this.cellPhone = cellPhone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getPageNo() {
return pageNo;
}
public void setPageNo(Integer pageNo) {
this.pageNo = pageNo;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
SERVER层
@Override
public Page listAccount(AccountQueryDTO accountQueryDTO) {
//查询当前条件下的总记录数
Integer totalCount = accountDao.selectAccountQueryCount(accountQueryDTO);
//创建分页的page对象
Page page = new Page();
page.setPageNo(accountQueryDTO.getPageNo());
page.setTotalCount(totalCount);
page.setPageSize(accountQueryDTO.getPageSize());
//获得开始行号和结束行号
List<Account> accountList = accountDao.selectAccountQuery(accountQueryDTO);
page.setList(accountList);
return page;
}
Hibernate按条件查询
最新推荐文章于 2023-06-14 10:04:35 发布