依赖的jar包:
实例类 Student.java
package zh.hibernate.domain;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name = "student", catalog = "demo")
public class Student implements Serializable {
/**
*
*/
private static final long serialVersionUID = -2303585682086502906L;
@GenericGenerator(name = "generator", strategy = "increment")
@Id
@GeneratedValue(generator = "generator")
@Column(name = "id", unique = true, nullable = false)
private int id;
@Column(name = "no")
private String no;
@Column(name = "name")
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student() {
}
}
测试类
package zh.hibernate.test
import org.hibernate.Session
import org.hibernate.SessionFactory
import org.hibernate.Transaction
import org.hibernate.boot.MetadataSources
import org.hibernate.boot.registry.StandardServiceRegistry
import org.hibernate.boot.registry.StandardServiceRegistryBuilder
import zh.hibernate.domain.Student
public class Test {
public static void main(String[] args) {
// 1、新建Configuration对象
// Configuration cfg =new Configuration().configure()
// 2、通过Confinguration创建SessionFactory对象
// 在hibernate3.x是这种写法
// SessionFactory sf =cfg.buildSessionFactory()
// hibernate4.3
// ServiceRegistry registry = new StandardServiceRegistryBuilder()
// .applySettings(cfg.getProperties())
// .build()
// SessionFactory sf = cfg.buildSessionFactory(registry)
// 3、通过SessionFactory对象得到Session
StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build()
SessionFactory sessionFactory = new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory()
Session session = sessionFactory.openSession()
// 4、通过Session对象得到Transaction
Transaction tx = session.beginTransaction()
// 5、保存输出
Student user = new Student()
user.setName("小米")
user.setNo("060814")
session.save(user)
// 6、提交事务
tx.commit()
// 7、关闭session
session.close()
}
}
hibernate.cfg.xml 配置文件
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/demo</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<mapping class="zh.hibernate.domain.Student" />
</session-factory>
</hibernate-configuration>