Hibernate基础-HelloWord

1. ORM ORM (Object /Relation Mapping ): 对象/关系映射(理解)
     1 ORM 主要解决对象 -关系的映射
     2 .ORM的思想:将关系数据库中表中的记录映射成为对象,以对象的形式展现,程序猿能够把对数据库的操作转化为对对象的操作。

2. Hibernate  HelloWord
     1 . 增加 jar 包:增加到当前项目的 classpath 下                            
          hibernate-release-4.2.4.F inallib equired*.jar(全部的包)                                           
          MySQL 的驱动mysql -connector -java -5.1.29 -bin.jar                                          

     2 . 配置 hibernate 的配置文件: hibernate.cfg.xml
    
. 利用 hibernate 插件生成 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 >
             
               </session -factory >
          </hibernate-configuration>

     . 编辑 hibernate.cfg.xml 文件

     I. 增加链接数据库的基本信息:

         <! -- 配置连接数据库的基本信息 -->
         <property name ="connection.driver_class" >com.mysql.jdbc.Driver </property >
         <property name ="connection.username" >root </property >
         <property name ="connection.password" >123456 </property >
         <property name ="connection.url" >jdbc :mysql:///hibernate4</property>

     II. 配置 Hibernate 使用的数据库方言:
         每一种数据库使用的基本的语法会有细微的差别,比如分页 MySQL 使用 limit。而 Oracle 使用 rownum。这就须要告诉 Hibernate 底层使用的是哪一种数据库

         <! -- 配置 Hibernate 的数据库方言 -->
         <property name ="dialect" >org.hibernate.dialect.MySQLInnoDBDialect </property >
  
         注意:方言相应的类来自于 hibernate-release- 4.2.4.Finalprojectetchibernate.properties 
    
    III. 编辑 Hibernate 的一般属性

         <! -- 创建数据表的策略(了解,最多使用的值是 update -->
         <property name ="hbm2ddl.auto" >update </property >

         > create : 每次执行都会删除上一次的表 ,又一次生成表 , 哪怕二次没有不论什么改变
         > create -drop :会依据 .hbm.xml 文件生成表, 可是SessionFactory 一关闭,  表就自己主动删除
         > update :最经常使用的属性值。也会依据 .hbm.xml 文件生成表 , 但若 .hbm.xml  文件和数据库中相应的数据表的表结构不同 ,
            Hiberante  将更新数据表结构,但不会删除已有的行和列
         > validate : 会和数据库中的表进行比較 ,  .hbm.xml 文件里的列在数据表中不存在。则抛出异常


         <! -- 是否打印 SQL -->
         <property name ="show_sql" >true</property>

         <! -- 是否格式化 SQL -->
         <property name ="format_sql" >true</property>

3 . 编写实体类( POJO)及 Hibernate 映射文件: xxx.hbm.xml

    I. 编写一个 POJO :必须包括一个 OID 字段和数据表主键相应。必须有一个无參数的构造器。为字段定义 getter setter;非 final 

    II.  hibernate 插件生成 xxx.hbm.xml 文件

    注意:须要对文件进行简单的改动:改动主键生成方式(使用 id  generator 子节点的 class 属性配置主键的生成方式, native 表示使用数据库本地的方式来
    生成主键, MySQL 会自己主动的选用 auto_increment。而 Oracle 则使用序列的方式

    <generator class ="assigned" /> 改动为 <generator class="native" />

    III.  hibernate 配置文件(hiberante.cfg.xml )中关联 hibernate 持久化类的映射文件
    <mapping resource ="com/atguigu/hibernate/entities/News.hbm.xml" />

4 . 通过 Hibernate API 完毕持久化操作

    1. 创建 SessionFactory : Session 的工厂类。

SessionFactory 是线程安全的。一般地。在一个 Java 应用中仅仅有一个 SessionFactory 实例

        Configuration configuration = new Configuration ().configure ();
        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings( configuration.getProperties())
                                                      .buildServiceRegistry ();
        SessionFactory sessionFactory = configuration.buildSessionFactory (serviceRegistry );

    2. 创建 Session : 表示 Hibernate 应用程序和数据库的一次会话
        Session session = sessionFactory.openSession ();

    3. 开启事务
        Transaction transaction = session.beginTransaction ();

    4. 运行持久化操作
         //save
         //session.save (news );

         //利用 OID 载入对象
        News news2 = (News ) session.get (News.class , 1);
        System.out.println (news2 );
        news2.setContent ("myBatis" );

    5. 提交事务
        transaction.commit ();

    6. 关闭 Session
        session.close ();

    7. 关闭 SessionFactory
        sessionFactory.close ();
        


    ★写測试类的时候一般採用注解更方便些:
        public class Testing {
            private SessionFactory sessionFactory ;
            private Session session ;
            private Transaction transaction = null ;
            
            @Before
            public void init (){
                Configuration configuration = new Configuration ().configure ();
                ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings( configuration.getProperties()).buildServiceRegistry();
                sessionFactory = configuration.buildSessionFactory (serviceRegistry );
                
                session = sessionFactory.openSession ();
                transaction = session.beginTransaction ();
             }
            
            @After
            public void destroy (){
                transaction.commit ();
                session.close ();
                sessionFactory.close ();
             }
            
            @Test
            public void test (){
                //測试部分
             }
         }      
        


原文地址:https://www.cnblogs.com/yangykaifa/p/6879331.html