整合的过程可以看这篇文章 https://blog.csdn.net/lushizhuo9655/article/details/107494081
需求一:完成带有搜索功能的分页客户列表
效果图:
1)先页面显示出来数据
//set和get方法省略
public class PageBean {
private Integer currentPage;//当前页
private Integer pageSize; //每页显示的条数
private Integer totalPage; //总页数
private Integer totalSize; //总条数
private List list; //每页显示的数据
//总条数的目的是为了获取总页数 当前页和每页显示的条数是页面传过来的 可以提前解决隐患
public PageBean(Integer currentPage, Integer pageSize, Integer totalSize) {
this.currentPage = currentPage;
this.pageSize = pageSize;
this.totalSize = totalSize;
if(this.currentPage ==null){
//如页面没有指定显示那一页.显示第一页.
this.currentPage=1;
}
if(this.pageSize==null){
//如果每页显示条数没有指定,默认每页显示3条
this.pageSize=3;
}
//计算总页数
this.totalPage =this.totalSize%this.pageSize==0?this.totalSize/this.pageSize:this.totalSize/this.pageSize+1;
//判断当前页数是否超出范围
//不能小于1
if(this.currentPage <1){
this.currentPage=1;
}
//不能大于总页数
if(this.currentPage>this.totalPage){
this.currentPage=this.totalPage;
}
}
//计算起始索引
public Integer getStart(){
return (this.currentPage-1)*this.pageSize;
}
}
//set和get方法省略
public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{
private Customer customer=new Customer();
private Integer currentPage;
private Integer pageSize;
private CustomerService customerService;
public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}
public String getList(){
DetachedCriteria dc=DetachedCriteria.forClass(Customer.class);
if(StringUtils.isNotBlank(customer.getCust_name())){
dc.add(Restrictions.like("cust_name","%"+customer.getCust_name()+"%"));
}
PageBean pageBean=customerService.getPageBean(dc,currentPage,pageSize);
ActionContext.getContext().put("pageBean", pageBean);
return "list";
}
@Override
public Customer getModel() {
return customer;
}
}
public class CustomerServiceImpl implements CustomerService{
private CustomerDao customerDao;
public PageBean getPageBean(DetachedCriteria dc, Integer currentPage, Integer pageSize) {
Integer totalSize=customerDao.getTotalCount(dc);
PageBean pageBean=new PageBean(currentPage, pageSize, totalSize);
List<Customer> list=customerDao.getList(dc,pageBean.getStart(),pageBean.getPageSize());
pageBean.setList(list);
return pageBean;
}
public void setCustomerDao(CustomerDao customerDao) {
this.customerDao = customerDao;
}
}
public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {
@Override
public Integer getTotalCount(DetachedCriteria dc) {
//设置查询的聚合函数,总记录数
dc.setProjection(Projections.rowCount());
List<Long> list=(List<Long>) this.getHibernateTemplate().findByCriteria(dc);
//清空之前设置的聚合函数
dc.setProjection(null);
if(list !=null && list.size()>0){
Long count = list.get(0);
return count.intValue();
}else{
return null;
}
}
@Override
public List<Customer> getList(DetachedCriteria dc, Integer start, Integer pageSize) {
//自动提供了一个分页查询的语句
List<Customer> list=(List<Customer>) this.getHibernateTemplate().findByCriteria(dc, start, pageSize);
return list;
}
}
2)解决一些琐碎的问题
页面中无论改变了这三个的任何一个,都应该提交这三个参数
准备一个表单,将这三个参数放到一个form中提交,每次都让表单提交
页面可以通过点击前一页,后一页,到第几页可以正常到达显示
在点击页面,希望页码保存下来,因此用个回显数据
每页显示条数
但是条件查询有问题,等有空在调试调试 :
解决方法:在数据库配置上加上?characterEncoding=UTF-8(编码问题)
设计baseDao
public interface BaseDao<T>{
//增
void save(T t);
//删
void delete(T t);
//删
void delete(Serializable id);
//改
void update(T t);
//查 根据id查询
T getById(Serializable id);
//查 符合条件的总记录数
Integer getTotalCount(DetachedCriteria dc);
//查 查询分页列表数据
List<T> getPageList(DetachedCriteria dc,Integer start,Integer pageSize);
}
public class BaseDaoImpl<T> extends HibernateDaoSupport implements BaseDao<T> {
//用于接收运行期泛型类型
private Class clazz;
public BaseDaoImpl(){
ParameterizedType ptClass = (ParameterizedType) this.getClass().getGenericSuperclass();
//获得运行期的泛型类型
clazz = (Class) ptClass.getActualTypeArguments()[0];
}
@Override
public void save(T t) {
getHibernateTemplate().save(t);
}
@Override
public void delete(T t) {
getHibernateTemplate().delete(t);
}
@Override
public void delete(Serializable id) {
T t=this.getById(id);//先取,再删
getHibernateTemplate().delete(t);
}
@Override
public void update(T t) {
getHibernateTemplate().update(t);
}
public T getById(Serializable id) {
return (T) getHibernateTemplate().get(clazz, id);
}
public Integer getTotalCount(DetachedCriteria dc) {
//设置查询的聚合函数,总记录数
dc.setProjection(Projections.rowCount());
List<Long> list = (List<Long>) getHibernateTemplate().findByCriteria(dc);
//清空之前设置的聚合函数
dc.setProjection(null);
if(list!=null && list.size()>0){
Long count = list.get(0);
return count.intValue();
}else{
return null;
}
}
public List<T> getPageList(DetachedCriteria dc, Integer start, Integer pageSize) {
List<T> list = (List<T>) getHibernateTemplate().findByCriteria(dc, start, pageSize);
return list;
}
}
public interface CustomerDao extends BaseDao<Customer>{
}
public class CustomerDaoImpl extends BaseDaoImpl<Customer> implements CustomerDao {
}