从Hibernate范型DAO设计制作的自用DAO
从Hibernate范型DAO设计打造的自用DAO
几天前拜读了rainlife大哥哥的文章:
www.iteye.com/topic/69293
于是乎自行改造了项目中的原有抽象结构:
范型DAO将所有DAO的CRUD方法使用范型抽象到此层面。
IGenericDAO.java 代码
- public interface IGenericDAO<t></t> {
- public List<t></t> find(String sql) throws NullSqlStringException,
- UnformatSelectStringException,AccessDataException;
- public T save(T t) throws NullPointerException,AccessDataException;
- public int deleteById(String id) throws AccessDataException;
- public T update(T t) throws NullPointerException,AccessDataException;
- public T getById(String id) throws AccessDataException;
- }
此DAO层接口主要处理针对某一PO的特殊操作的方法。
ISysUserDao.java 代码
- public interface ISysUserDao<sysuser></sysuser> extends IGenericDAO<sysuser></sysuser> {
- public void initializeRoles(SysUser user) throws NullPointerException;
- public void initializeOrganization(SysUser user) throws NullPointerException;
- public void initializeHeadShip(SysUser user) throws NullPointerException;
- public void initializePosition(SysUser user) throws NullPointerException;
- public void initializeTitle(SysUser user) throws NullPointerException;
- public void initializeWorkGroups(SysUser user) throws NullPointerException;
- }
其实,范型的真实类型已经在这一层上体现了,我们使用范型主要还是为了减少在此层接口中大量出现的相同的CRUD的方法的定义。
1 楼
xly_971223
2007-04-17
这是hibernate给提供的接口吗 请问是那个版本?
hibernate3.1之前的好像没支持范型
hibernate3.1之前的好像没支持范型
2 楼
soleegn
2007-04-17
http://www.hibernate.org/328.html里面提到的。
3 楼
bbiao
2007-06-21
你把代码抄错了,范型是不可以这么定义的....
这种模式我也用过,Hibernate上给的参考是
这种模式我也用过,Hibernate上给的参考是
public interface GenericDAO<T, ID extends Serializable> { T findById(ID id, boolean lock); List<T> findAll(); List<T> findByExample(T exampleInstance); T makePersistent(T entity); void makeTransient(T entity); }