Configuration steps for Hibernate environments:
Add the jar packages required for hibernate, adding these jars to project,
the establishment of Hibernate.cfg.xml. Hibernate's hibernate.cfg.xml configuration file defaults to the PROJECT/SRC directory,
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:
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:
Create the following packages and classes, and modify the Hibernate.cfg.xml
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:
The configuration file Hibernate.cfg.xml is found through the Configure () of the Config class.
After getting the configuration file, create the sessionfactory through Buildsessionfactory ().
Create a Session by Sessionfactory's Opensession ().
open a thing through the BeginTransaction () Session.
Call the Session's Save (), update (), delete () and other methods to perform the database operation.
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.
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.