通过代码生成机制的appfuse访问数据都通过GenericManager来实现,GenericManager默认提供了以下几个方法:

 package org.appfuse.service;

 import java.io.Serializable;
import java.util.List; /**
* Generic Manager that talks to GenericDao to CRUD POJOs.
*
* <p>Extend this interface if you want typesafe (no casting necessary) managers
* for your domain objects.
*
* @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
* Updated by jgarcia: added full text search + reindexing
* @param <T> a type variable
* @param <PK> the primary key for that type
*/
public interface GenericManager<T, PK extends Serializable> { /**
* Generic method used to get all objects of a particular type. This
* is the same as lookup up all rows in a table.
* @return List of populated objects
*/
List<T> getAll(); /**
* Generic method to get an object based on class and identifier. An
* ObjectRetrievalFailureException Runtime Exception is thrown if
* nothing is found.
*
* @param id the identifier (primary key) of the object to get
* @return a populated object
* @see org.springframework.orm.ObjectRetrievalFailureException
*/
T get(PK id); /**
* Checks for existence of an object of type T using the id arg.
* @param id the identifier (primary key) of the object to get
* @return - true if it exists, false if it doesn't
*/
boolean exists(PK id); /**
* Generic method to save an object - handles both update and insert.
* @param object the object to save
* @return the updated object
*/
T save(T object); /**
* Generic method to delete an object
* @param object the object to remove
*/
void remove(T object); /**
* Generic method to delete an object based on class and id
* @param id the identifier (primary key) of the object to remove
*/
void remove(PK id); /**
* Generic method to search for an object.
* @param searchTerm the search term
* @param clazz type of class to search for.
* @return a list of matched objects
*/
List<T> search(String searchTerm, Class clazz);
/**
* Generic method to regenerate full text index of the persistent class T
*/
void reindex(); /**
* Generic method to regenerate full text index of all indexed classes
*
* @param async
* true to perform the reindexing asynchronously
*/
void reindexAll(boolean async);
}

GenericManager

通常我们用getAll()访问表中所有的数据,可惜无排序;用search(String searchTerm, Class clazz)来过滤数据,可惜不能自定义条件,只能全字段搜索。

下面是我在开发过程中扩展出来的几个方法,直接附上GenericDaoHibernate中的实现,各层的声明就没再累赘。

1. 自定义排序的getAll

  public List<T> getAll(String order) {
Session sess = getSession();
Criteria criteria = sess.createCriteria(persistentClass);
criteria.addOrder(Order.desc(order));
System.out.println(criteria);
return criteria.list();
}

getAll

2. 自定义HQL语句的查询

  public List<T> selectDataByHql(String hql) {
Session session = getSession();
Query query=session.createQuery(hql);
//执行查询,返回对象集合
List<T> allClasses = query.list();
return allClasses;
}

selectDataByHql

3. 根据某一列字段精确匹配的数据,并可排序,比如State=1

 public List<T> search(String property,Object value,String order) throws SearchException {
Session sess = getSession();
Criteria cri= sess.createCriteria(persistentClass);
if(StringUtils.isNotBlank(order)){
cri.addOrder(Order.desc(order));
}
cri.add(Restrictions.eq(property,value));
return cri.list();
}

search

4. 自定义条件的查询

 public List<T> search(Criterion query,String order) throws SearchException {
Session sess = getSession();
Criteria cri= sess.createCriteria(persistentClass);
if(StringUtils.isNotBlank(order)){
cri.addOrder(Order.desc(order));
}
cri.add(query);
return cri.list();
}

search

有人可能疑问为啥定义了4,还要再定义2呢,只能说编码风格的问题,我喜欢用4的方式,但有人喜欢HQL语句,觉得更加直观。

最新文章

  1. Kinect开发文章目录
  2. C# 生成字符串的 CheckSum
  3. blade and soul factions
  4. 多元线性回归 &mdash;&mdash;模型、估计、检验与预测
  5. (x&amp;y) + ((x^y)&gt;&gt;1)即x和y的算数平均值
  6. 转:vs发布window应用程序时出错:未能签名 ...\setup.exe
  7. Javascript零散知识点总结
  8. asp.net webform javascript postback JSON
  9. HDU 1074 Doing Homework(状态压缩DP)
  10. linux服务器load的含义
  11. JAVA提高二:枚举
  12. Dubbo的三种连接方式
  13. 【Spark调优】Kryo序列化
  14. python3+selenium入门09-键盘事件
  15. 第2章 GNS3和PacketTracer网络模拟器(3)_搭建Packet tracer实验环境
  16. leetcode 题解: Gray Code
  17. CF734F Anton and School (构造)
  18. [Mac入门]如何在Mac下显示Finder中的所有文件
  19. SQL(insert、delete、update)执行成功,但是数据库表中无显示无记录
  20. javascript中typeof用法

热门文章

  1. WPF自定义控件第一 - 进度条控件
  2. APP开放源码第一弹《纳豆》
  3. 刷LeetCode的正确姿势——第1、125题
  4. direction和unicode-bidi
  5. Android Studio快捷键switch case 轻松转换为if else
  6. JavaScript具有自动垃圾回收机制
  7. MVC5 网站开发之四 业务逻辑层的架构和基本功能
  8. SQL Server-简单查询示例(十一)
  9. [SQL] SQL 基础知识梳理(五) - 复杂查询
  10. 杂谈:用 Sublime Text 2 写 ActionScript3