Hibernate Learning notes 02--Eclipse under Hibernate+mysql implementation.

Source: Internet
Author: User

Configuration steps for Hibernate environments:

    1. Add the jar packages required for hibernate, adding these jars to project,


    2. the establishment of Hibernate.cfg.xml. Hibernate's hibernate.cfg.xml configuration file defaults to the PROJECT/SRC directory,

    3. The contents of the Hibernate.cfg.xml are as follows (this is, of course, copied from Hibernate's Reference document and not modified):

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

The hibernate environment is configured to complete.

Hibernate integrates with MySQL:

    1. Add support for MySQL-driven jar packages, such as:

2. Modify Hibernate.cfg.xml

     Modify the connection mode for MySQL:

At this point, the hibernate + MySQL environment is built ( of course, a database named Hibernate is created under the MySQL database ).

Let's test the following:

    1. Create the following packages and classes, and modify the Hibernate.cfg.xml

    2. Paste the code:

2.1 Student.java

Package Com.hibernate.model;public class Student {private int id;private String name;private int age;public int getage () { return age;} public int getId () {return ID;} Public String GetName () {return name;} public void Setage (int.) {this.age = age;} public void setId (int id) {this.id = ID;} public void SetName (String name) {this.name = name;}}

2.2 Student.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 ">

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

2.4 Studenttest

Package Com.hibernate;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.cfg.configuration;import Com.hibernate.model.student;public class Studenttest {public static void main ( String[] args) {Student s = new Student (); S.setid (1); S.setname ("S1"); S.setage (+);//cfd.configure (), configure () Do not write parameters default lookup src directory hibernate.cfg.xmlConfiguration cfd = new Configuration ();//buildsessionfactory () Produce a sessionfactory factory sessionfactory SF = Cfd.configure (). Buildsessionfactory (); Session session = Sf.opensession (); Session.begintransaction (); Session.save (s); Session.gettransaction (). commit (); Session.close (); Sf.close ();}}

Run the main method in the Studenttest class, create a new table named student in the name Hibernate database and add data such as:

--------------------------------------------------------------------------------------------------------------- ------

Note:

Steps for hibernate to use the operations database:

  1. The configuration file Hibernate.cfg.xml is found through the Configure () of the Config class.

  2. After getting the configuration file, create the sessionfactory through Buildsessionfactory ().

  3. Create a Session by Sessionfactory's Opensession ().

  4. open a thing through the BeginTransaction () Session.

  5. Call the Session's Save (), update (), delete () and other methods to perform the database operation.

  6. get the things that are currently being manipulated through the Session's Gettransaction () and update the data to the database by taking the commit () of the thing.

  7. Finally, turn off Session, sessionfactory.

Configuration cfd = new configuration (); Sessionfactory SF = Cfd.configure (). Buildsessionfactory (); Session session = Sf.opensession (); Session.begintransaction ();//session.save (s); Call the session of adding and deleting methods session.gettransaction (). commit (); Session.close (); Sf.close ();

End


Hibernate Learning notes 02--Eclipse under Hibernate+mysql implementation.

Related Article

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.