Generic Data Access Objects
普通数据访问对象,这个是Hibernate官方网站上面的一个DAO类的设计模式,基于JDK5.0范型支持,文章地址如下:
http://www.hibernate.org/328.html
我下面的代码与Hibernate官网上提供的有点不同。
首先定义DAO类的接口IGenericDAO,该接口定义了共同的CRUD操作:<o:p></o:p>
java 代码
-
-
-
-
- public interface IGenericDAO 〈T, ID extends Serializable〉
- {
-
- public T findById(ID id);
-
-
- T findById(ID id, boolean lock);
-
-
- List<t></t> findAll();
-
-
- List<t></t> findByExample(T exampleInstance);
-
-
- T makePersistent(T entity);
-
-
- void makeTransient(T entity);
- }
下面是使用Hibernate针对该接口的实现GenericDAOHibernate:
java 代码
-
-
-
-
-
-
-
- public abstract class GenericDAOHibernate 〈T,ID extends Serializable, DAOImpl extends IGenericDAO〈T,ID〉〉
- implements IGenericDAO〈T,ID〉
- {
- private Class<t></t> persistentClass;
-
- protected Session session;
-
- public GenericDAOHibernate()
- {
- this.persistentClass = (Class<t></t>) ((ParameterizedType) getClass()
- .getGenericSuperclass()).getActualTypeArguments()[0];
- }
-
- @SuppressWarnings("unchecked")
- public DAOImpl setSession(Session s)
- {
- this.session = s;
- return (DAOImpl)this;
- }
-
- protected Session getSession()
- {
- if (session == null)
- throw new IllegalStateException(
- "Session has not been set on DAO before usage");
- return session;
- }
-
- public Class<t></t> getPersistentClass()
- {
- return persistentClass;
- }
-
-
- @SuppressWarnings("unchecked")
- public T findById(ID id)
- {
- return (T) getSession().load(getPersistentClass(), id);
- }
-
- @SuppressWarnings("unchecked")
- public T findById(ID id, boolean lock)
- {
- T entity;
- if (lock)
- entity = (T) getSession().load(getPersistentClass(), id, LockMode.UPGRADE);
- else
- entity = findById(id);
-
- return entity;
- }
-
- @SuppressWarnings("unchecked")
- public List<t></t> findAll()
- {
- return findByCriteria();
- }
-
- @SuppressWarnings("unchecked")
- public List<t></t> findByExample(T exampleInstance)
- {
- Criteria crit = getSession().createCriteria(getPersistentClass());
- Example example = Example.create(exampleInstance);
- crit.add(example);
- return crit.list();
- }
-
- @SuppressWarnings("unchecked")
- public List<t></t> findByExample(T exampleInstance, String[] excludeProperty)
- {
- Criteria crit = getSession().createCriteria(getPersistentClass());
- Example example = Example.create(exampleInstance);
- for (String exclude : excludeProperty)
- {
- example.excludeProperty(exclude);
- }
- crit.add(example);
- return crit.list();
- }
-
- @SuppressWarnings("unchecked")
- public T makePersistent(T entity)
- {
- getSession().saveOrUpdate(entity);
-
- return entity;
- }
-
- public void makeTransient(T entity)
- {
- getSession().delete(entity);
- }
-
- @SuppressWarnings("unchecked")
- protected List<t></t> findByCriteria(Criterion... criterion)
- {
- Criteria crit = getSession().createCriteria(getPersistentClass());
- for (Criterion c : criterion)
- {
- crit.add(c);
- }
- return crit.list();
- }
-
- @SuppressWarnings("unchecked")
-
-
-
- protected List<t></t> findByCriteria(Order order,Criterion... criterion)
- {
- Criteria crit = getSession().createCriteria(getPersistentClass());
- for (Criterion c : criterion)
- {
- crit.add(c);
- }
- if(order!=null)
- crit.addOrder(order);
- return crit.list();
- }
-
- @SuppressWarnings("unchecked")
- protected List<t></t> findByCriteria(int firstResult,int rowCount,Order order,Criterion... criterion)
- {
- Criteria crit = getSession().createCriteria(getPersistentClass());
- for (Criterion c : criterion)
- {
- crit.add(c);
- }
- if(order!=null)
- crit.addOrder(order);
- crit.setFirstResult(firstResult);
- crit.setMaxResults(rowCount);
- return crit.list();
- }
- }
这样,我们自己所要使用的DAO类,就可以直接从这个Hibernate的DAO类继承:
比如说我们定义一个IUserDAO接口,该接口继承IGenericDAO:
java 代码
- public interface IUserDAO extends IGenericDAO〈User,Integer〉
- {
- public User find(String username,String password);
- public User find(String username);
- }
该接口从IGenericDAO继承,自然也就定义了IGenericDAO接口所定义的通用CRUD操作。<o:p>
</o:p>
再来看一下针对IUserDAO 的Hibernate实现UserDAOHibernate:
java 代码
- public class UserDAOHibernate extends GenericDAOHibernate〈User,Integer,IUserDAO〉 implements IUserDAO {
-
- public User find(String username, String password) {
-
- }
-
- public User find(String username) {
-
- }
- }
UserDAOHibernate继承GenericDAOHibernate并实现IUserDAO接口,这样,我们的UserDAOHibernate既拥有通用的CRUD操作,也实现了针对用户的特定的业务操作。
说明 :由于范型的符号经过在线编辑器发布后,被过滤掉了,所以上面修改后的几个符号,都是我在中文状态下输入的。
分享到:
相关推荐
在VC++中使用ADO(ActiveX Data Objects)与Access数据库进行交互是常见的数据访问技术,尤其是在开发Windows桌面应用程序时。本文将详细介绍如何配置VC++项目,使其能够连接和操作Access 2003到Access 2007的数据库。...
本文介绍了一种利用ADOX(ActiveX Data Objects Extensions)技术来动态创建Access数据库的方法。ADOX是一种扩展了ADO(ActiveX Data Objects)的技术,主要用于管理数据库对象,如表、索引等。 #### 三、动态创建...
REFERENCETYPE REFTO typename FOR references to arbitrary data objects ADATA: dref TYPE |LIKE <data_object>. GET REFERENCE OF DataObject INTO dref. IF Reference is typed? X = dref->*. Y = dref->comp...
- **Indexing System**: Access elements of an object using indexing (e.g., `data[1,2]` to access the element in the first row and second column of a matrix). - **Accessing Values with Names**: Use ...
//Microsoft ActiveX Data Objects 2.8 Library using System; using System.Collections.Generic; using System.Linq; using System.Text; using ADOX; using System.IO; namespace WebRequestT
Data Instead Of Unicode Vs. 8-bit Overview Of Syntax Changes New Syntax Changed Syntax Removed Syntax Changes Already Present In Python 2.6 Library Changes PEP 3101: A New Approach To String ...
1,01.zip Calling Stored Procedures 调用存储过程(8KB)<END><br>2,02.zip Create Access data source name dynamically 动态创建Access的数据源名(5KB)<END><br>3,03.zip Using DAO to read data ...
4.1PostgreSQL 6.5 - 7.4Firebird 1.0 - 1.5Interbase 5.0 - 7.5Microsoft SQL Server 7, 2000Sybase ASE 12.0, 12.5Oracle 9iSQLite 2.8For other databases we propose to use implemented Active Data Objects ...
MFC提供了CDAO(Microsoft Data Access Objects)类库,包括CdaoDatabase、CdaoTableDef、CdaoRecordset和CdaoQueryDef等类,用于数据库的连接、表的操作、记录集的获取和查询定义。然而,MFC的标准向导只适用于基于...
You'll work with examples so you understand how to encapsulate and hide data by working with properties and access control. Then, you'll get to grips with complex scenarios where you use instances ...
Python 3.7.3 is the third maintenance ...The insertion-order preservation nature of dict objects is now an official part of the Python language spec. Notable performance improvements in many areas.
在DataSnap中,通常会使用ADO(ActiveX Data Objects)来连接和操作数据库。ADODataSnap1可能指的是包含数据库连接和查询的组件或代码文件,它封装了与数据库的交互,使得业务逻辑层可以专注于业务规则,而不是底层...
Next, you’ll learn to work with operators, objects and data-sources in increasingly realistic situations. Finally, you’ll start putting the pieces together to create sophisticated programs of your ...
II Tables and Objects 95 11 Data Structures 97 11.1 Arrays 97 Property of Ian Bloss ix 11.2 Matrices and Multi-Dimensional Arrays 98 11.3 Linked Lists 100 11.4 Queues and Double Queues 100 ...
Implemented a generic batch command mode for the AcpiExec utility (execute any AML debugger command) (Valery Podrezov). ---------------------------------------- 12 September 2006. Summary of changes ...
python3.7.4官方文档。Python 3.7.4 is the ...The insertion-order preservation nature of dict objects is now an official part of the Python language spec;Notable performance improvements in many areas.
Part IV: Data Access 812 Chapter 24: File System Data 814 Streams 815 The Classes for Input and Output 815 Serialized Objects 842 Monitoring the File Structure 847 Summary 855 ...
You'll work with examples so you understand how to encapsulate and hide data by working with properties and access control. Then, you'll get to grips with complex scenarios where you use instances ...