"Three big SSH framework" Hibernate basics Second: Write Hibernateutil Tool class optimization performance

Source: Internet
Author: User

Write the Hibernateutil class to improve the execution speed of the program relative to the code in the previous article

First, you still have to write a JavaBean (User.java):

Package Cn.itcast.hibernate.domain;import Java.util.date;public class User {private int id;private String name;private Date birthday;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. Add a mapping file under the current package: 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 ">

After that, we will configure HIBERNATE.CFG.CML, which is placed directly below the SRC folder:

<?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 ">


Next, we're going to write the Hibernateutil.java class, which is basically to get the session.

Because. Every time we get a session in the code, it's time-consuming. We want it to be done only once, so we're going to do a tool class to initialize hibernate. And because the tool class generally does not want to be inherited, so that the user can change the tool class things. So generally we will use Finalkeyword.

Package Cn.itcast.hibernate;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.transaction;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 ();}}

And then. We write a simple test class that joins a data add.java:

Package Cn.itcast.hibernate;import Java.util.date;import Org.hibernate.session;import org.hibernate.Transaction; Import Cn.itcast.hibernate.domain.user;public class Add {public static void main (string[] args) {Session s = hibernateutil . GetSession (); Here, the GetSession () method in the Hibernateutil tool class is called directly to get sessiontransaction tx = S.begintransaction (); Open Transaction User user = new user (); User.setname ("Zhangsan"); User.setbirthday (new Date ()); S.save (user); Save Object Tx.commit ();//COMMIT Transaction s.close ();//Close Session}}
Thus, a simple hibernateutil class that gets the session to improve the performance of the program is written.



"Three big SSH framework" Hibernate basics Second: Write Hibernateutil Tool class optimization performance

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.