Skip to content

Commit

Permalink
Example构造方法增加notNull参数,默认false,允许值为null,值为null的时候不加入到条件中。
Browse files Browse the repository at this point in the history
  • Loading branch information
abel533 committed Jan 5, 2016
1 parent 1bc107d commit 18fd7d9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
4 changes: 4 additions & 0 deletions src/main/java/tk/mybatis/mapper/entity/Condition.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ public Condition(Class<?> entityClass) {
public Condition(Class<?> entityClass, boolean exists) {
super(entityClass, exists);
}

public Condition(Class<?> entityClass, boolean exists, boolean notNull) {
super(entityClass, exists, notNull);
}
}
46 changes: 32 additions & 14 deletions src/main/java/tk/mybatis/mapper/entity/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public class Example {

protected boolean exists;

protected boolean notNull;

protected Set<String> selectColumns;

protected List<Criteria> oredCriteria;
Expand All @@ -63,13 +65,25 @@ public Example(Class<?> entityClass) {
}

/**
* 带exists参数的构造方法
* 带exists参数的构造方法,默认notNull为false,允许为空
*
* @param entityClass
* @param exists - true时,如果字段不存在就抛出异常,false时,如果不存在就不使用该字段的条件
*/
public Example(Class<?> entityClass, boolean exists) {
this(entityClass, exists, false);
}

/**
* 带exists参数的构造方法
*
* @param entityClass
* @param exists - true时,如果字段不存在就抛出异常,false时,如果不存在就不使用该字段的条件
* @param notNull - true时,如果值为空,就会抛出异常,false时,如果为空就不使用该字段的条件
*/
public Example(Class<?> entityClass, boolean exists, boolean notNull) {
this.exists = exists;
this.notNull = notNull;
oredCriteria = new ArrayList<Criteria>();
this.entityClass = entityClass;
table = EntityHelper.getEntityTable(entityClass);
Expand Down Expand Up @@ -146,7 +160,7 @@ public Criteria createCriteria() {
}

protected Criteria createCriteriaInternal() {
Criteria criteria = new Criteria(propertyMap, exists);
Criteria criteria = new Criteria(propertyMap, exists, notNull);
return criteria;
}

Expand All @@ -160,16 +174,15 @@ protected abstract static class GeneratedCriteria {
protected List<Criterion> criteria;
//字段是否必须存在
protected boolean exists;
//值是否不能为空
protected boolean notNull;
//属性和列对应
protected Map<String, EntityColumn> propertyMap;

protected GeneratedCriteria(Map<String, EntityColumn> propertyMap) {
this(propertyMap, true);
}

protected GeneratedCriteria(Map<String, EntityColumn> propertyMap, boolean exists) {
protected GeneratedCriteria(Map<String, EntityColumn> propertyMap, boolean exists, boolean notNull) {
super();
this.exists = exists;
this.notNull = notNull;
criteria = new ArrayList<Criterion>();
this.propertyMap = propertyMap;
}
Expand Down Expand Up @@ -218,7 +231,11 @@ protected void addCriterion(String condition) {

protected void addCriterion(String condition, Object value, String property) {
if (value == null) {
throw new RuntimeException("Value for " + property + " cannot be null");
if (notNull) {
throw new RuntimeException("Value for " + property + " cannot be null");
} else {
return;
}
}
if (property == null) {
return;
Expand All @@ -228,7 +245,11 @@ protected void addCriterion(String condition, Object value, String property) {

protected void addCriterion(String condition, Object value1, Object value2, String property) {
if (value1 == null || value2 == null) {
throw new RuntimeException("Between values for " + property + " cannot be null");
if (notNull) {
throw new RuntimeException("Between values for " + property + " cannot be null");
} else {
return;
}
}
if (property == null) {
return;
Expand Down Expand Up @@ -384,12 +405,9 @@ public Criteria andEqualTo(Object param) {
}

public static class Criteria extends GeneratedCriteria {
protected Criteria(Map<String, EntityColumn> propertyMap) {
super(propertyMap);
}

protected Criteria(Map<String, EntityColumn> propertyMap, boolean exists) {
super(propertyMap, exists);
protected Criteria(Map<String, EntityColumn> propertyMap, boolean exists, boolean notNull) {
super(propertyMap, exists, notNull);
}
}

Expand Down

0 comments on commit 18fd7d9

Please sign in to comment.