Skip to content

Commit e8164a4

Browse files
committed
Example增加复杂的and 和 or 功能,SqlHelper 中复杂的 if 改为 choose 方式。
1 parent 2e233f7 commit e8164a4

File tree

3 files changed

+305
-34
lines changed

3 files changed

+305
-34
lines changed

src/main/java/tk/mybatis/mapper/entity/Example.java

Lines changed: 241 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,18 +232,33 @@ public List<Criteria> getOredCriteria() {
232232
}
233233

234234
public void or(Criteria criteria) {
235+
criteria.setAndOr("or");
235236
oredCriteria.add(criteria);
236237
}
237238

238239
public Criteria or() {
239240
Criteria criteria = createCriteriaInternal();
241+
criteria.setAndOr("or");
242+
oredCriteria.add(criteria);
243+
return criteria;
244+
}
245+
246+
public void and(Criteria criteria) {
247+
criteria.setAndOr("and");
248+
oredCriteria.add(criteria);
249+
}
250+
251+
public Criteria and() {
252+
Criteria criteria = createCriteriaInternal();
253+
criteria.setAndOr("and");
240254
oredCriteria.add(criteria);
241255
return criteria;
242256
}
243257

244258
public Criteria createCriteria() {
245259
Criteria criteria = createCriteriaInternal();
246260
if (oredCriteria.size() == 0) {
261+
criteria.setAndOr("and");
247262
oredCriteria.add(criteria);
248263
}
249264
return criteria;
@@ -280,6 +295,8 @@ protected abstract static class GeneratedCriteria {
280295
protected boolean exists;
281296
//值是否不能为空
282297
protected boolean notNull;
298+
//连接条件
299+
protected String andOr;
283300
//属性和列对应
284301
protected Map<String, EntityColumn> propertyMap;
285302

@@ -311,6 +328,14 @@ private String property(String property) {
311328
}
312329
}
313330

331+
public String getAndOr() {
332+
return andOr;
333+
}
334+
335+
public void setAndOr(String andOr) {
336+
this.andOr = andOr;
337+
}
338+
314339
public boolean isValid() {
315340
return criteria.size() > 0;
316341
}
@@ -361,6 +386,44 @@ protected void addCriterion(String condition, Object value1, Object value2, Stri
361386
criteria.add(new Criterion(condition, value1, value2));
362387
}
363388

389+
protected void addOrCriterion(String condition) {
390+
if (condition == null) {
391+
throw new MapperException("Value for condition cannot be null");
392+
}
393+
if (condition.startsWith("null")) {
394+
return;
395+
}
396+
criteria.add(new Criterion(condition, true));
397+
}
398+
399+
protected void addOrCriterion(String condition, Object value, String property) {
400+
if (value == null) {
401+
if (notNull) {
402+
throw new MapperException("Value for " + property + " cannot be null");
403+
} else {
404+
return;
405+
}
406+
}
407+
if (property == null) {
408+
return;
409+
}
410+
criteria.add(new Criterion(condition, value, true));
411+
}
412+
413+
protected void addOrCriterion(String condition, Object value1, Object value2, String property) {
414+
if (value1 == null || value2 == null) {
415+
if (notNull) {
416+
throw new MapperException("Between values for " + property + " cannot be null");
417+
} else {
418+
return;
419+
}
420+
}
421+
if (property == null) {
422+
return;
423+
}
424+
criteria.add(new Criterion(condition, value1, value2, true));
425+
}
426+
364427
public Criteria andIsNull(String property) {
365428
addCriterion(column(property) + " is null");
366429
return (Criteria) this;
@@ -529,6 +592,145 @@ public Criteria andAllEqualTo(Object param) {
529592
}
530593
return (Criteria) this;
531594
}
595+
596+
public Criteria orIsNull(String property) {
597+
addOrCriterion(column(property) + " is null");
598+
return (Criteria) this;
599+
}
600+
601+
public Criteria orIsNotNull(String property) {
602+
addOrCriterion(column(property) + " is not null");
603+
return (Criteria) this;
604+
}
605+
606+
public Criteria orEqualTo(String property, Object value) {
607+
addOrCriterion(column(property) + " =", value, property(property));
608+
return (Criteria) this;
609+
}
610+
611+
public Criteria orNotEqualTo(String property, Object value) {
612+
addOrCriterion(column(property) + " <>", value, property(property));
613+
return (Criteria) this;
614+
}
615+
616+
public Criteria orGreaterThan(String property, Object value) {
617+
addOrCriterion(column(property) + " >", value, property(property));
618+
return (Criteria) this;
619+
}
620+
621+
public Criteria orGreaterThanOrEqualTo(String property, Object value) {
622+
addOrCriterion(column(property) + " >=", value, property(property));
623+
return (Criteria) this;
624+
}
625+
626+
public Criteria orLessThan(String property, Object value) {
627+
addOrCriterion(column(property) + " <", value, property(property));
628+
return (Criteria) this;
629+
}
630+
631+
public Criteria orLessThanOrEqualTo(String property, Object value) {
632+
addOrCriterion(column(property) + " <=", value, property(property));
633+
return (Criteria) this;
634+
}
635+
636+
public Criteria orIn(String property, Iterable values) {
637+
addOrCriterion(column(property) + " in", values, property(property));
638+
return (Criteria) this;
639+
}
640+
641+
public Criteria orNotIn(String property, Iterable values) {
642+
addOrCriterion(column(property) + " not in", values, property(property));
643+
return (Criteria) this;
644+
}
645+
646+
public Criteria orBetween(String property, Object value1, Object value2) {
647+
addOrCriterion(column(property) + " between", value1, value2, property(property));
648+
return (Criteria) this;
649+
}
650+
651+
public Criteria orNotBetween(String property, Object value1, Object value2) {
652+
addOrCriterion(column(property) + " not between", value1, value2, property(property));
653+
return (Criteria) this;
654+
}
655+
656+
public Criteria orLike(String property, String value) {
657+
addOrCriterion(column(property) + " like", value, property(property));
658+
return (Criteria) this;
659+
}
660+
661+
public Criteria orNotLike(String property, String value) {
662+
addOrCriterion(column(property) + " not like", value, property(property));
663+
return (Criteria) this;
664+
}
665+
666+
/**
667+
* 手写条件
668+
*
669+
* @param condition 例如 "length(countryname)<5"
670+
* @return
671+
*/
672+
public Criteria orCondition(String condition) {
673+
addOrCriterion(condition);
674+
return (Criteria) this;
675+
}
676+
677+
/**
678+
* 手写左边条件,右边用value值
679+
*
680+
* @param condition 例如 "length(countryname)="
681+
* @param value 例如 5
682+
* @return
683+
*/
684+
public Criteria orCondition(String condition, Object value) {
685+
criteria.add(new Criterion(condition, value, true));
686+
return (Criteria) this;
687+
}
688+
689+
/**
690+
* 将此对象的不为空的字段参数作为相等查询条件
691+
*
692+
* @param param 参数对象
693+
* @author Bob {@link}0haizhu0@gmail.com
694+
* @Date 2015年7月17日 下午12:48:08
695+
*/
696+
public Criteria orEqualTo(Object param) {
697+
MetaObject metaObject = SystemMetaObject.forObject(param);
698+
String[] properties = metaObject.getGetterNames();
699+
for (String property : properties) {
700+
//属性和列对应Map中有此属性
701+
if (propertyMap.get(property) != null) {
702+
Object value = metaObject.getValue(property);
703+
//属性值不为空
704+
if (value != null) {
705+
orEqualTo(property, value);
706+
}
707+
}
708+
}
709+
return (Criteria) this;
710+
}
711+
712+
/**
713+
* 将此对象的所有字段参数作为相等查询条件,如果字段为 null,则为 is null
714+
*
715+
* @param param 参数对象
716+
*/
717+
public Criteria orAllEqualTo(Object param) {
718+
MetaObject metaObject = SystemMetaObject.forObject(param);
719+
String[] properties = metaObject.getGetterNames();
720+
for (String property : properties) {
721+
//属性和列对应Map中有此属性
722+
if (propertyMap.get(property) != null) {
723+
Object value = metaObject.getValue(property);
724+
//属性值不为空
725+
if (value != null) {
726+
orEqualTo(property, value);
727+
} else {
728+
orIsNull(property);
729+
}
730+
}
731+
}
732+
return (Criteria) this;
733+
}
532734
}
533735

534736
public static class Criteria extends GeneratedCriteria {
@@ -545,6 +747,8 @@ public static class Criterion {
545747

546748
private Object secondValue;
547749

750+
private String andOr;
751+
548752
private boolean noValue;
549753

550754
private boolean singleValue;
@@ -556,39 +760,62 @@ public static class Criterion {
556760
private String typeHandler;
557761

558762
protected Criterion(String condition) {
763+
this(condition, false);
764+
}
765+
766+
protected Criterion(String condition, Object value, String typeHandler) {
767+
this(condition, value, typeHandler, false);
768+
}
769+
770+
protected Criterion(String condition, Object value) {
771+
this(condition, value, null, false);
772+
}
773+
774+
protected Criterion(String condition, Object value, Object secondValue, String typeHandler) {
775+
this(condition, value, secondValue, typeHandler, false);
776+
}
777+
778+
protected Criterion(String condition, Object value, Object secondValue) {
779+
this(condition, value, secondValue, null, false);
780+
}
781+
782+
protected Criterion(String condition, boolean isOr) {
559783
super();
560784
this.condition = condition;
561785
this.typeHandler = null;
562786
this.noValue = true;
787+
this.andOr = isOr ? "or" : "and";
563788
}
564789

565-
protected Criterion(String condition, Object value, String typeHandler) {
790+
protected Criterion(String condition, Object value, String typeHandler, boolean isOr) {
566791
super();
567792
this.condition = condition;
568793
this.value = value;
569794
this.typeHandler = typeHandler;
795+
this.andOr = isOr ? "or" : "and";
570796
if (value instanceof Collection<?>) {
571797
this.listValue = true;
572798
} else {
573799
this.singleValue = true;
574800
}
575801
}
576802

577-
protected Criterion(String condition, Object value) {
578-
this(condition, value, null);
803+
protected Criterion(String condition, Object value, boolean isOr) {
804+
this(condition, value, null, isOr);
579805
}
580806

581-
protected Criterion(String condition, Object value, Object secondValue, String typeHandler) {
807+
protected Criterion(String condition, Object value, Object secondValue, String typeHandler, boolean isOr) {
582808
super();
583809
this.condition = condition;
584810
this.value = value;
585811
this.secondValue = secondValue;
586812
this.typeHandler = typeHandler;
587813
this.betweenValue = true;
814+
this.andOr = isOr ? "or" : "and";
588815
}
589816

590-
protected Criterion(String condition, Object value, Object secondValue) {
591-
this(condition, value, secondValue, null);
817+
protected Criterion(String condition, Object value, Object secondValue, boolean isOr) {
818+
this(condition, value, secondValue, null, isOr);
592819
}
593820

594821
public String getCondition() {
@@ -603,6 +830,14 @@ public Object getSecondValue() {
603830
return secondValue;
604831
}
605832

833+
public String getAndOr() {
834+
return andOr;
835+
}
836+
837+
public void setAndOr(String andOr) {
838+
this.andOr = andOr;
839+
}
840+
606841
public boolean isNoValue() {
607842
return noValue;
608843
}

0 commit comments

Comments
 (0)