Hibernate is a lightweight ORM framework for JDBC that acts as a persistence layer for the project
Hibernate-Dependent libraries:
Create a project to add a jar package:
Hibernate.cfg.xml:
<! DOCTYPE hibernate-configuration Public
"-//hibernate/hibernate Configuration DTD 3.0//en"
"Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" >
<session-factory>
<!--database connection information--
<property name= "Connection.driver_class" >
Com.mysql.jdbc.Driver
</property>
<property name= "Connection.url" >jdbc:mysql://localhost/mysql</property>
<property name= "Connection.username" >root</property>
<property name= "Connection.password" >sxh</property>
<!--True indicates that SQL sent hibernate to the database is displayed--
<property name= "Show_sql" >true</property>
<!--Database dialect---
<property name= "dialect" >
Org.hibernate.dialect.MySQLDialect
</property>
<mapping resource= "Cn/haidashu/pojo/users.hbm.xml"/>
</session-factory>
Users.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" >
<class name= "Cn.haidashu.pojo.Users" table= "Users" >
<!--primary KEY policies--
<id name= "id" >
<generator class= "native"/>
</id>
<property name= "Name"/>
<property name= "pwd"/>
</class>
Users Category:
Package Cn.haidashu.pojo;
public class Users {
private int id;
private String name;
Private String pwd;
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 String getpwd () {
return pwd;
}
public void SetPwd (String pwd) {
This.pwd = pwd;
}
}
Test class:
Package Cn.haidashu.pojo;
Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import org.hibernate.Transaction;
Import org.hibernate.cfg.Configuration;
public class Test {
public static void Main (string[] args) {
Configuration cfg = new configuration ();
Sessionfactory SF = Cfg.configure (). Buildsessionfactory ();
Session session = Sf.opensession ();
Session.begintransaction ();
Save data
Users user = new users ();
User.setname ("Zoe");
User.setpwd ("335666");
Session.save (user);
System.out.println ("1");
Commit a transaction
Tx.commit ();
Session.close ();
Session.gettransaction (). commit ();
Session.close ();
Sf.close ();
}
}
Code Address: Http://pan.baidu.com/s/1nve1E8T
Hibernate framework use case