Hibernate框架基础

Hibernate是一个开放源码的、非常优秀、成熟的O/R Mapping框架。它提供了强大、高性能的Java对象和关系数据的持久化和查询功能。

   Hibernate 只是一个将持久化类与数据库表相映射的工具,每个持久化类实例均对应于数据库表中的一条数据行。可以使用面向对象的方法操作此持久化类实例,完成对数据库表的插入、删除、修改等操作。

     利用Hibernate操作数据库,我们通过应用程序经过Hibernate持久层来访问数据库,其实Hibernate完成了以前JDBC的功能,不过Hibernate使用面向对象的方法操作数据库。


Hibernate入门示例:

1步: 先建一个Java工程导入使用Hibernate最小必要包。可以到网站下载Hibernate最新的包,如果访问数据库,则需要导入数据库驱动包。

2步:在src创建配置文件hibernate.cfg.xml,放置在src目录中。

3步:编写一个会话工厂类。通过会话工厂类产生一个会话Session对象。Session对象是Hibernate的核心。任何对数据库操作都在会话中进行的。

4步:编写POJO类以及映射文件(两者在同一文件夹下)。

5步:编写测试文件


hibernate.cfg.xml文件如下:

<span style="color:#6666cc;"><?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>
	<property name="connection.url">
		jdbc:mysql://127.0.0.1:3306/hib
	</property>
	<property name="connection.username">root</property>
	<property name="connection.password">1234</property>
	
	<property name="dialect">
		org.hibernate.dialect.MySQLDialect
	</property>

	<mapping resource="cn/hncu/domain/Student.hbm.xml"/>
	<mapping resource="cn/hncu/domain/Dept.hbm.xml"/>
</session-factory>
</hibernate-configuration></span>


映射文件示例如下:

Dept.hbm.xml

<span style="color:#6666cc;"><?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 package="cn.hncu.domain">
	<class name="Dept" table="depts" catalog="hib">
		<id name="deptId" type="java.lang.String">
			<column name="id" length="8"></column>
		</id>
		<property name="deptName" type="java.lang.String">
			<column name="name" length="30"></column>
		</property>
		
		<set name="students" table="students" inverse="true" cascade="all">
			<key >
				<column name="dept" length="8"></column>
			</key>
			<one-to-many class="Student" />
		 </set>
	</class>
</hibernate-mapping></span>
Student.hbm.xml

<span style="color:#6666cc;"><?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 package="cn.hncu.domain">
	<class name="Student" table="students" catalog="hib">
		<id name="studId" type="java.lang.String">
			<column name="id" length="8"></column>
		</id>
		
		<property name="studName" type="java.lang.String">
			<column name="name" length="30"></column>
		</property>
		
		<property name="age" type="java.lang.Integer">
			<column name="age" />
		</property>
		
		<many-to-one name="dept" class="Dept" fetch="select">
			<column name="dept" length="8"></column>
		</many-to-one>
		
	</class>
</hibernate-mapping></span>

HibernateSessionFactory类核心代码:

<span style="color:#3366ff;">import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateSessionFactory {
	private static final String configFile = "/hibernate.cfg.xml";
	private static Configuration config = new Configuration();
	private static SessionFactory sessionFactory = null;
	private static final ThreadLocal<Session> t = new ThreadLocal<Session>();

	static {
		try {
			config.configure(configFile);
			sessionFactory = config.buildSessionFactory();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
	}

	public static Session getSession() throws HibernateException {
		Session session = t.get();
		if (session == null || !session.isOpen()) {
			if (sessionFactory == null) {
				rebuildSessionFactory();
			}
			session = (sessionFactory!=null)?sessionFactory.openSession():null;
			t.set(session);
		}
		return session;
	}

	private static void rebuildSessionFactory() {
		try {
			config.configure(configFile);
			sessionFactory = config.buildSessionFactory();
		} catch (HibernateException e) {
			e.printStackTrace();
		}

	}
	
	public static void close(){
		Session session = t.get();
		t.set(null);
		if(session!=null && session.isOpen()){
			session.close();
		}
	}
}</span><span style="color:#006600;">
</span>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值