添加查询条件

更多可惨见HqlHelper源码注释

(1/5)基本查询条件

无 tableAlias 参数的重载方法,tableAlias=HqlHelper.currTable 即:fromClazz 的别名

方法等价HQL片段
eq,ne,ge,gt,lt,le(String tableAlias, String fieldName, Object value)tableAlias.fieldName =,!=,>=,>,<,<= value
in,notIn(String tableAlias, String fieldName, Object… values)tableAlias.fieldName in,not in values
like,notLike(String tableAlias, String fieldName, String value, MatchType matchType)tableAlias.fieldName like,not like matchType:value
ilike,notiLike(String tableAlias, String fieldName, String value, MatchType matchType)upper(tableAlias.fieldName) like,not like upper(matchType:value)
like,notLike(String tableAlias, String fieldName, String value)tableAlias.fieldName like,not like ‘%value%’
ilike,notiLike(String tableAlias, String fieldName, String value)upper(tableAlias.fieldName) like,not like upper(‘%value%’)
isNull,isNotNull(String tableAlias, String fieldName)tableAlias.fieldName is null,is not null
between,notBetween(String tableAlias, String fieldName, Object value1, Object value2)tableAlias.fieldName between,not between value1 and value2

(2/5)双字段查询条件

无 tableAlias 参数的重载方法,tableAlias=HqlHelper.currTable 即:fromClazz 的别名

方法等价HQL片段
eq,ne,ge,gt,lt,leProperty(String tableAlias, String fieldName, String otherTableAlias, String otherFieldName)tableAlias.fieldName =,!=,>=,>,<,<= otherTableAlias.otherFieldName

(3/5)集合查询条件:一般不用

无 tableAlias 参数的重载方法,tableAlias=HqlHelper.currTable 即:fromClazz 的别名

方法等价HQL片段
sizeEq,Ge,Gt,Le,Lt,Ne(String tableAlias, String fieldName, Integer size)size(tableAlias.fieldName) =,>=,>,<=,<,!= size
isEmpty,isNotEmpty(String tableAlias, String fieldName)tableAlias.fieldName is empty,is not empty

(4/5)字符串长度查询条件:一般不用

无 tableAlias 参数的重载方法,tableAlias=HqlHelper.currTable 即:fromClazz 的别名

方法等价HQL片段
lengthEq,Ge,Gt,Le,Lt,Ne(String tableAlias, String fieldName, Integer length)length(tableAlias.fieldName) =,>=,>,<=,<,!= length
lengthEq,Ge,Gt,Le,Lt,NeProperty(String tableAlias, String fieldName, String otherTableAlias, String otherFieldName)length(tableAlias.fieldName) =,>=,>,<=,<,!= length(otherTableAlias.otherFieldName)

(5/5)条件分组

  1. .and().条件1.条件2.end() 等价HQL片段 and (条件1 and 条件2)
  2. .or().条件1.条件2.end() 等价HQL片段 or (条件1 and 条件2)

例子:

  1. @Test
  2. public void conditionGroup() {
  3. // 查询sortSeq>98,并且名称包含`天`或者`庆`字的城市 查询2条记录
  4. HqlHelper helper = HqlHelper.queryFrom(City.class);
  5. helper.fetch("id", "name")
  6. .gt("sortSeq", 98)
  7. .and()
  8. .like("name", "天").or().like("name", "庆").end()
  9. .end()
  10. .setFirstResult(0).setMaxResults(2);
  11. Records tempRecords = helperService.getRecords(helper, false);
  12. System.err.println(tempRecords);
  13. }

结果:

  1. select
  2. city0_.id as col_0_0_,
  3. city0_.name as col_1_0_
  4. from
  5. dodo_city city0_
  6. where
  7. city0_.sort_seq>?
  8. and (
  9. city0_.name like ?
  10. or city0_.name like ?
  11. ) limit ?
  12. Records [rawData=[
  13. {name=安庆市, id=1160799058548936704},
  14. {name=重庆市, id=1160799060084051968}]]