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