Hibernate 学习-1

1.添加jar包

   需要的jar包有(1)hebernate3.jar

                      (2).orcal数据库驱动  ojdbc5.jar

                      (3)request下的五个jar包 :antlr-2.7.6.jar

                                                           acommons-collections-3.1.jar

                                                           dom4j-1.6.1.jar

                                                           jta-1.1.jar

                                                           slf4j-api-1.6.1.jar

2.编写Hibernate配置文件

<!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="hibernate.dialect">
org.hibernate.dialect.Oracle10gDialect
</property>
<property name="hibernate.connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="hibernate.connection.username">scott</property><!--用户名 -->
<property name="hibernate.connection.password">tiger</property><!-- 密码 -->
<property name="hibernate.connection.url">
jdbc:oracle:thin:@172.16.17.158:1521:orcl
</property><!--//数据库IP地址  -->
<property name="show_sql">true</property><!-- 生成sql语句 -->
<property name="format_sql">true</property><!-- 生成格式化sql语句 -->

<mapping resource="com/yh/hib/entity/Emp.hbm.xml" />
</session-factory>

</hibernate-configuration>

3.创建持久化类和映射文件

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.yh.hib.entity">
<class name="Emp" table="Emp">
<id name="empno" column="empno" type="java.lang.Integer">
<generator class="assigned"></generator>
</id>
<property name="ename" column="ename" type="java.lang.String"></property>
<property name="job" type="java.lang.String"></property>
</class>
</hibernate-mapping>

4.Hibernate 核心接口一共有5个 : Configuration 、SessionFactory 、 Session 、 Transaction 和 Query。通过这些接口,不仅可以对持久化对象进行存取,还能够进行事务控制。

    (1)、Configuration 接口

    该接口负责管理Hibernate 的配置信息。Hibernate 运行时需要一些底层实现的基本信息。。这些信息包括:数据库URL、数据库用户名等等。

    使用Hibernate 必须首先提供这些信息以完成初始化工作,为后续操作做好准备。这些信息在 Hibernate 配置文件 hibernate.cfg.xml 中。当调用:

    

Configuration config = new Configuration().configure() ; 

 

    时,Hibernate 会自动在目录下搜索 hibernate.cfg.xml 文件, 并将其读取到内存中作为后续操作的基础配置。

 (2)、SessionFactory 接口

    SessionFactory 负责创建 Session 实例。可以通过Configuration 实例构建

Configuration config = new Configuration().configure() ;
SessionFactory sessionFactory = config.buidSessionFactory() ; 

    SessionFactory 保存了对应当前数据库配置的所有映射关系。同时也负责维护当前的二级数据缓存和Statement Pool。

    注意:SessionFactory 构造完毕之后,即被赋予了特定的配置信息。之后config 的任何变更将不会影响到已经创建的SessionFactory实例。

  (3)、Session 接口

    该接口是 Hibernate持续化操作的基础,提供了众多持久化的方法,如 save、update、delete 等。Session 实例有 SessionFactory 构建: 

Configuration config = new Configuration().configure() ;
SessionFactory sessionFactory = config.buidSessionFactory() ;
Session session = sessionFactory.openSession() ; 

  (4)、Tarnsaction 接口

    该接口是 Hibernate 中进行事务操作的接口 , Transaction 接口是对实际事务实现的一个抽象,这些实现包括 JDBC 的事务、JTA 中的 UserTransaction 等等。

  (5)、Query 接口

    Query 接口用于执行 HQL 语句。Query和HQL是分不开的。

原文地址:https://www.cnblogs.com/jimorulang/p/5519689.html