The first thing to do is download the hibernate file package.
Then create a new project:
1. Import the jar package (open the downloaded package and find the required package in the Lib file with the jar package in it)
Note: Beginners can copy the code temporarily.
2. Create a new class file with the following contents:
public class Person {
Properties of the class
Private Integer ID;
private String name;
private String password;
Private Date birthday;
No parameter constructor
Public person () {}
Initialize value
Public person (string name, string password, Date birthday) {
Super ();
THIS.name = name;
This.password = password;
This.birthday = Birthday;
}
To create a Set,get method for a property
Public Integer getId () {
return ID;
}
public void SetId (Integer id) {
This.id = ID;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public String GetPassword () {
return password;
}
public void SetPassword (String password) {
This.password = password;
}
Public Date Getbirthday () {
return birthday;
}
public void Setbirthday (Date birthday) {
This.birthday = Birthday;
}
}
3. Create a new persistent mapping file, the file name is generally named: Object name. Hbm.xml, the following:
<?xml version= "1.0" encoding= "UTF-8"?>
<!--persistence maps to database tables--
<! DOCTYPE hibernate-mapping Public
"-//hibernate/hibernate Mapping DTD 3.0//en"
"Http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
<!--packages refers to the package name in which the file is located--
<!--class Name= "category name" table= "Table name"-
<class name= "Person" table= "T_person" >
<id name= "id" >
<!--
Generator represents the primary key
Native: Represents the primary key self-increment
-
<generator class= "native" ></generator>
</id>
< property configuration for!--class:
Name: The property name of the class
Column: The name of the table
Length: The column name of the table
-
<property name= "name" column= "T_name" ></property>
<property name= "Password" length= "></property>"
<property name= "Birthday" ></property>
</class>
4. Create the configuration XML file in the SRC directory, the file name is generally hibernate.cfg.xml, The content is as follows (you can find the Hibernate.cfg.xml file copy in the etc file under the project file, and the configuration file finds the database configuration you used in the Hibernate.properties.template file):
<! DOCTYPE hibernate-configuration Public
"-//hibernate/hibernate Configuration DTD 3.0//en"
"Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" >
<session-factory name= "foo" >
<!--database dialect ---
<property name= "Hibernate.dialect" >org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!--Create a drive -
<property name= "Hibernate.connection.driver_class" >com.mysql.jdbc.Driver</property>
<!--linked database
connecting the machine: Jdbc:mysql:///myhibernate
-
<property name= "Hibernate.connection.url" >jdbc:mysql:///myhibernate</property>
<!--database name --
<property name= "Hibernate.connection.username" >root</property>
<!--database Password --
<property name= "Hibernate.connection.password" >root</property>
< Automatically create TABLE operations!--database
Create-drop: Create a data table when the program starts, delete the previously created table (design form) when the program exits
Create : Deletes the last created table before the program starts, and then creates a new table structure
Update : If you create a table without a table at the start of the program, check that there are no updates (recommended)
Validate: Checks the table structure at program startup and does not create tables
-
<property name= "Hibernate.hbm2ddl.auto" >update</property>
<!--executing statements in the console output database --
<property name= "Hibernate.show_sql" >true</property>
<!--database executes standard format statement output --
<property name= "Hibernate.format_sql" >false</property>
<!--introduce a configuration file that needs to be persisted--
<mapping resource= "Com/hibernate/person.hbm.xml"/>
</session-factory>
5. Test:
public class Testhibernate {
Get Hibernate.cfg.xml Configuration
Configuration Con=new configuration (). Configure ();
Link Factory
Sessionfactory sf=con.buildsessionfactory ();
@Test
//add
public void Add () {
//session equivalent to the operation of a database table
Session session=sf.opensession ();
//hibernate, adding and removing changes requires support for the transaction
Session.begintransaction ();
Person preson=new person ("Lisa", "123", New Date ());
//Execute SQL statement
Session.save (Preson);
//Commit transaction
Session.gettransaction (). commit ();
//close session
Session.close ();
}
@Test
Update
public void Update () {
Session session=sf.opensession ();
Session.begintransaction ();
Person p= (person) session.get (person.class,3);
P.setname ("Holle");
Session.update (P);
Session.gettransaction (). commit ();
Session.close ();
}
@Test
Delete
public void Delete () {
Session session=sf.opensession ();
Session.begintransaction ();
Person P=new person ();
P.setid (2);
Session.delete (P);
Session.gettransaction (). commit ();
Session.close ();
}
@Test
Find
public void Select () {
Session session=sf.opensession ();
Session.begintransaction ();
Person p= (person) session.get (person.class,3);
System.out.println (P.getname () + "" +p.getpassword () + "" +p.getbirthday ());
Session.gettransaction (). commit ();
Session.close ();
}
}