如何使用 AND 和 OR 条件生成 crudrepository findBy 方法签名?
How to generate crudrepository findBy method signature with AND and OR Conditions?
如何在 JPARepository 中生成对应于以下查询的 findBy 方法?
select * from person where name=?1 and eff_dt < ?2 and (exp_dt >?2 OR exp_dt IS NULL)
技术上你可以做到。但最好避免它。
假设您的实体 class 就像
class Person {
String name;
Integer effDt;
Integer expDt;
// getters and setters
}
您的方法名称将是
findByNameIsAndEffDtLessThanAndExpDtGreaterThanOrNameIsAndEffDtLessThanAndExpDtIsNull(String name, Integer effDt, Integer expDt, String name2, Integer effDt2);
//name and name2 will be same & effDt and effDt2 will be same.
//Boolean Algebra: A(B+C) = A.B + A.C
如您所见,这不必要地复杂且难以理解。请改用本机查询。
select * 来自 name=?1 and eff_dt < ?2 and (exp_dt >?2 OR exp_dt IS NULL)
通过变量的名称假设实体 class
@Entity
public class Person{
@Id
@GeneratedValue
private long id;
private String name;
private Date eff_dt;
private Date exp_dt;
}
查询可以
public final static String FIND_ALL_PERSONS="select s from Person p where p.name=:name and p.eff_dt < :eff_dt and (exp_dt > : exp_dt OR exp_dt is null)";
@Query(FIND_ALL_PERSONS)
List<Person> findAllPersons(@Param("name")String name,@Param("eff_dt") eff_dt,@Param("exp_dt") Date axp_dt);
如何在 JPARepository 中生成对应于以下查询的 findBy 方法?
select * from person where name=?1 and eff_dt < ?2 and (exp_dt >?2 OR exp_dt IS NULL)
技术上你可以做到。但最好避免它。
假设您的实体 class 就像
class Person {
String name;
Integer effDt;
Integer expDt;
// getters and setters
}
您的方法名称将是
findByNameIsAndEffDtLessThanAndExpDtGreaterThanOrNameIsAndEffDtLessThanAndExpDtIsNull(String name, Integer effDt, Integer expDt, String name2, Integer effDt2);
//name and name2 will be same & effDt and effDt2 will be same.
//Boolean Algebra: A(B+C) = A.B + A.C
如您所见,这不必要地复杂且难以理解。请改用本机查询。
select * 来自 name=?1 and eff_dt < ?2 and (exp_dt >?2 OR exp_dt IS NULL) 通过变量的名称假设实体 class
@Entity
public class Person{
@Id
@GeneratedValue
private long id;
private String name;
private Date eff_dt;
private Date exp_dt;
}
查询可以
public final static String FIND_ALL_PERSONS="select s from Person p where p.name=:name and p.eff_dt < :eff_dt and (exp_dt > : exp_dt OR exp_dt is null)";
@Query(FIND_ALL_PERSONS)
List<Person> findAllPersons(@Param("name")String name,@Param("eff_dt") eff_dt,@Param("exp_dt") Date axp_dt);