1. 程式人生 > >hibernate入門知識-01

hibernate入門知識-01

學習 參數 action pub getprop java jdb cmp sch

1.Hibernate是一個開放源代碼的對象關系映射框架,它對JDBC進行了非常輕量級的對象封裝

2.是一個全自動的orm框架,hibernate可以自動生成SQL語句,自動執行,使得Java程序員可以隨心所欲的使用對象編程思維來操縱數據庫。 Hibernate可以應用在任何使用JDBC的場合,既可以在Java的客戶端程序使用,也可以在Servlet/JSP的Web應用中使用,最具革命意義的是,Hibernate可以在應用EJB的J2EE架構中取代CMP,完成數據持久化的重任。

3.起源:2001年,澳大利亞墨爾本一位名為Gavin King的27歲的程序員,上街買了一本SQL編程的書,他厭倦了實體bean,認為自己可以開發出一個符合對象關系映射理論,並且真正好用的Java持久化層框架,因此他需要先學習一下SQL。這一年的11月,Hibernate的第一個版本發布了。

3.hibernate是完全的orm框架的一種(使用hql)...

4.創建maven工程,添加需要的依賴,在maven工程的pom.xml中添加兩個依賴

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.11.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>4.3.11.Final</version>
</dependency>

5.在src/main/java下創建:hibernate.cfg.xml主配置文件配置如下

  

<session-factory>

  <!--配置數據庫相關參數-->
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.url">jdbc:mysql:///hibernate?characterEncoding=UTF-8</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.connection.password">ou134568</property>

  <!-- 方言選擇,根據不同的數據庫 -->
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="show_sql">true</property><!-- 打印sql語句 -->

  <!-- 加入映射文件,javabean的映射配置文件 -->
  <!-- <mapping resource="po/User.hbm.xml"></mapping> -->
  <mapping resource="onetomany/Clazz.hbm.xml"></mapping>
  <mapping resource="onetomany/Students.hbm.xml"></mapping>
</session-factory>

6.在測試類中,首先讀取主配置文件/生成服務註冊器/生成sessionfactory

  我們可以寫成一個工具類,這樣我們只要調用openSession()就可以了...

  org.hibernate.Session本質上是對java.sql.Connection的封裝

  

  public class DBUtils {
     private static Configuration CGF;
     private static SessionFactory FACTORY;
      static{
      //讀取主配置hibernate.cfg.xml文件
      CGF = new Configuration().configure();
      //生成服務註冊器
      ServiceRegistry sr = new StandardServiceRegistryBuilder()
      .applySettings(CGF.getProperties()).build();
      //由服務註冊器來生成sessionfactory
      FACTORY = CGF.buildSessionFactory(sr);
      }


      public static Session openSession(){
      return FACTORY.openSession();
      }
      }

7.測試方法--根據主配置文件和映射文件生成對應的表,把所有的有映射配置文件的實體類生成對應的表,如果已經存在則不生成

    public void test01(){

    //讀取hibernate.cfg.xml配置文件
    Configuration cfg = new Configuration().configure();
    //根據主配置文件--映射文件生成對應的表
    SchemaExport export = new SchemaExport(cfg);
    //生成表
    export.create(true,true);

     }

8.測試方法,將一個對象持久化(存入數據庫)

    public void test02(){

    Session session = DBUtils.openSession();

    session.beginTransaction();
    Clazz clazz = new Clazz();
    clazz.setClazzName("一年級");

    Students student01=new Students();
    student01.setStudentName("王五");
    student01.setClazz(clazz);

    Students student02=new Students();
    student01.setStudentName("張三");
    student01.setClazz(clazz);
    

    //保存,要先保存clazz.否則會異常

    session.save(clazz);
    session.save(student01);
    session.save(student02);
    

    //提交事務並關閉session
    session.getTransaction().commit();
    session.close();

    }

hibernate入門知識-01