Hibernate is an open source object-relational mapping framework that provides JDBC with a very lightweight object encapsulation that maps Pojo to database tables, is a fully automated ORM framework, and Hibernate automatically generates SQL statements, automatically executes, So that Java programmers can use object programming thinking to manipulate the database at will.
Orm:object realtion Mapping
One, import jar package
Second, create a database, build a table
Third, create the class, and the corresponding relationship mapping file for the table
Account.class
public class Account {/* must generate setter getter method, otherwise package exception */private int accountid;private String name;private int age;private String nickname;public int Getaccountid () {return accountId;} public void Setaccountid (int accountId) {this.accountid = accountId;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} Public String Getnickname () {return nickname;} public void Setnickname (String nickname) {this.nickname = nickname;}}
Account.hbm.xml
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org/dtd/ Hibernate-mapping-3.0.dtd ">
Four, configure Hibernate configuration file
Hibernate.cfg.xml
<?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 ">Five, code calls
Public String Register () {System.out.println ("Start registration-------------------------"); Account Account = new account (); Account.setaccountid (1); Account.setname ("Xiao Gang"); Account.setage (15); Account.setnickname ("xg"); Gets the management class object that loads the configuration file config = new configuration (); Config.configure (); Default load Src/hibenrate.cfg.xml File//Create session factory object Sessionfactory SF = Config.buildsessionfactory (); Create session (represents a session, a conversation with a database connection) session session = Sf.opensession (); Open transaction Transaction tx = Session.begintransaction (); Save-database Session.save (account); Commit Transaction Tx.commit (); Close Session.close (); Sf.close (); return SUCCESS; }
() Hibernate usage of Java Web