zoukankan      html  css  js  c++  java
  • 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();
            
        }
    }
  • 相关阅读:
    mysql索引创建&查看&删除
    linq中不能准确按拼音排序
    Vue的组件的注册,复用以及组件中template多行处理
    Vue的简单使用和部分常用指令
    SpringBootMVC+thymeleaf模板初探
    记一次遗留代码的重构改造:数十万行国家标准坐标文件分析方法的改造与提速
    springBoot 集成Mysql数据库
    C#和Java的对比
    架构学习提炼笔记(三):高性能架构设计技巧——读写分离
    架构学习提炼笔记(二):架构设计的流程是什么?
  • 原文地址:https://www.cnblogs.com/bendoudou/p/8528606.html
Copyright © 2011-2022 走看看