通过代码生成机制的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语句,觉得更加直观。

转载于:https://www.cnblogs.com/disappearwind/p/4642470.html

Appfuse:扩展自己的GenericManager相关推荐

  1. Appfuse:记录操作日志

    appfuse的数据维护操作都发生在***form页面,与之对应的是***FormController,在Controller中处理数据的操作是onSubmit方法,既然所有的操作都通过onSubmi ...

  2. 使用 AppFuse 的七个理由之二

    理由 1:测试 测试是在软件开发项目中很少被给予足够信任的一个环节.注意我并不是说在软件开发的一些刊物中没有得到足够的信任!很多文章和案例研究都给出了测试优先的开发方式和足够的测试覆盖面以提高软件的质 ...

  3. 使用 AppFuse 的七个理由(中英文两版)

    使用 AppFuse 的七个理由 学习 Java 开放源码工具 -- 并使用这些工具提高生产效率 文档选项 <script language="JavaScript" typ ...

  4. appfuse的常见错误及其解决方法

    4.经常出现的错误 1.eclipse maven插件,报错:Plugin execution not covered by lifecycle configuration:  解决办法: 升级ecl ...

  5. J2EE开发平台:Eclipse之Appfuse浅析

    很久没来更新过Blog了,工作忙啊,从J2ME转换到J2EE,需要学习的东西好好多啊,再加上这两年纷至沓来的各种框架,简直让人有点目不暇接啊,但是,没办法啊,所有的用人单位都需要你这个的会,那个也得会 ...

  6. VS Code 安装 Go 插件、自定义扩展配置、断点调试

    1. 安装插件 使用快捷键 Ctrl+Shift+X 打开插件安装页面,安装 Go 插件. 2. 自定义扩展配置 使用快捷键 Ctrl+, 打开自定义配置页,编辑 settings.json ,定义与 ...

  7. VS Code 安装插件、自定义模板、自定义配置参数、自定义主题、配置参数说明、常用的扩展插件

    1. 下载和官网教程 下载地址:https://code.visualstudio.com/ 官方教程:https://code.visualstudio.com/docs 2. 安装插件 安装扩展插 ...

  8. gcc 自动识别的文件扩展名,gcc/g++ -x 选项指定语言,不同 gcc 版本 -std 编译选项支持列表

    对于执行 C 或者 C++ 程序,需要借助 gcc(g++)指令来调用 GCC 编译器. 对于以 .c 为扩展名的文件,GCC 会自动将其视为 C 源代码文件 对于以 .cpp 为扩展名的文件,GCC ...

  9. 用动态实现扩展TVM

    用动态实现扩展TVM Extending TVM with Dynamic Execution Outline ● Motivation for Dynamism ● Representing Dyn ...

最新文章

  1. 怎么读懂python语句_Python入门基础知识点总结,一点文章就能让你看懂Python
  2. oracle 游标中抛出异常的处理方式
  3. 最近很火的 ClickHouse 是什么?
  4. NFV业务技术说明—Vecloud微云
  5. 基于微服务API级权限的技术架构
  6. 总谐波失真80_如何将总谐波失真降至 10% 以下
  7. 微信支付金额为0.01分报错,和少一分钱的解决办法
  8. jsp中使用echarts简单示例
  9. PHP前后端分离 数据格式,前后端分离项目,标准json协议格式参考
  10. Flash Professional CS6 安装zxp插件
  11. zigbee网络各层的主要功能
  12. 「x86」- 特权级(Privilege Level)学习笔记 @20210215
  13. Apache2 Windows安装与HTTP Server Digest 认证
  14. java生成不可修改的pdf_好記性不如爛筆頭4-JAVA生成PDF文件
  15. [DataAnalysis]数据分析和大数据入门推荐书单
  16. 什么是欧拉角/姿态角?
  17. 做一个有时间观念的人
  18. jzoj4024 [佛山市选2015]石子游戏
  19. 如何彻底卸载\删除android设备上预装app
  20. VS错误 CS0120 对象引用对于非静态的字段、方法或属性

热门文章

  1. mysql消息订阅与发布_消息发布与订阅
  2. html选择拖动条,纯CSS做的滑动范围选择条
  3. java将数组加上千分号_PHP实现对数字分隔加千分号的方法
  4. 数据解决方案:原力大数据教你如何撰写数据分析报告
  5. R语言︱文本挖掘套餐包之——XML+SnowballC+tm包
  6. 992. Sort Array By Parity II - LeetCode
  7. 也来谈一谈js的浅复制和深复制
  8. Android---真机调试时不能识别手机的解决方案
  9. android sdk更新后出现please update ADT to the latest ve
  10. ERROR: modinfo: could not find module rbd FATAL