前提:dao层是个接口.并且继承
public interface CourierRepository extends JpaRepository<Object, Integer>,JpaSpecificationExecutor<Courier> {
//如果有需要可以自己在此自定义方法 加上query update和delete 加上@Modifying注解
}
-----------------------------------------------------------------------------------------------------
//无条件分页查询
@Action(value="courier_pageQuery",results={@Result(name="success",type=
"json")})
public String page(){
Pageable pageable = new PageRequest(page-1, rows);
//获取数据封装
Page<Courier> pageData = courierService.findPageData(pageable);
//存入到map集合中 HashMap<Object,
Object> hashMap = new HashMap<>();hashMap.put("total", pageData.getTotalElements());
hashMap.put("rows",pageData.getContent());
ActionContext.getContext().getValueStack().push(hashMap);
return SUCCESS;
}
----------------------------------------------------------------------------------------------------------------------------------
//带条件的分页查询方式
@Action(value = "area_pageQuery", results = { @Result(name = "success", type = "json") })
public String queryPage() {Pageable pageable = new PageRequest(page - 1, rows);
ArrayList<Predicate> list = new ArrayList<>();
Specification<Area> specification = new Specification<Area>() {
@Override
public Predicate toPredicate(Root<Area> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
if (StringUtils.isNotBlank(model.getProvince())) {
Predicate p1 = cb.like(root.get("province").as(String.class), "%" + model.getProvince() + "%");
list.add(p1);
}
if (StringUtils.isNotBlank(model.getCity())) {
Predicate p2 = cb.like(root.get("city").as(String.class), "%" + model.getCity() + "%");
list.add(p2);
}
if (StringUtils.isNotBlank(model.getDistrict())) {
Predicate p3 = cb.like(root.get("district").as(String.class), "%" + model.getDistrict() + "%");
list.add(p3);
}
return cb.and(list.toArray(new Predicate[0]));
}
};
Page<Area> pageData = areaService.findPageData(specification, pageable);
// 存入到map集合中
//①这个方法是为自定义方法,实现方式如下
return SUCCESS;
}
//我在这里
/**
* 做分页的操作抽取
*/
protected Integer page;
protected Integer rows;
public void setPage(Integer page) {
this.page = page;
}
public void setRows(Integer rows) {
this.rows = rows;
}
protected void pushPageDataToValueStack(Page<T> pageData) {
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("total", pageData.getTotalElements());
hashMap.put("rows", pageData.getContent());
ActionContext.getContext().getValueStack().push(hashMap);
}
}
----------------------------------------------------------------------------------------------------------------------------------
//有条件的多表查询
@Action(value = "courier_pageQuery", results = { @Result(name = "success", type = "json") })
public String page() {
System.out.println("有条件分页查询数据...");
Pageable pageable = new PageRequest(page - 1, rows);
// 封装条件查询对象:
// root 是要查询的表.
// CriteriaQuery:构造简单条件查询.提供where方法
// CriteriaBuilder:.构造的是条件对象.构造复杂查询结果
Specification<Courier> specification = new Specification<Courier>() {
@Override
public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
ArrayList<Predicate> list = new ArrayList<>();
// 进行员工编号查询
if (StringUtils.isNotBlank(model.getCourierNum())) {
Predicate p1=cb.equal(root.get("courierNum").as(String.class), model.getCourierNum());
list.add(p1);
}
// 进行员工公司查询
if (StringUtils.isNotBlank(model.getCompany())) {
Predicate p2 = cb.like(root.get("companey").as(String.class), "%" + model.getCompany() + "%");
list.add(p2);
}
// 进行员工类型
if (StringUtils.isNotBlank(model.getType())) {
Predicate p3 = cb.like(root.get("type").as(String.class), model.getType());
list.add(p3);
}
// 进行多表的查询
Join<Courier, Standard> standardJoin = root.join("standard", JoinType.INNER);
if(model.getStandard()!=null&&StringUtils.isNotBlank(model.getStandard().getName())) {
Predicate p4 = cb.like(standardJoin.get("name").as(String.class),
"%" + model.getStandard().getName() + "%");
list.add(p4);
}
return cb.and(list.toArray(new Predicate[0]));
}
};
Page<Courier> pageData = courierService.findPageData(specification, pageable);
// 分页加入值栈中
pushPageDataToValueStack(pageData);
return SUCCESS;
}