现在做持久化基本上都会用到ORM,现在最通用的解决方案当然就是Hibernate以及由之引申出来的EJB3的持久化方案,我们在做DAO层的时候经常是直接写HQL,并不是说它不好,而是不够通用。例如本来有个功能是查询某种类型的User,但是需要额外的过滤和排序,那么就需要拼HQL的,不太好维护,所以有一个面向对象的查询接口还是比较好。
下面就是这个雏形,是我现在的项目中用到的,抛砖引玉吧。
下面是全部的类和主要的方法,其他的方法都忽略了,可以到Google Code下载全部的源代码。
/**
* Search info needed by client, only provide additional conditions and sorts.
*/
public class SearchInfo implements Serializable
{
/**
* Additional conditions for some query info provided by service.
*/
private List<Condition> conditions = new ArrayList<Condition>();
/**
* Sort info.
*/
private List<Sort> sorts = new ArrayList<Sort>();
/**
* Page size.
*/
private int pageSize = Constants.DEFAULT_PAGE_SIZE;
/**
* First return row.
*/
private int firstRow;
/**
* Whether need return total rows.
*/
private boolean getTotalRows;
/**
* All query info needed to construct a QL string.
*/
public class QueryInfo extends SearchInfo implements Serializable
{
public static final String COUNT_COLUMN = "totalrows";
/**
* Select fields.
*/
private List<Field> selects = new ArrayList<Field>();
/**
* From tagets.
*/
private List<From> froms = new ArrayList<From>();
/**
* Group by fields.
*/
private List<Field> groupBys = new ArrayList<Field>();
/**
* Having conditions.
*/
private List<Condition> havings = new ArrayList<Condition>();
/**
* Native hints.
*/
private List<String> hints = new ArrayList<String>();
/**
* Fetch size.
*/
private int fetchSize;
/**
* Search result by search info.
*/
public class SearchResult
{
/**
* Total rows matched, if search info do not let get total rows, it will return -1.
*/
private int totalRows=-1;
/**
* Indicate if any more matched result can be returned
*/
private boolean hasMore;
/**
* Page result only.
*/
private List pageResult = new ArrayList();
/**
* Search condition, used in where and having part of QL.
*/
public class Condition implements Serializable
{
/**
* Condtion field, can be null, for example, for not, and and or Condition.
*/
private Field field;
/**
* Operator.
*/
private Operator operator;
/**
* Condition value.
*/
private Object value;
/**
* Query field, used in select, condition, sort, having and sort part of QL.
*/
public class Field implements Serializable
{
/**
* From target of field.
*/
private From from;
/**
* Field name.
*/
private String name;
/**
* Aggregation function.
*/
private Function function;
/**
* Filed alias, especially needed when Function is not null.
*/
private String alias;
/**
* Query target, used in from part of QL, Field also need reference to it.
*/
public class From implements Serializable
{
public static final Class DIRECT_TABLE_CLASS= DirectTable.class;
private static final String DIRECT_TABLE_SEPERATOR=" ";
/**
* Model class type. If is direct table
*/
private Class target;
/**
* Alias for model class or table.
*/
private String alias;
/**
* Aggregation function.
*/
public enum Function implements Serializable
{
count,sum,max,min,avg
}
/**
* Operator for condition
*/
public enum Operator implements Serializable
{
equal("="), notEqual("!="), like("like"), isNull("is null"),
isNotNull("is not null"), greater(">"), greaterOrEqual(">="),
less("<"), lessOrEqual("<="), in("in"), and("and"),
or("or"), not("not");
private final String value;
Operator(String value)
{
this.value = value;
}
public String toString()
{
return value;
}
}
/**
* Sort info, used in sort part of QL.
*/
public class Sort implements Serializable
{
/**
* Sort field.
*/
private Field field;
/**
* Sort order.
*/
private boolean asc;
发表评论