Operating System: window8
Database Version: mysql-installer-community-5.6.17.0 (installation)
Jdbc: mysql-connector-java-5.1.30-bin
Hibernate: hibernate-release-4.2.3.Final
The specific operations are as follows:
1. Create a project hibernatedemo in eclipse.
2. unzip the hibernate-release-4.2.3.Final to copy the package in lib/required to the WEB-INF/lib folder and copy the jdbc driver to this folder, as shown in: you can also use the java build path in the project option properties of eclipse to import data)
3. The code for creating a database example in eclipse to create a table person is as follows:
Create table person (
Id int (11) not null auto_increment primary key,
Name varchar (100) not null default '',
Age int)
4. Create the persistence class person. java code as follows:
Package hibernate4.object;
Public class person {
Private Integer id;
Private String name;
Private Integer age;
Public person (){}
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 Integer getAge (){
Return age;
}
Public void setAge (Integer age ){
This. age = age;
}
Public String toString (){
Return "person: [id =" + id + "; name =" + name + "; age =" + age + "]";
}
}
5. Compile the Code of the person ing file person. hbm. xml as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE hibernate-mapping PUBLIC
"-// Hibernate/Hibernate Mapping DTD 3.0 // EN"
Http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd>
<Hibernate-mapping>
<Class name = "hibernate4.object. person" table = "person">
<Id name = "id" column = "id" type = "java. lang. Integer">
<Generator class = "native"/>
</Id>
<Property name = "name" column = "name" type = "java. lang. String"/>
<Property name = "age" column = "age" type = "java. lang. Integer"/>
</Class>
</Hibernate-mapping>
6. Compile the hibernate configuration file hibernate. cfg. xml Code as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE hibernate-configuration PUBLIC
"-// Hibernate/Hibernate Configuration DTD 3.0 // EN"
Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd>
<Hibernate-configuration>
<Session-factory>
<Property name = "show_ SQL"> true </property>
<Property name = "connection. url">
Jdbc: mysql: // 127.0.0.1: 3306/example
</Property>
<Property name = "dialect">
Org. hibernate. dialect. MySQLDialect
</Property>
<Property name = "connection. username"> root </property>
<Property name = "connection. password"> Scorpio19920204 </property>
<Property name = "connection. driver_class">
Com. mysql. jdbc. Driver
</Property>
<Mapping resource = "person. hbm. xml"/>
</Session-factory>
</Hibernate-configuration>
7. Compile the test file FirstHibernate. java code as follows:
Package hibernate4.object;
Import org. hibernate. Session;
Import org. hibernate. SessionFactory;
Import org. hibernate. cfg. Configuration;
Import org. hibernate. service. ServiceRegistry;
Import org. hibernate. service. ServiceRegistryBuilder;
Import hibernate4.object. person;
Public class FirstHibernate {
Public static void main (String args []) {
Person = new person ();
Person. setName ("neo ");
Person. setAge (20 );
Configuration cfg = new Configuration ();
Cfg. configure ();
// SessionFactory sf = cfg. configure (). buildSessionFactory (); // * The expiration Method * parses Hibernate. cfg. xml and returns a Configuration that already has Configuration options.
ServiceRegistry sr = new ServiceRegistryBuilder (). applySettings (cfg. getProperties (). buildServiceRegistry ();
// SessionFactory is similar to a Connection in the database.
SessionFactory sf = cfg. buildSessionFactory (sr );
// Open a connection
Session session = sf. openSession ();
// Start a transaction
Session. beginTransaction ();
Session. save (person );
// Obtain a transaction and submit it.
Session. getTransaction (). commit ();
// Close the connection
Session. close ();
// Close the factory
Sf. close ();
}
}
8. Run the test, start the mysql database, and run the FirstHibernate. java file. The running result is as follows:
The database is shown as follows:
Learning sentiment:
I encountered many problems in the course of learning. Because some codes on the reference books were too old, I encountered many problems during the operation. However, I was able to search for relevant materials online and solve them one by one, I also learned a lot of new knowledge.