Hibernate for Java Web uses-crud DAO

Source: Internet
Author: User
Tags java web

1, make a simple util.

public class Hibernateutils {private static sessionfactory sf;static {//Load primary configuration file SF = new configuration (). Configure (). Buildsessionfactory ();} Open Sessionpublic static Session getsession () {return sf.opensession ();}}

2, make a accounts Qrud interface.

Public interface Accountdaointerface {//Increase void Save (account emp);//update void Update (account EMP);//Find Account FindByID ( Serializable ID); List<account> GetAll (); List<account> getAll (String accountname); list<account> getAll (int index, int count);//delete void Delete (Serializable id);}

  

3, Implement Interface

public class Accountdao implements Accountdaointerface {@Overridepublic account FindByID (Serializable ID) {Session session = NULL; Transaction tx = null;try {//Get sessionsession = Hibernateutils.getsession ();//Open tx = Session.begintransaction ();// Make a query return (account) Session.get (Account.class, id);} catch (Exception e) {throw new RuntimeException (e);} finally {tx.commit (); Session.close ();}} @Overridepublic list<account> GetAll () {session session = NULL; Transaction tx = Null;try {session = Hibernateutils.getsession (); tx = Session.begintransaction ();//HQL queries Query q = Session . CreateQuery ("from Account"); return Q.list ();} catch (Exception e) {throw new RuntimeException (e);} finally {tx.commit (); Session.close ();}} @SuppressWarnings ("unchecked") @Overridepublic list<account> getAll (String AccountName) {session session = NULL; Transaction tx = Null;try {session = Hibernateutils.getsession (); tx = Session.begintransaction (); Query Q =session.createquery ("From account where empname=?"); /indexStarting from 0 q.setparameter (0, AccountName);//Query return Q.list ();} catch (Exception e) {throw new RuntimeException (e);} finally {tx.commit (); Session.close ();}} @Overridepublic list<account> getAll (int index, int count) {session session = NULL; Transaction tx = Null;try {session = Hibernateutils.getsession (); tx = Session.begintransaction ();  Query q = Session.createquery ("from Account");//Set paging parameter Q.setfirstresult (index);  Query start line number position q.setmaxresults (count); The number of rows returned by the query list<account> list = Q.list (); return list;} catch (Exception e) {throw new RuntimeException (e);} finally {tx.commit (); Session.close ();}} @Overridepublic void Save (account EMP) {session session = NULL; Transaction tx = Null;try {session = Hibernateutils.getsession (); tx = Session.begintransaction ();//Save Session.save (EMP) ;} catch (Exception e) {throw new RuntimeException (e);} finally {tx.commit (); Session.close ();}} @Overridepublic void Update (account EMP) {session session = NULL; Transaction tx = Null;try {session = Hibernateutils. GetSession (); tx = Session.begintransaction ();//Update session.update (EMP);} catch (Exception e) {throw new RuntimeException (e);} finally {tx.commit (); Session.close ();}} @Overridepublic void Delete (Serializable ID) {session session = NULL; Transaction tx = Null;try {session = Hibernateutils.getsession (); tx = Session.begintransaction ();//Query the object first and then decide to delete obj = Session.get (account.class, id); if (obj! = null) {session.delete (obj);}} catch (Exception e) {throw new RuntimeException (e);} finally {tx.commit (); Session.close ();}}}

  

4, easy to use

list<account> accounts = new Accountdao (). GetAll (); for (int i = 0; i < accounts.size (); i++) {Account account = A Ccounts.get (i); System.out.println (Account.getname ());}

  

Hibernate for Java Web uses-crud DAO

Related Article

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.