Hibernate Learning 2_ Simple Hibernate implementation

Source: Internet
Author: User

In this paper, Java Engineering +hibernate Simple implementation of a Hibernate persisted Java object to MySQL database function.

1. Project configuration

2. Database connection configuration file Hibernate.cfg.xml

<?XML version= "1.0"?><!DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org/ Dtd/hibernate-mapping-3.0.dtd "><hibernate-mapping Package= "Com.djl.test">    <classname= "Student"Table= "Student">      <IDname= "id"column= "ID">      </ID>      < Propertyname= "Name"column= "Name"type= "string"/>      < Propertyname= "Age"column= "Age"type= "integer"/>    </class></hibernate-mapping>
View Code

3. New Java Entity Student.java

 Packagecom.djl.test; Public classStudent {Private intID;  Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }    PrivateString name; Private intAge ;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }    }
View Code

4. Establish the mapping relationship between entity and database Student.hbm.xml

<?XML version= "1.0"?><!DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org /dtd/hibernate-mapping-3.0.dtd "><hibernate-mapping Package= "Com.djl.test">    <classname= "Student"Table= "Student">      <IDname= "id"column= "ID">      </ID>      < Propertyname= "Name"column= "Name"type= "string"/>      < Propertyname= "Age"column= "Age"type= "integer"/>    </class></hibernate-mapping>
View Code

5, simple test program to call

 Packagecom.djl.test;Importorg.hibernate.Session;Importorg.hibernate.SessionFactory;Importorg.hibernate.cfg.Configuration; Public classMaintest { Public Static voidMain (String ... args) {Student stu=NewStudent (); Stu.setid (0); Stu.setage (20); Stu.setname ("Wangshan"); Configuration Config=NewConfiguration ();        Config.configure (); Sessionfactory Factory=config.buildsessionfactory (); Session Session=factory.opensession ();         Session.begintransaction ();        Session.save (Stu);        Session.gettransaction (). commit ();        Session.close ();            Factory.close (); }}
View Code

Run results

Hibernate Learning 2_ Simple Hibernate implementation

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.