Controller
@RequestMapping("/yderpList.do")
@ResponseBody
public Map list(YDERP yderp, int page, int rows, String startTime, String endTime){
log.info("###查询接入ERP列表开始");
Page<YDERP> result = yderpService.findAll(
page-1,
rows,
yderp,
startTime, endTime);
Map map = new HashMap();
map.put("total",result.getTotalElements());
map.put("rows",result.getContent());
return map;
}
Service
@Override
public Page<YDERP> findAll(int start, int end, final YDERP yderp, final String createDateTimeStart, final String createDateTimeEnd) {
Pageable pageable = new PageRequest(start, end, Sort.Direction.ASC,"id")
final Page<YDERP> page = yderpDao.findAll(new Specification<YDERP>() {
@Override
public Predicate toPredicate(Root<YDERP> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
List<Predicate> list = new ArrayList<Predicate>()
if(StringUtils.isNotBlank(yderp.getErpName())){
list.add(criteriaBuilder.like(root.get("erpName").as(String.class), "%"+yderp.getErpName()+"%"))
}
if(StringUtils.isNotBlank(yderp.getClientId())){
list.add(criteriaBuilder.equal(root.get("clientId"),yderp.getClientId()))
}
if(StringUtils.isNotBlank(yderp.getJoinMode())){
list.add(criteriaBuilder.equal(root.get("joinMode"),yderp.getJoinMode()))
}
if(StringUtils.isNotBlank(yderp.getStatus())){
list.add(criteriaBuilder.equal(root.get("status"),yderp.getStatus()))
}
if(StringUtils.isNotBlank(yderp.getOrgId())){
list.add(criteriaBuilder.equal(root.get("orgId"),yderp.getOrgId()))
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd")
SimpleDateFormat sdfmat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
if(createDateTimeStart!=null && createDateTimeEnd!=null){
try {
list.add(criteriaBuilder.between(root.<Date>get("createDatetime"),
sdfmat.parse(sdfmat.format(sdf.parse(createDateTimeStart).getTime())),
sdfmat.parse(sdfmat.format(sdf.parse(createDateTimeEnd).getTime() + 86400000))))
} catch (ParseException e) {
e.printStackTrace()
}
}
Predicate[] predicates = new Predicate[list.size()]
return criteriaBuilder.and(list.toArray(predicates))
}
},pageable)
return page
}