Hibernate series (V): Use Hibernate to complete simple crud operations

Source: Internet
Author: User

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.java

Package 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.java

Package 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.java

Package 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

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.