你的位置:首页 > Java教程

[Java教程]Appfuse:扩展自己的GenericManager

通过代码生成机制的appfuse访问数据都通过GenericManager来实现,GenericManager默认提供了以下几个方法:

 1 package org.appfuse.service; 2  3 import java.io.Serializable; 4 import java.util.List; 5  6 /** 7  * Generic Manager that talks to GenericDao to CRUD POJOs. 8  * 9  * <p>Extend this interface if you want typesafe (no casting necessary) managers10  * for your domain objects.11  *12  * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>13  * Updated by jgarcia: added full text search + reindexing14  * @param <T> a type variable15  * @param <PK> the primary key for that type16 */17 public interface GenericManager<T, PK extends Serializable> {18 19   /**20    * Generic method used to get all objects of a particular type. This21    * is the same as lookup up all rows in a table.22    * @return List of populated objects23   */24   List<T> getAll();25 26   /**27    * Generic method to get an object based on class and identifier. An28    * ObjectRetrievalFailureException Runtime Exception is thrown if29    * nothing is found.30    *31    * @param id the identifier (primary key) of the object to get32    * @return a populated object33    * @see org.springframework.orm.ObjectRetrievalFailureException34   */35   T get(PK id);36 37   /**38    * Checks for existence of an object of type T using the id arg.39    * @param id the identifier (primary key) of the object to get40    * @return - true if it exists, false if it doesn't41   */42   boolean exists(PK id);43 44   /**45    * Generic method to save an object - handles both update and insert.46    * @param object the object to save47    * @return the updated object48   */49   T save(T object);50 51   /**52    * Generic method to delete an object53    * @param object the object to remove54   */55   void remove(T object);56 57   /**58    * Generic method to delete an object based on class and id59    * @param id the identifier (primary key) of the object to remove60   */61   void remove(PK id);62 63   /**64    * Generic method to search for an object.65    * @param searchTerm the search term66    * @param clazz type of class to search for.67    * @return a list of matched objects68   */69   List<T> search(String searchTerm, Class clazz);70   /**71    * Generic method to regenerate full text index of the persistent class T72   */73   void reindex();74 75   /**76    * Generic method to regenerate full text index of all indexed classes77    *78    * @param async79    *      true to perform the reindexing asynchronously80   */81   void reindexAll(boolean async);82 }

GenericManager

通常我们用getAll()访问表中所有的数据,可惜无排序;用search(String searchTerm, Class clazz)来过滤数据,可惜不能自定义条件,只能全字段搜索。

下面是我在开发过程中扩展出来的几个方法,直接附上GenericDaoHibernate中的实现,各层的声明就没再累赘。

1. 自定义排序的getAll

1 public List<T> getAll(String order) {2     Session sess = getSession();3     Criteria criteria = sess.createCriteria(persistentClass);4     criteria.addOrder(Order.desc(order));5     System.out.println(criteria);6     return criteria.list();7   }

getAll

2. 自定义HQL语句的查询

1 public List<T> selectDataByHql(String hql) {2     Session session = getSession();3     Query query=session.createQuery(hql);4     //执行查询,返回对象集合 5     List<T> allClasses = query.list();6     return allClasses;7   }

selectDataByHql

3. 根据某一列字段精确匹配的数据,并可排序,比如State=1

1 public List<T> search(String property,Object value,String order) throws SearchException {2     Session sess = getSession();3     Criteria cri= sess.createCriteria(persistentClass); 4     if(StringUtils.isNotBlank(order)){5       cri.addOrder(Order.desc(order));6     }7     cri.add(Restrictions.eq(property,value));8     return cri.list();9   }

search

4. 自定义条件的查询

1 public List<T> search(Criterion query,String order) throws SearchException {2     Session sess = getSession();3     Criteria cri= sess.createCriteria(persistentClass); 4     if(StringUtils.isNotBlank(order)){5       cri.addOrder(Order.desc(order));6     }7     cri.add(query);8     return cri.list();9   }

search

 

有人可能疑问为啥定义了4,还要再定义2呢,只能说编码风格的问题,我喜欢用4的方式,但有人喜欢HQL语句,觉得更加直观。