Document> Duan Hongjie> Eight, Design mode: Dao
RW-RW-R - Robbin Wheel 8, Design Mode: DAO Using Data Access Object (DAO) allows us to separate the underlying data access logic to business logic. We build a DAO class that provides GRUD (creation, reading, update, delete) for each data source. Below is a DAO instance for a data source Grud (created). 1. Interface (Demodao.java) package com.company.demo.da
Import net.sf.hibernate.hibernateException;
Import com.company.demo.jdo.de;
Import com.company.demo.dataModule. *;
/ **
*
Title: Struts Hibernate Development Template P>
*
Description: General Development Template P>
*
Copyright: CopyRight (c) 2003 P>
*
Company: Advantage Technology P>
* @Author Duan Hongjie
* @version 1.0
* /
Public interface demodao {
/ ** Set data model ** /
Public void setDataModule; DataModule DataModule;
/ ** Establish an object ** /
Public Void CreateDemo (Demo Demo) THROWS HibernateException, Exception
}
2. Abstract factory (DemodaFactory.java) package com.company.demo.dao;
/ **
*
Title: Struts Hibernate Development Template P>
*
Description: General Development Template P>
*
Copyright: CopyRight (c) 2003 P>
*
Company: Advantage Technology P>
* @Author Duan Hongjie
* @version 1.0
* /
Public class demodaofactory {
Private demodaofactory ()
{}
Public final static demodao getdemodao () {
Return New DemodaoImpl ();
}
}
3. DEMODAOIMPL.JAVA) package com.company.demo.dao;
Import java.util.list;
Import com.company.demo.dataModule. *;
Import com.company.demo.jdo.de;
Import Net.sf.hibActory;
Import net.sf.hibernate.hibernateException;
Import net.sf.hibernate.Session;
Import net.sf.hibernate.transaction;
Import net.sf.hibernate.query;
/ **
*
Title: Struts Hibernate Development Template P>
*
Description: General Development Template P>
*
Copyright: CopyRight (c) 2003 P>
*
Company: Advantage Technology P>
* @Author Duan Hongjie
* @version 1.0
* /
Public Class DemodaoImpl Implements Demodao {
Private sessionFactory sessionFactory;
Private Datamodule DataModule;
/ **
* Setting data model
* @Param DataModule
* /
Public void setDataModule {DataModule DataModule {
THIS.DATAMODULE = DATAMODULE;
THIS.SessionFactory = DATAModule.getsessionFactory ();
}
/ **
* Establish Demo
* @Param Demo
* @Throws HibernateException
* @Throws java.lang.exception
* /
Public Void CreateDemo (Demo Demo) throws HibernateException, Exception {
Transaction Transaction = NULL;
Session session = sessionFactory.openSession ();
Try {
Transaction = session.begintransaction ();
Session.save (demo);
Transaction.commit ();
} catch (hibernateException he) {
IF (Transaction! = NULL) {
Transaction.rollback ();
}
Throw He;
} finally {
session.close ();
}
}
}