The first step, we need to write a Pojo class: User.java
Package Cn.itcast.hibernate.domain;import Java.util.date;public class User {private int id;private 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;}}
In order for this javabean to have the ability to persist operations, we need to add a mapping file under the current 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 ">
The Org.hibernate.cfg.Configuration instance represents an application-to-SQL database mapping configuration, and the configuration object provides a Buildsessionfactory method that can produce an immutable sessionfactor Y object. Therefore, we need to create Configuration:hibernate.cfg.xml by mapping the file<?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 ">
Finally, we write a simple main class to implement simple data insertion Base.java: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 ();//Generate a Configuration object C Fg.configure (); Call the Configure () method to load the mapping file sessionfactory SF = Cfg.buildsessionfactory (); Produces a sessionfactorysession s = sf.opensession (); Open Sessiontransaction tx = S.begintransaction (); Open Transaction User user = new user (), User.setbirthday (New Date ()), User.setname ("name"), S.save (user); Save the user object in the session tx.commit (); Commit Transaction s.close (); Close SessionSystem.out.println ("End");}}
Simple data insertion can be achieved by running it.The jar package that needs to be used:
Hibernate series (i): simple data insertion