通過代碼生成機制的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 }getAll2. 自定義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 }selectDataByHql3. 根據某一列字段精確匹配的數據,并可排序,比如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 }search4. 自定義條件的查詢

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語句,覺得更加直觀。
新聞熱點
疑難解答