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) 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相关推荐
- Appfuse:记录操作日志
appfuse的数据维护操作都发生在***form页面,与之对应的是***FormController,在Controller中处理数据的操作是onSubmit方法,既然所有的操作都通过onSubmi ...
- 使用 AppFuse 的七个理由之二
理由 1:测试 测试是在软件开发项目中很少被给予足够信任的一个环节.注意我并不是说在软件开发的一些刊物中没有得到足够的信任!很多文章和案例研究都给出了测试优先的开发方式和足够的测试覆盖面以提高软件的质 ...
- 使用 AppFuse 的七个理由(中英文两版)
使用 AppFuse 的七个理由 学习 Java 开放源码工具 -- 并使用这些工具提高生产效率 文档选项 <script language="JavaScript" typ ...
- appfuse的常见错误及其解决方法
4.经常出现的错误 1.eclipse maven插件,报错:Plugin execution not covered by lifecycle configuration: 解决办法: 升级ecl ...
- J2EE开发平台:Eclipse之Appfuse浅析
很久没来更新过Blog了,工作忙啊,从J2ME转换到J2EE,需要学习的东西好好多啊,再加上这两年纷至沓来的各种框架,简直让人有点目不暇接啊,但是,没办法啊,所有的用人单位都需要你这个的会,那个也得会 ...
- VS Code 安装 Go 插件、自定义扩展配置、断点调试
1. 安装插件 使用快捷键 Ctrl+Shift+X 打开插件安装页面,安装 Go 插件. 2. 自定义扩展配置 使用快捷键 Ctrl+, 打开自定义配置页,编辑 settings.json ,定义与 ...
- VS Code 安装插件、自定义模板、自定义配置参数、自定义主题、配置参数说明、常用的扩展插件
1. 下载和官网教程 下载地址:https://code.visualstudio.com/ 官方教程:https://code.visualstudio.com/docs 2. 安装插件 安装扩展插 ...
- gcc 自动识别的文件扩展名,gcc/g++ -x 选项指定语言,不同 gcc 版本 -std 编译选项支持列表
对于执行 C 或者 C++ 程序,需要借助 gcc(g++)指令来调用 GCC 编译器. 对于以 .c 为扩展名的文件,GCC 会自动将其视为 C 源代码文件 对于以 .cpp 为扩展名的文件,GCC ...
- 用动态实现扩展TVM
用动态实现扩展TVM Extending TVM with Dynamic Execution Outline ● Motivation for Dynamism ● Representing Dyn ...
最新文章
- 手把手教你 MongoDB 的安装与详细使用(一)
- linux硬链接与软链接
- matlab路面,赛道道路路况分析问题 matlab高手进下
- Linux 进程通信fifo,Linux 进程通信之FIFO的实现
- [SQL] 函数整理(T-SQL 版)
- Emlog百度快速收录插件
- 按钮、菜单的重绘代码
- eslint 换行_给 eslint 写一个插件
- php debugdumpparams,PHP PDOStatement::debugDumpParams讲解
- centos6.5系统执行mv /* /path,只能用cd命令,如何恢复
- 形式化方法对软件开发的挑战:一些历史与现实
- linux 基础知识大全(收藏这一篇就够了)
- IO流和NIO流的区别
- ST-LINK Utility + ST LINK+ STM32G474 erase chip fails
- 呼吸灯(Verilog)
- java中的常见异常1 -- 新手上路,减速慢行
- hadoop2.3.0 Windows (nutch)爬虫使用
- 843 Guess the Word
- dota自走棋寻找不到服务器,《DOTA自走棋》服务器不对怎么办 服务器不对解决方法介绍...
- 源码编译系列之mpv播放器编译教程