Hibernate环境搭建

一、环境准备

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 - 必需架包
1antlr-2.7.7.jar
2dom4j-1.6.1.jar
3hibernate-commons-annotations-4.0.5.Final.jar
4hibernate-core-4.3.11.Final.jar
5hibernate-jpa-2.1-api-1.0.0.Final.jar
6jandex-1.1.0.Final.jar
7javassist-3.18.1-GA.jar
8jboss-logging-3.1.3.GA.jar
9jboss-logging-annotations-1.2.0.Beta1.jar
10jboss-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();
		}
	}

}

环境搭建成功!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值