一、环境准备
1、hibernate-release-4.3.11.Final
2、jdk 1.7
3、mysql
hibernate4 官方架包下载可参考我的另一篇文章:三大框架官方架包下载方法
二、开始搭建:
1、创建java项目:
2、导入hibernate必需架包:位于
\hibernate-release-4.3.11.Final\lib\required\目录下.。
序号 | hibernate 4 - 必需架包 |
---|---|
1 | antlr-2.7.7.jar |
2 | dom4j-1.6.1.jar |
3 | hibernate-commons-annotations-4.0.5.Final.jar |
4 | hibernate-core-4.3.11.Final.jar |
5 | hibernate-jpa-2.1-api-1.0.0.Final.jar |
6 | jandex-1.1.0.Final.jar |
7 | javassist-3.18.1-GA.jar |
8 | jboss-logging-3.1.3.GA.jar |
9 | jboss-logging-annotations-1.2.0.Beta1.jar |
10 | jboss-transaction-api_1.2_spec-1.0.0.Final.jar |
3、导入mysql的jdbc架包:
mysql-connector-java-5.1.26-bin.jar
三、相关文件内容:
1、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">
<hibernate-configuration>
<session-factory>
<!-- 数据库连接设置 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- Student为你的数据库名称 -->
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">
org.hibernate.cache.NoCacheProvider
</property>
<!-- 显示SQL语句 -->
<property name="show_sql">true</property>
<!-- 格式化输出SQL语句 -->
<property name="format_sql">true</property>
<!-- validate 加载hibernate时,验证创建数据库表结构 -->
<!-- create 每次加载hibernate,重新创建数据库表结构,这就是导致数据库表数据丢失的原因。 -->
<!-- create-drop 加载hibernate时创建,退出是删除表结构 -->
<!-- update 加载hibernate自动更新数据库结构 -->
<property name="hbm2ddl.auto">create</property>
<!-- hibernate与数据库的对象关系映射文件**.hbm.xml -->
<mapping resource="com/test/pojo/Student.hbm.xml" />
<!-- 使用annotion注解时,hibernate与数据库对象关系映射表示 -->
<!-- 推荐使用annotion注解来表示映射关系 -->
<!-- <mapping class="com.test.pojo.Student" /> -->
</session-factory>
</hibernate-configuration>
2、Student.java
package com.test.pojo;
public class Student {
private int id;
private String name;
public Student() {
}
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;
}
}
3、Student.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.test.pojo.Student" table="Student">
<id name="id" column="id">
<generator class="assigned"></generator>
</id>
<property name="name" column="name"></property>
</class>
</hibernate-mapping>
4、TestHibernate.java
package com.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import com.test.pojo.Student;
public class TestHibernate {
public static void main(String[] args) {
Configuration cfg = new Configuration().configure();
// hibernate3.0版本创建sessionFactory的方法
// SessionFactory factory = cfg.buildSessionFactory();
// hibernate4.0版本创建sessionFactory方法
StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder()
.applySettings(cfg.getProperties());
ServiceRegistry sr = ssrb.build();
SessionFactory factory = cfg.buildSessionFactory(sr);
Session session = factory.openSession();
Transaction tran = session.beginTransaction();
Student stu = new Student();
stu.setId(3);
stu.setName("王五");
try {
session.save(stu);
tran.commit();
System.out.println("保存成功!");
} catch (Exception e) {
tran.rollback();
System.out.println("保存失败!");
} finally {
session.close();
factory.close();
}
}
}
环境搭建成功!