这一节主要想要说明一下框架中的查询条件接口
1. 条件查询的整个类结构图
2. 在hqhop-framework-common项目下面查询条件的的目录结构:
3. 打开SearchOperator.java , 该类包含了所有的查询操作符,是一个枚举类型:
public enum SearchOperator { eq("等于", "="), ne("不等于", "!="), gt("大于", ">"), gte("大于等于", ">="), lt("小于", "<"), lte("小于等于", "<="), prefixLike("前缀模糊匹配", "like"), prefixNotLike("前缀模糊不匹配", "not like"), suffixLike("后缀模糊匹配", "like"), suffixNotLike("后缀模糊不匹配", "not like"), like("模糊匹配", "like"), notLike("不匹配", "not like"), isNull("空", "is null"), isNotNull("非空", "is not null"), in("包含", "in"), notIn("不包含", "not in"), custom("自定义默认的", null); private final String info; private final String symbol; SearchOperator(final String info, String symbol) { this.info = info; this.symbol = symbol; } public String getInfo() { return info; } public String getSymbol() { return symbol; } public static String toStringAllOperator() { return Arrays.toString(SearchOperator.values()); } /** * 操作符是否允许为空 * * @param operator * @return */ public static boolean isAllowBlankValue(final SearchOperator operator) { return operator == SearchOperator.isNotNull || operator == SearchOperator.isNull; } public static SearchOperator valueBySymbol(String symbol) throws SearchException { symbol = formatSymbol(symbol); for (SearchOperator operator : values()) { if (operator.getSymbol().equals(symbol)) { return operator; } } throw new SearchException("SearchOperator not method search operator symbol : " + symbol); } private static String formatSymbol(String symbol) { if (Utils.isEmpty(symbol)) { return symbol; } return symbol.trim().toLowerCase().replace(" ", " "); } }
4. Searchable抽象类的主要实现都是在SearchRequest中完成的,可以代码较多可以直接查询
5. 查询接口的使用方式:
1) 只有单个单个条件(查询用户age=21的记录)
Searchable searchable = Searchable.newSearchable(); searchable.addSearchFilter("age", SearchOperator.eq, 21);
2) 两个查询条件,用and连接(查询用户name=‘silentwu‘ and age=21的记录
Searchable searchable = Searchable.newSearchable(); searchable.and(SearchFilterHelper.newCondition("age", SearchOperator.eq, 21), SearchFilterHelper.newCondition("name", SearchOperator.eq, "silentwu"));
3. 两个查询条件,用or连接
Searchable searchable = Searchable.newSearchable(); searchable.or(SearchFilterHelper.newCondition("age", SearchOperator.eq, 21), SearchFilterHelper.newCondition("name", SearchOperator.eq, "silentwu"));
时间: 2024-10-08 06:04:19