泛型DAO
来源:互联网 发布:java软件编程课程 编辑:程序博客网 时间:2024/05/16 13:52
泛型DAO:把所有相同的操作写在一起
JDK1.5或者JDK5上加的泛型DAO
如下例子:
GenericDao.java
public interface GenericDao<T,PK extends Serializable> {
public T findById(PK id);
public List<T> findAll();
public T save(T entity);
public void update(T entity);
public void delete(T entity);
}
GenericDaoHibernate.java
import java.lang.reflect.ParameterizedType;
public class GenericDaoHibernate<T, PK extends Serializable> implements
GenericDao<T, PK> {
private Class<T> clazz;
public GenericDaoHibernate() {
clazz = (Class<T>) ((ParameterizedType) getClass()
.getGenericSuperclass()).getActualTypeArguments()[0]; //取得CLAZZ的类型
}
public void delete(T entity) {
Session session = HibernateSessionFactory.getSessionFactory()
.getCurrentSession();
session.delete(entity);
}
public List<T> findAll() {
Session session = HibernateSessionFactory.getSessionFactory()
.getCurrentSession();
Query query = session.createQuery("from " + clazz.getName());
return query.list();
}
public T findById(PK id) {
Session session = HibernateSessionFactory.getSessionFactory()
.getCurrentSession();
return (T) session.get(clazz, id);
}
public T save(T entity) {
Session session = HibernateSessionFactory.getSessionFactory()
.getCurrentSession();
session.save(entity);
return entity;
}
public void update(T entity) {
Session session = HibernateSessionFactory.getSessionFactory()
.getCurrentSession();
session.update(entity);
}
}
GuestBookDao .java
public interface GuestBookDao extends GenericDao<GuestBook, Integer> {
public List<GuestBook> findAllOrderByid();
}
GuestbookDaoHibernate .java
public class GuestbookDaoHibernate extends
GenericDaoHibernate<GuestBook, Integer> implements GuestBookDao {
public List<GuestBook> findAllOrderByid() {
Session session = HibernateSessionFactory.getSessionFactory()
.getCurrentSession();
Query query = session.createQuery("from Guestbook order by id desc");
return query.list();
}
}
- 泛型DAO
- DAO泛型
- 泛型dao
- 泛型DAO
- 泛型Dao
- 泛型dao
- 泛型DAO
- 泛型dao
- 泛型dao 详细剖析
- 泛型 Hibernate DAO 例
- JAVA泛型(DAO)
- Hibernate泛型DAO
- Hibernian泛型 DAO
- 泛型dao的设计
- 泛型DAO设计
- Hibernate泛型DAO设计
- JPA 泛型DAO 封装
- Hibernate泛型DAO设计
- 未来10年互联网的十大发展趋势
- 比较两颗二叉树是否相等
- 未来人类科技发展时间表
- December, 23
- 55
- 泛型DAO
- HTC Diamond2(2)
- 人工智能发展情况调研
- C++实现Simple Factory模式
- 很实用且必用的小脚本代码
- JavaScript闭包讲解
- Larbin-C++实现的爬虫
- my string.js
- C++中定义与声明的区别