Skip to content

Commit

Permalink
改进对null的查询
Browse files Browse the repository at this point in the history
改进对空集合的查询
改进对like的查询
  • Loading branch information
Dhweicheng authored and abel533 committed Aug 8, 2020
1 parent afb6ffc commit 1523d57
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 9 deletions.
Expand Up @@ -17,12 +17,8 @@ private SqlCriteriaHelper() {
this.criteria = new Sqls.Criteria();
}

private SqlCriteriaHelper(Class<T> clazz) {
this.criteria = new Sqls.Criteria();
}

public static <T> SqlCriteriaHelper<T> custom(Class<T> clazz) {
return new SqlCriteriaHelper<T>(clazz);
return new SqlCriteriaHelper<T>();
}

/**
Expand Down Expand Up @@ -173,7 +169,7 @@ public SqlCriteriaHelper<T> andLessThanOrEqualTo(Fn<T, Object> fn, Object value)
* @return
*/
public SqlCriteriaHelper<T> andIn(Fn<T, Object> fn, Iterable values) {
if(Optional.ofNullable(values).isPresent()){
if(Optional.ofNullable(values).isPresent() && values.iterator().hasNext()){
this.criteria.getCriterions().add(new Sqls.Criterion(Reflections.fnToFieldName(fn), values, "in", "and"));
}
return this;
Expand All @@ -187,7 +183,7 @@ public SqlCriteriaHelper<T> andIn(Fn<T, Object> fn, Iterable values) {
* @return
*/
public SqlCriteriaHelper<T> andNotIn(Fn<T, Object> fn, Iterable values) {
if(Optional.ofNullable(values).isPresent()){
if(Optional.ofNullable(values).isPresent() && values.iterator().hasNext()){
this.criteria.getCriterions().add(new Sqls.Criterion(Reflections.fnToFieldName(fn), values, "not in", "and"));
}
return this;
Expand Down Expand Up @@ -463,7 +459,7 @@ public SqlCriteriaHelper<T> orLessThanOrEqualTo(Fn<T, Object> fn, String value)
* @return
*/
public SqlCriteriaHelper<T> orIn(Fn<T, Object> fn, Iterable values) {
if(Optional.ofNullable(values).isPresent()){
if(Optional.ofNullable(values).isPresent() && values.iterator().hasNext()){
this.criteria.getCriterions().add(new Sqls.Criterion(Reflections.fnToFieldName(fn), values, "in", "or"));
}
return this;
Expand All @@ -477,7 +473,7 @@ public SqlCriteriaHelper<T> orIn(Fn<T, Object> fn, Iterable values) {
* @return
*/
public SqlCriteriaHelper<T> orNotIn(Fn<T, Object> fn, Iterable values) {
if(Optional.ofNullable(values).isPresent()){
if(Optional.ofNullable(values).isPresent() && values.iterator().hasNext()){
this.criteria.getCriterions().add(new Sqls.Criterion(Reflections.fnToFieldName(fn), values, "not in", "or"));
}
return this;
Expand Down Expand Up @@ -523,11 +519,47 @@ public SqlCriteriaHelper<T> orNotBetween(Fn<T, Object> fn, Object value1, Object
*/
public SqlCriteriaHelper<T> orLike(Fn<T, Object> fn, String value) {
if(Optional.ofNullable(value).isPresent()){
value = "%"+value+"%";
this.criteria.getCriterions().add(new Sqls.Criterion(Reflections.fnToFieldName(fn), value, "like", "or"));
}
return this;
}


/**
* OR column LIKE %value
* 当 value = null 则当前属性不参与查询
* @param fn
* @param value
* @return
*/
public SqlCriteriaHelper<T> orLikeLeft(Fn<T, Object> fn, String value) {
if(Optional.ofNullable(value).isPresent()){
value = "%"+value;
this.criteria.getCriterions().add(new Sqls.Criterion(Reflections.fnToFieldName(fn), value, "like", "or"));
}
return this;
}




/**
* OR column LIKE value%
* 当 value = null 则当前属性不参与查询
* @param fn
* @param value
* @return
*/
public SqlCriteriaHelper<T> orLikeRight(Fn<T, Object> fn, String value) {
if(Optional.ofNullable(value).isPresent()){
value = value+"%";
this.criteria.getCriterions().add(new Sqls.Criterion(Reflections.fnToFieldName(fn), value, "like", "or"));
}
return this;
}


/**
* OR column NOT LIKE value
* 当 value = null 则当前属性不参与查询
Expand All @@ -537,11 +569,45 @@ public SqlCriteriaHelper<T> orLike(Fn<T, Object> fn, String value) {
*/
public SqlCriteriaHelper<T> orNotLike(Fn<T, Object> fn, String value) {
if(Optional.ofNullable(value).isPresent()){
value = "%"+value+"%";
this.criteria.getCriterions().add(new Sqls.Criterion(Reflections.fnToFieldName(fn), value, "not like", "or"));
}
return this;
}



/**
* OR column NOT LIKE %value
* 当 value = null 则当前属性不参与查询
* @param fn
* @param value
* @return
*/
public SqlCriteriaHelper<T> orNotLikeLeft(Fn<T, Object> fn, String value) {
if(Optional.ofNullable(value).isPresent()){
value = "%"+value+"%";
this.criteria.getCriterions().add(new Sqls.Criterion(Reflections.fnToFieldName(fn), value, "not like", "or"));
}
return this;
}

/**
* OR column NOT LIKE value%
* 当 value = null 则当前属性不参与查询
* @param fn
* @param value
* @return
*/
public SqlCriteriaHelper<T> orNotLikeRight(Fn<T, Object> fn, String value) {
if(Optional.ofNullable(value).isPresent()){
value = value+"%";
this.criteria.getCriterions().add(new Sqls.Criterion(Reflections.fnToFieldName(fn), value, "not like", "or"));
}
return this;
}


@Override
public Sqls.Criteria getCriteria() {
return criteria;
Expand Down
Expand Up @@ -7,6 +7,7 @@
import tk.mybatis.mapper.weekend.entity.Country;
import tk.mybatis.mapper.weekend.mapper.CountryMapper;

import java.util.ArrayList;
import java.util.List;

/**
Expand Down Expand Up @@ -84,4 +85,29 @@ public void like() {
sqlSession.close();
}
}

/**
* in查询 空集合问题
*/
@Test
public void list() {
SqlSession sqlSession = MybatisHelper.getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);

List<Country> selectBySqlCriteriaHelper= mapper.selectByExample(new Example.Builder(Country.class)
.where(SqlCriteriaHelper.custom(Country.class)
.andIn(Country::getCountryname, new ArrayList())
.orLike(Country::getCountryname, "A")).build());

List<Country> selectByWeekendSqls = mapper.selectByExample(new Example.Builder(Country.class)
.where(WeekendSqls.<Country>custom()
.andIn(Country::getCountryname, new ArrayList())
.orLike(Country::getCountryname, "A")).build());
//判断两个结果数组内容是否相同
Assert.assertArrayEquals(selectBySqlCriteriaHelper.toArray(), selectByWeekendSqls.toArray());
} finally {
sqlSession.close();
}
}
}

0 comments on commit 1523d57

Please sign in to comment.