我数据库对应:publish_time datetime
实体:private Date publishTime;
开始时间和结束时间用String接收
private String startTimeStr;
private String endTimeStr;
我这直接用jpa的一种写法(EntityManager原生sql写法后续补上)
话不多直接贴代码:
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import javax.persistence.criteria.Predicate;
@Override
public Page findPage(BZSentimentVo vo) throws BusinessException {
Page<BZSentiment> bzHotLineList = null;
Pageable pageable = new PageRequest(0, 10, Sort.Direction.DESC, "fcd");
bzHotLineList = bzSentimentRepository.findAll((root, query, cb) -> {
List<Predicate> predicates = new ArrayList<Predicate>();
// 处理时间范围
if (vo.getStartTimeStr() != null) {
//大于或等于传入时间
predicates.add(cb.greaterThanOrEqualTo(root.get("publishTime").as(String.class), vo.getStartTimeStr()));
}
if (vo.getEndTimeStr() != null) {
//小于或等于传入时间
predicates.add(cb.lessThanOrEqualTo(root.get("publishTime").as(String.class), vo.getEndTimeStr()));
}
if (StringUtils.isNotBlank(vo.getT().getOpinCode())) {
//模糊查找
predicates.add(cb.like(root.get("opinCode").as(String.class), "%" + vo.getT().getOpinCode() + "%"));
}
if(StringUtils.isNotEmpty(vo.getT().getOpinLevel())){
predicates.add(cb.equal(root.get("opinLevel").as(String.class),vo.getT().getOpinLevel()));
}
return cb.and(predicates.toArray(new Predicate[predicates.size()]));
}, pageable);
return bzHotLineList;
}
相关资料: