上述方法之间的区别在于以下代码片段的正确性:
,其中
Criteria criteria = new Criteria();
criteria.where("name").is("Jon")
.where("age").is(20)
.where("gender").is("male");
和
Criteria criteria = new Criteria()
criteria.and("name").is("Jon")
.and("age").is(20)
.and("gender").is("male");
混合
Criteria criteria = new Criteria()
criteria.where("name").is("Jon")
.and("age").is(20)
.and("gender").is("male");
应该使用哪一个来附加新条件?
例如
Criteria criteria = new Criteria()
criteria.where("name").is("Jon")
.and("age").is(20);
if(isComplex)
criteria.and("gender").is("male");
请在使用它们时给我一些例子。
答案 0 :(得分:1)
正如你从后端实现看到的这两个函数作为伙伴
/**
* Static factory method to create a Criteria using the provided key
*
* @param key
* @return
*/
public static Criteria where(String key) {
return new Criteria(key);
}
/**
* Static factory method to create a Criteria using the provided key
*
* @return
*/
public Criteria and(String key) {
return new Criteria(this.criteriaChain, key);
}
所以在和Method Criteria类中使用其数据成员criteriaChain是一个List ...所以你可以将Criteria列表与方法关联,但不与where关联。
这是Criteria Class(Partial)
public class Criteria implements CriteriaDefinition {
/**
* Custom "not-null" object as we have to be able to work with {@literal null} values as well.
*/
private static final Object NOT_SET = new Object();
private String key;
private List<Criteria> criteriaChain; \\This datamember is used when you are using and