Appfuse:擴充套件自己的GenericManager

消失的風發表於2015-07-13

通過程式碼生成機制的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) managers
10  * for your domain objects.
11  *
12  * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
13  *  Updated by jgarcia: added full text search + reindexing
14  * @param <T> a type variable
15  * @param <PK> the primary key for that type
16  */
17 public interface GenericManager<T, PK extends Serializable> {
18 
19     /**
20      * Generic method used to get all objects of a particular type. This
21      * is the same as lookup up all rows in a table.
22      * @return List of populated objects
23      */
24     List<T> getAll();
25 
26     /**
27      * Generic method to get an object based on class and identifier. An
28      * ObjectRetrievalFailureException Runtime Exception is thrown if
29      * nothing is found.
30      *
31      * @param id the identifier (primary key) of the object to get
32      * @return a populated object
33      * @see org.springframework.orm.ObjectRetrievalFailureException
34      */
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 get
40      * @return - true if it exists, false if it doesn't
41      */
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 save
47      * @return the updated object
48      */
49     T save(T object);
50 
51     /**
52      * Generic method to delete an object
53      * @param object the object to remove
54      */
55     void remove(T object);
56 
57     /**
58      * Generic method to delete an object based on class and id
59      * @param id the identifier (primary key) of the object to remove
60      */
61     void remove(PK id);
62 
63     /**
64      * Generic method to search for an object.
65      * @param searchTerm the search term
66      * @param clazz type of class to search for.
67      * @return a list of matched objects
68      */
69     List<T> search(String searchTerm, Class clazz);
70     /**
71      * Generic method to regenerate full text index of the persistent class T
72      */
73     void reindex();
74 
75     /**
76      * Generic method to regenerate full text index of all indexed classes
77      *
78      * @param async
79      *            true to perform the reindexing asynchronously
80      */
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語句,覺得更加直觀。

相關文章