Hibernate及其配置文件

  Hibernate是一个开放源代码的对象关系映射框架。对象关系映射,ORM(Object/Relationship Mapping):对象关系映射是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术。是通过使用描述对象和数据库之间映射的元数据,将java程序中的对象自动持久化到关系数据库中。

  hibernate一般作为模型层/数据访问层。hibernate通过配置文件(hibernate.cfg.xml)和映射文件(***.hbm.xml)把JAVA对象或PO(Persistent Object,持久化对象)映射到数据库中,对数据表进行CURD操作。

  hibernate的运行流程:

  1、应用程序先调用Configration类,该类读取Hibernate的配置文件及映射文件中的信息,并用这些信息生成一个SessionFactpry对象。

  2、然后从SessionFactory对象生成一个Session对象,并用Session对象生成Transaction对象;可通过Session对象的get(),load(),save(),update(),delete()和saveOrUpdate()等方法对PO进行加载,保存,更新,删除等操作;在查询的情况下,可通过Session对象生成一个Query对象,然后利用Query对象执行查询操作;如果没有异常,Transaction对象将 提交这些操作结果到数据库中。

  hibernate配置文件hibernate.cfg.xml

  在Eclipse上装一个jboss插件,右键会自动出来hibernate的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!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.connection.username">beita</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@127.0.0.1:1521/orcl</property>
        
        <!-- 控制台 打印sql语句 -->
        <property name="show_sql">true</property>
        <!-- 美化sql语句 -->
        <property name="format_sql">true</property>
        <!-- 数据库方言 -->
        <property name="dialect">org.hibernate.dialect.OracleDialect</property>
        <!--定义生成表的策略,在mysql中推荐使用update,在oracle中,第一次运行时建议使用create,然后再把该配置注释
            update,create,create-drop,none
        -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!--  
            class:是带有@Entity注解实体类的全路径名
        -->
                <!--  
            class:是带有@Entity注解实体类的全路径名
        -->
        <!-- <mapping class="com.hyhltech.entity.Student"/>
        
        <mapping class="com.hyhltech.transaction.entity.Account"/>
        <mapping class="com.hyhltech.transaction.entity.Computer"/>
        
        <!-- mapping标签:关联对象关系映射的文件 
            resource:hbm.xml的相对路径-->
        <mapping resource="com/hyhltech/entity/Clothes.hbm.xml"/>
        
    </session-factory>
</hibernate-configuration>

 

hibernate映射文件xxx.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="cn.itcast.elec.domain.ElecText" table="Elec_Text">
        <id name="textID" type="string" column="textID">
            <generator class="uuid"></generator>
        </id>
        <property name="textName" type="string" column="textName"></property>
        <property name="textDate" type="date" column="textDate"></property>
        <property name="textRemark" type="string" column="textRemark"></property>
    </class>
</hibernate-mapping>

 下面是hibernate的测试:

public class TestHibernate {

    /**保存*/
    @Test
    public void save(){
        Configuration configuration = new Configuration();
        //默认加载类路径下的hibernate.cfg.xml,同时加载映射文件
        configuration.configure();
        SessionFactory sf = configuration.buildSessionFactory();
        Session s = sf.openSession();
        Transaction tr = s.beginTransaction();
        
        ElecText elecText = new ElecText();
        elecText.setTextName("测试Hibernate名称");
        elecText.setTextDate(new Date());
        elecText.setTextRemark("测试Hibernate备注");
        s.save(elecText);
        
        tr.commit();
        s.close();
        
    }
}

 

posted on 2018-03-08 15:13  笨兜兜  阅读(177)  评论(0)    收藏  举报

导航