hibernate4.3.10 Environment Construction

Source: Internet
Author: User

1, the first is to introduce the required package



2. Then configure the Hibernate.cfg.xml configuration file, connect the MySQL database information, and introduce the mapping file of the other submodule


3, write the sub-module mapping file, here is a simple login information table, Login.hbm.xml

public class Login {private int id;private string username;private string password, (getter/setter) public login () {}public Login (int ID, string username, string password) {super (); this.id = Id;this.username = Username;this.password = password;} }

5, writing the DAO layer, DAO is responsible for some operations of the underlying database, here need to implement a DAO interface, so that the business logic component relies on DAO interface instead of the concrete implementation class, the dependencies between the components of the system are raised to the interface level, avoid class-level direct coupling (if the system has changed, As long as the interface hierarchy does not change, the upper components that depend on the component do not need to be changed, thus providing good reuse.

Logindao Interface:

Public interface Logindao {public void Savelogin (login login);p ublic void Deletelogin (login login);p ublic void Updatelogi N (Login login);p ublic login findlogin (int id);p ublic login findlogin (String name);}
Logindaoimpl Implementation class:

public class Logindaoimpl implements Logindao {public void Deletelogin (login login) {hibernateutil.delete (login);} Public Login findlogin (int id) {return (Login) Hibernateutil.findbyid (Login.class, id);} Public Login Findlogin (String name) {return (Login) hibernateutil.findbyname (name);} public void Savelogin (login login) {hibernateutil.add (login);} public void Updatelogin (login login) {hibernateutil.update (login);}}

6, write the business logic component Service,dao has helped us implement the database operation, in the business logic component we only need to invoke the DAO component and focus on the implementation of the business logic

Loginservice Interface:

Public interface Loginservice {public void Save (login login);p ublic void Delete (login login);p ublic void Update (Login Logi N);p ublic login FindByID (int id);p ublic login findbyname (String name);}
Loginserviceimpl Implementation class:

public class Loginserviceimpl implements Loginservice {private Logindao logindao;public Logindao Getlogindao () {return lo Gindao;} public void Setlogindao (Logindao logindao) {This.logindao = Logindao;} public void Delete (login login) {logindao.deletelogin (login);} Public Login FindByID (int id) {return logindao.findlogin (ID);} Public Login findbyname (String name) {return logindao.findlogin (name);} public void Save (login login) {logindao.savelogin (login);} public void update (login login) {logindao.updatelogin (login);}}

7, write to get Hibernate Sessionfactory class tool class, here to write a simple tool class, the general application is in the spring container to manage the sessionfactory

public class Hibernateutil {private static sessionfactory sf;static {configuration cfg = new configuration (); Cfg.configur E ("Hibernateconfig/hibernate.cfg.xml"); SF = Cfg.buildsessionfactory ();} public static Session GetSession () {return sf.opensession ();} public static void Add (Object entity) {session session = NULL; Transaction tx = Null;try {session = Hibernateutil.getsession (); tx = Session.begintransaction (); Session.save (entity); Tx.commit ();} catch (Hibernateexception e) {e.printstacktrace (); throw e;} finally {if (session! = NULL) {Session.close ();}}} public static void Delete (Object entity) {session session = NULL; Transaction tx = Null;try {session = Hibernateutil.getsession (); tx = Session.begintransaction (); Session.delete (entity) ; Tx.commit ();} catch (Hibernateexception e) {e.printstacktrace (); throw e;} finally {if (session! = NULL) {Session.close ();}}} public static void update (Object entity) {session session = NULL; Transaction tx = Null;try {session = Hibernateutil.getsession (); tx = SESsion.begintransaction (); session.update (entity); Tx.commit ();} catch (Hibernateexception e) {e.printstacktrace (); throw e;} finally {if (session! = NULL) {Session.close ();}}} public static Object FindByID (Class clazz, Serializable ID) {Session session = Null;try {session = Hibernateutil.getsessio n (); Object ob = Session.get (clazz, id); return ob;} catch (Hibernateexception e) {e.printstacktrace (); throw e;} finally {if (session! = NULL) {Session.close ();}}} public static Object Findbyname (String name) {Session session = Null;try {session = Hibernateutil.getsession (); Query query = session.createquery ("from test where name =: Name"); Query.setparameter ("name", name); Object ob = Query.uniqu Eresult (); return OB;} catch (Hibernateexception e) {e.printstacktrace (); throw e;} finally {if (session! = NULL) {Session.close ();}}}

Note: When the hibernate.cfg.xml is not placed under SRC, set up here so that the application can find this configuration file

Configuration cfg = new configuration (); Cfg.configure ("Hibernateconfig/hibernate.cfg.xml");

8. Invoking the business logic component in action provides an implementation to save the user name password

Public String Execute () {login login=new login (); Login.setusername (GetUserName ()); Login.setpassword (GetPassword ()); Ls.save (login); return SUCCESS;}

9. Configure each Bean in the spring configuration file and rely on injection

<bean id= "Logindao" class= "Com.demo.dao.daoImpl.LoginDaoImpl"/><bean id= "Loginservice" class= " Com.demo.service.serviceImpl.LoginServiceImpl "><property name=" Logindao "ref=" Logindao "/></bean> <bean id= "registeraction" class= "Com.demo.action.RegisterAction" scope= "prototype" ><property name= "ls" ref = "Loginservice"/></bean>

10. Testing

A simple registration page to enter the user name password, click Register to save to the database


Save successfully in database




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

hibernate4.3.10 Environment Construction

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.