Struts + hibernate Template Development notes-design mode: Dao

Source: Internet
Author: User

Using the data access object (DAO), we can split the underlying data access logic from the business logic. We constructed a DAO class that provides grud (create, read, update, and delete) operations for each data source.

The following is a DaO instance for data source grud (Creation) operations.

1. Interface (demodao. Java)

Package COM. company. demo. dao; import net. SF. hibernate. hibernateexception; import COM. company. demo. JDO. demo; import COM. company. demo. datamodule. *;/*** <p> title: STRUTS + hibernate development template </P> * <p> Description: general development template </P> * <p> copyright: copyright (c) 2003 </P> * <p> company: advantageous technologies </P> * @ author section Hong jie * @ version 1.0 */public interface demodao {/** Set Data Model **/Public void setdatamodule (datamodule ); /** create object **/Public void createdemo (Demo demo) throws hibernateexception, exception ;}

2. Abstract Factory (demodaofactory. Java)

Package COM. company. demo. dao;/*** <p> title: STRUTS + hibernate development template </P> * <p> Description: general development template </P> * <p> copyright: copyright (c) 2003 </P> * <p> company: advantageous technologies </P> * @ author Duan Hong jie * @ version 1.0 */public class demodaofactory {private demodaofactory () {} public final static demodao getdemodao () {return New demodaoimpl ();}}

3. DAO class (demodaoimpl. Java)

Package COM. company. demo. dao; import Java. util. list; import COM. company. demo. datamodule. *; import COM. company. demo. JDO. demo; import net. SF. hibernate. sessionfactory; import net. SF. hibernate. hibernateexception; import net. SF. hibernate. session; import net. SF. hibernate. transaction; import net. SF. hibernate. query;/*** <p> title: STRUTS + hibernate development template </P> * <p> Description: general development template </P> * <p> copyright: copyright (c) 20 03 </P> * <p> company: advantageous technologies </P> * @ author section Hong jie * @ version 1.0 */public class demodaoimpl implements demodao {private sessionfactory; private datamodule;/*** set the data model * @ Param datamodule */Public void setdatamodule (datamodule) {This. datamodule = datamodule; this. sessionfactory = datamodule. getsessionfactory ();}/*** create demo * @ Param demo * @ throws hibernateexception * @ throws Java. lang. Exception */Public void createdemo (Demo demo) throws hibernateexception, exception {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 ();}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.