The next few chapters learn that the main function of hibernate,hibernate is to connect to the database, simplifying the operation of JDBC.
First, we create the project and then import the Hibernate jar package and SQL Server driver into it.
Next, we need to write an entity class: User
Package Cn.itcast.hibernate.domain;import Java.util.date;public class User {private int id;//private String name; privat e String name; Private Date birthday; public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public Date Getbirthday () {return birthday;} public void Setbirthday (Date birthday) {this.birthday = birthday;}}
Three properties are available: ID, name, birthday
We then configure the mapping file for the user entity class under User's package: User.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 ">
As you can see, we have configured a <class>,name property that is the thunder of this class, table is the database to be mapped<id><property><property> These three tags define the attribute column in the table, where the <generator> tag is set for the ID, which specifies the policy that the primary key generates (described in the back)
Next, we will write the configuration file, created in the SRC directory: 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 ">In this configuration of the database driver, database address and database, user name, password, SQL Server database dialect, as well as the table, whether to display SQL statements, the last <mapping> is to user.hbm.xml this mapping file introduced.
Then we write a test class to implement:
Package Cn.itcast.hibernate;import Java.util.date;import Org.hibernate.session;import org.hibernate.SessionFactory ; import Org.hibernate.transaction;import Org.hibernate.cfg.configuration;import cn.itcast.hibernate.domain.User; public class Base {public static void main (string[] args) {Configuration cfg = new Configuration (); Cfg.configure (); Sessionfactory SF = Cfg.buildsessionfactory (); Session s = sf.opensession (); Transaction tx = s.begintransaction (); User U = New user (), U.setbirthday (New Date ()), U.setname ("name1"); S.save (U); Tx.commit (); S.close (); System.out.println ("End");}}
In this code, first load the configuration file, then open the session, then use the session to open the transaction, and then save the user object in the session, commit the transaction, close the session.In hibernate, data is not inserted by default, and only open transactions can be inserted.
Once we run, we can see that a data is inserted into the database.
"Three big SSH framework" Hibernate Basics First: Writing the first hibernate program