使用 Spring 中的日期创建 JPA 查询
Creating JPA query with Date in Spring
这是我的实体的样子:
@Entity
public class Registration {
@Id
@GeneratedValue
private Integer id;
@org.springframework.format.annotation.DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate date;
}
这就是我的回购协议的样子:
@Query(value = "SELECT * FROM registration WHERE MONTH(date) = ?1 AND YEAR(date) = ?2")
List<Registration> findAll(Integer month, Integer year);
这将是服务:
public List<Registration> getCurrentRegistration() {
LocalDate today = LocalDate.now();
return registrationRepository.findAll(today.getMonth().getValue(), today.getYear());
}
public List<Registration> getRegistrations(Integer month, Integer year) {
return registrationRepository.findAll(month, year);
}
如何将我的本机查询更改为 JPA 查询?
JPA 查询能否在 postgresql 和 hsqldb 上运行?
为什么 JPA 查询最适合 spring 应用程序? (或者为什么他们不是)
您可以使用 QueryDSL JPA (https://github.com/querydsl/querydsl/tree/master/querydsl-jpa) 来定义谓词:
Predicate createPredicate(Integer month, Integer year) {
return QRegistration.date.year().eq(year).and(QRegistration.date.month().eq(month));
}
然后让你的 repo 扩展 QueryDslPredicateExecutor:
public interface RegistrationRepository extends JpaRepository<Registration>, QueryDslPredicateExecutor {
// Your query methods here
}
这包含一个 List<T> findAll(Predicate predicate)
方法,您可以将谓词传递给该方法以获取您之后的项目,例如:
registrationRepository.findAll(createPredicate(1, 1970));
有关将 QueryDSL 与 Spring 一起使用的更多信息,请参阅此处:https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/
做一个规范class,把下面的规范方法写进去
import javax.persistence.criteria.Predicate;
import org.springframework.data.jpa.domain.Specification;
public class RegistrationSpecification {
public static Specification<Registration > registrationSpecForDate(
LocalDate invoiceDate ) {
return (root, cq, cb) -> {
List<Predicate> predicates = new ArrayList<Predicate>();
if (invoiceDate!=(null)) {
predicates.add(cb.greaterThanOrEqualTo(root.get("date"),
invoiceDate));
}
return cb.and(predicates.toArray(new Predicate[0]));
};
}
然后在您的存储库中将此规范注入 JPA 的 findAll() 方法中。
`public List<Registration> getRegistrations(LocalDate date) {
return
registrationRepository.findAll
(RegistrationSpecification.registrationSpecForDate(date));
`
这是我的实体的样子:
@Entity
public class Registration {
@Id
@GeneratedValue
private Integer id;
@org.springframework.format.annotation.DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate date;
}
这就是我的回购协议的样子:
@Query(value = "SELECT * FROM registration WHERE MONTH(date) = ?1 AND YEAR(date) = ?2")
List<Registration> findAll(Integer month, Integer year);
这将是服务:
public List<Registration> getCurrentRegistration() {
LocalDate today = LocalDate.now();
return registrationRepository.findAll(today.getMonth().getValue(), today.getYear());
}
public List<Registration> getRegistrations(Integer month, Integer year) {
return registrationRepository.findAll(month, year);
}
如何将我的本机查询更改为 JPA 查询? JPA 查询能否在 postgresql 和 hsqldb 上运行? 为什么 JPA 查询最适合 spring 应用程序? (或者为什么他们不是)
您可以使用 QueryDSL JPA (https://github.com/querydsl/querydsl/tree/master/querydsl-jpa) 来定义谓词:
Predicate createPredicate(Integer month, Integer year) {
return QRegistration.date.year().eq(year).and(QRegistration.date.month().eq(month));
}
然后让你的 repo 扩展 QueryDslPredicateExecutor:
public interface RegistrationRepository extends JpaRepository<Registration>, QueryDslPredicateExecutor {
// Your query methods here
}
这包含一个 List<T> findAll(Predicate predicate)
方法,您可以将谓词传递给该方法以获取您之后的项目,例如:
registrationRepository.findAll(createPredicate(1, 1970));
有关将 QueryDSL 与 Spring 一起使用的更多信息,请参阅此处:https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/
做一个规范class,把下面的规范方法写进去
import javax.persistence.criteria.Predicate;
import org.springframework.data.jpa.domain.Specification;
public class RegistrationSpecification {
public static Specification<Registration > registrationSpecForDate(
LocalDate invoiceDate ) {
return (root, cq, cb) -> {
List<Predicate> predicates = new ArrayList<Predicate>();
if (invoiceDate!=(null)) {
predicates.add(cb.greaterThanOrEqualTo(root.get("date"),
invoiceDate));
}
return cb.and(predicates.toArray(new Predicate[0]));
};
}
然后在您的存储库中将此规范注入 JPA 的 findAll() 方法中。
`public List<Registration> getRegistrations(LocalDate date) {
return
registrationRepository.findAll
(RegistrationSpecification.registrationSpecForDate(date));
`