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