国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Appfuse:擴展自己的GenericManager

2019-11-15 00:51:30
字體:
來源:轉載
供稿:網友
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語句,覺得更加直觀。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 札达县| 剑河县| 长春市| 青河县| 嵩明县| 沛县| 永寿县| 房山区| 秭归县| 怀来县| 台北县| 东兰县| 合山市| 元氏县| 梅河口市| 霍山县| 新民市| 平湖市| 镇沅| 湘乡市| 邢台县| 贺州市| 台中县| 富宁县| 青浦区| 保定市| 双辽市| 永嘉县| 沛县| 昌都县| 酒泉市| 黑山县| 彭水| 佛山市| 富阳市| 澜沧| 土默特左旗| 图片| 洞头县| 满洲里市| 聊城市|