1. Create a common java project.
Hibernate is a lightweight framework. Unlike servlet, it also requires tomcat support. Only jdk support is required for Hibernate.
2. Introduce the jar package.
You can directly introduce the jar package in the project, and go to project ---> properties --->, and then, for example:
Another method is to import the data to the database, which is equivalent to a folder, where all the jar packages are placed in the new folder. Choose form --> Option --> and then, for example:
3. Provide the Hibernate configuration file. Hibernate. cfg. xml file. Complete the configuration.
<Hibernate-configuration>
<Session-factory>
<Property name = "hibernate. connection. driver_class"> com. mysql. jdbc. Driver </property>
<Property name = "hibernate. connection. url"> jdbc: mysql: // localhost: 3306/hibernate_first </property>
<Property name = "hibernate. connection. username"> root </property>
<Property name = "hibernate. connection. password"> bjpowernode </property>
<Property name = "hibernate. dialect"> org. hibernate. dialect. MySQLDialect </property>
</Session-factory>
</Hibernate-configuration>
Connect to the mysql database and explain the above labels. Explain in sequence: the first is the mySql driver, the second is the url of the connection, and the hibernate_first is the database name after the url; the third and fourth are the user name and password respectively. The fifth is dialect. Hibernate encapsulates databases and translates them into different forms, such as pages in drp. If oracle databases are used, they are translated into SQL statements with layer-3 nesting. If the mySql database is used, it is translated into a limit statement.
4. Create an Object User class:
Package com. bjpowernode. hibernate;
Import java. util. Date;
Public class User {
Private String id;
Private String name;
Private String password;
Private Date createTime;
Private Date expireTime;
Public String getId (){
Return id;
}
Public void setId (String 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 getCreateTime (){
Return createTime;
}
Public void setCreateTime (Date createTime ){
This. createTime = createTime;
}
Public Date getExpireTime (){
Return expireTime;
}
Public void setExpireTime (Date expireTime ){
This. expireTime = expireTime;
}
}
5. create User. hbm. xml. This file is used to map fields of the object to the database table. That is, the fields of the object class need to be mapped to the database table.
<? Xml version = "1.0"?>
<! DOCTYPE hibernate-mapping PUBLIC
"-// Hibernate/Hibernate Mapping DTD 3.0 // EN"
Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>
<Hibernate-mapping>
<Class name = "com. bjpowernode. hibernate. User">
<Id name = "id">
<Generator class = "uuid"/>
</Id>
<Property name = "name"/>
<Property name = "password"/>
<Property name = "createTime"/>
<Property name = "expireTime"/>
</Class>
</Hibernate-mapping>
6. We have completed the ing, but how does hibernate know that we have completed the ing and how does it map? In this case, we need to tell hibernate our own ing file, that is, to configure our ing file in hibernate. cfg. xml.
<Mapping resource = "com/bjpowernode/hibernate/User. hbm. xml"/>
7. Generate a database table. We have also seen that we have not created a new data table. in step 3, we just created a new database. According to our common practice, we should create a new data table. Otherwise, where should the entity be stored. Don't worry about this. database tables are definitely needed. There is no doubt about this, but we don't need to build it ourselves. Now hibernate needs to help us implement it. How can we implement it, hibernate will be based on the configuration file hibernate. cfg. xml and our ing file User. hbm. xml will automatically generate the corresponding table for us, and the name of this table is also given to us: the default is User. How to generate a table?
// The hibernate. cfg. xml file is read by default.
Configuration cfg = new Configuration (). configure ();
SchemaExport export = new SchemaExport (cfg );
Export. create (true, true );
8. Let's start the operation. Let's add a user object and see how hibernate is added? What is the difference with our previous practices?
Public class Client {
Public static void main (String [] args ){
// Read the hibernate. cfg. xml file
Configuration cfg = new Configuration (). configure ();
// Create SessionFactory
SessionFactory factory = cfg. buildSessionFactory ();
// Obtain the session
Session session = null;
Try {
Session = factory. openSession ();
// Start the transaction
Session. beginTransaction ();
User user = new User ();
User. setName ("Zhang San ");
User. setPassword ("123 ");
User. setCreateTime (new Date ());
User. setExpireTime (new Date ());
// Save the User object
Session. save (user );
// Submit the transaction
Session. getTransaction (). commit ();
} Catch (Exception e ){
E. printStackTrace ();
// Roll back the transaction
Session. getTransaction (). rollback ();
} Finally {
If (session! = Null ){
If (session. isOpen ()){
// Close the session
Session. close ();
}
}
}
}
}
Step 8: we can see that we are not familiar with the SQL statements of the insert into table. How can we add them? Let me tell you the truth. I did add it. If you don't believe it, you can try it by yourself. This is also the advantage of hibernate and it completely encapsulates jdbc, this reduces the data operation time.