The hibernate operation database is used here to perform simple crud operations.
First, we need to first write a javabean:
Package Cn.itcast.domain;import Java.util.date;public class User {private int id;private String name;private Date Birthda y;public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public Date Getbirthday () {return birthday;} public void Setbirthday (Date birthday) {this.birthday = birthday;}}
And then, write it on the mapping file: User.hbm.xml
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">
Continue we need to write out the mapping file for the database: Hibernate.cfg.xml<?xml version= ' 1.0 ' encoding= ' utf-8 '?> <! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "http// Hibernate.sourceforge.net/hibernate-configuration-3.0.dtd ">
When all this is done, all we need is to write the DAO layer.First, we have to implement the DAO Layer Interface (interface): Userdao.java
Package Cn.itcast.dao;import Cn.itcast.domain.user;public interface Userdao {public void Saveuser (user user);p ublic User Finduserbyname (String name);p ublic user Finduserbyid (int id);p ublic void updateUser (user user);p ublic void Remove ( User user);
And we need to write a simple Hinbernateutil class to get Session:HibernateUtil.javaPackage Cn.itcast.dao;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.cfg.configuration;public Final class Hibernateutil {private static sessionfactory sessionfactory; Private Hibernateutil () {}static{configuration cfg = new Configuration (); Cfg.configure (); sessionfactory = Cfg.buildsessionfactory ();} public static Sessionfactory Getsessionfactory () {return sessionfactory;} public static Session GetSession () {return sessionfactory.opensession ();}}
Next, we will write the implementation of the DAO Layer: Userdaoimpl.javaPackage Cn.itcast.dao.impl;import Org.hibernate.criteria;import Org.hibernate.session;import Org.hibernate.transaction;import Org.hibernate.criterion.restrictions;import Cn.itcast.dao.HibernateUtil;import Cn.itcast.dao.userdao;import Cn.itcast.domain.user;public class Userdaoimpl implements Userdao {@Overridepublic void Saveuser (user user) {//Save an object session s = NULL; Transaction tx = Null;try{s = Hibernateutil.getsession (); tx = S.begintransaction (); s.save (user); Tx.commit ();} Finally{if (s!=null) {s.close ();}}} @Overridepublic User finduserbyname (String name) {//Find an object by name session s = Null;try{s = Hibernateutil.getsession (); Criteria C = S.createcriteria (User.class); C.add (Restrictions.eq ("name", name)); User user = (user) c.uniqueresult ();//returns the unique return user;} Finally{if (s!=null) {s.close ();}}} @Overridepublic User Finduserbyid (int id) {//Find an object by id session s = null;try{s = Hibernateutil.getsession (); User user = (user) S.get (user.class, id); return user;} Finally{if (s!=null) {s.close ();}}} @Overridepublic void UpdateUser (user user) {//Update an object session s = NULL; Transaction tx = Null;try{s = Hibernateutil.getsession (); tx = S.begintransaction (); s.update (user); Tx.commit ();} Finally{if (s!=null) {s.close ();}}} @Overridepublic void Remove (user user) {//Delete an object session s = NULL; Transaction tx = Null;try{s = Hibernateutil.getsession (); tx = S.begintransaction (); s.delete (user); Tx.commit ();} Finally{if (s!=null) {s.close ();}}}}
Finally, we write a test class class to verify success: Daotest.javaPackage Cn.test;import Java.util.date;import Cn.itcast.dao.userdao;import Cn.itcast.dao.impl.UserDaoHibernateImpl; Import Cn.itcast.domain.user;public class Daotest {/** * @param args */public static void main (string[] args) {Userdao dao = new Userdaohibernateimpl (); User user = new user (), User.setname ("name"), User.setbirthday (New Date ());d ao.saveuser (user), User.setname ("New name") ;d ao.updateuser (user); User u = dao.finduserbyname (User.getname ());d ao.remove (U);}}
Hibernate series (V): Use Hibernate to complete simple crud operations