Spring Boot使用JPA多条件查询mysql(2)
- UID
- 1066743
|
Spring Boot使用JPA多条件查询mysql(2)
新建实体相关的接口继承JpaSpecificationExecutor
QualityCheckRepository.java
代码如下:
package com.biologic.api.service;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Service;
import com.biologic.entity.QualityCheck;
@Service
public interface QualityCheckRepository extends JpaRepository<QualityCheck, Long>, JpaSpecificationExecutor<QualityCheck> {
}
使用方法
重写findAll中的new Specification()方法和public Predicate toPredicate(Root root, CriteriaQuery
package com.biologic.api;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.biologic.api.service.QualityCheckRepository;
import com.biologic.entity.QualityCheck;
@Controller
@RequestMapping(path = "/demo")
public class QualityCheckController {
@Autowired
private QualityCheckRepository qualityCheckRepository;
@GetMapping(path = "/qualityCheck")
public @ResponseBody String qualityCheck() {
QualityCheck model = new QualityCheck();
model.setQualified(1);
model.setProcessed(0);
List<QualityCheck> results = findSearch(model);
for (QualityCheck qualityCheck : results) {
System.out.println("正在质检:" + qualityCheck.getId());
qualityCheck.setProcessed(1);
qualityCheckRepository.save(qualityCheck);
}
return "ok";
}
public List<QualityCheck> findSearch(QualityCheck model) {
List<QualityCheck> result = qualityCheckRepository.findAll(new Specification<QualityCheck>() {
@Override
public Predicate toPredicate(Root<QualityCheck> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
List<Predicate> list = new ArrayList<Predicate>();
list.add(cb.equal(root.get("qualified").as(Integer.class), model.getQualified()));
list.add(cb.equal(root.get("processed").as(Integer.class), model.getProcessed()));
if (model.getVersion() != null && !model.getVersion().equals("")) {
list.add(cb.equal(root.get("version").as(String.class), model.getVersion()));
}
if (model.getVersion() != null && !model.getVersion().equals("")) {
list.add(cb.like(root.get("version").as(String.class), "%" + model.getVersion() + "%"));
}
// 日期
if (model.getUpload_time() != null) {
Date startDate = model.getUpload_time();
Date endDate = new Date();
// 处理字符串时间
// SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
// try {
// startDate = format.parse(date);
// } catch (ParseException e) {
// startDate = new Date(946656000000L);//2000 01 01
// }
// endDate = startDate;
// Calendar calendar = Calendar.getInstance() ;
// calendar.setTime(endDate);
// calendar.add(Calendar.DATE, 1);
// endDate = calendar.getTime();
// calendar = null;
list.add(cb.between(root.<Date>get("upload_time"), startDate, endDate));
}
Predicate[] p = new Predicate[list.size()];
return cb.and(list.toArray(p));
}
});
return result;
}
} |
|
|
|
|
|