码迷,mamicode.com
首页 > 其他好文 > 详细

J2EE开发框架搭建(3) - 查询条件封装

时间:2014-08-28 14:49:59      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:j2ee开发框架   spring4   springmvc4   hibernate4   druid   

这一节主要想要说明一下框架中的查询条件接口

1. 条件查询的整个类结构图

bubuko.com,布布扣

2. 在hqhop-framework-common项目下面查询条件的的目录结构:

bubuko.com,布布扣

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"));




J2EE开发框架搭建(3) - 查询条件封装

标签:j2ee开发框架   spring4   springmvc4   hibernate4   druid   

原文地址:http://blog.csdn.net/asdewq380303318/article/details/38895571

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!