Hibernate按条件查询

     /**
     * 按条件查询用户数量
     * @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;
    }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值