Hibernate--入門

一 概述
1 框架:寫程序,使用框架之后,幫我們實(shí)現(xiàn)一部分功能,使用框架好處,少寫一部分代碼實(shí)現(xiàn)功能。
2 Hibernate
(1)hibernate框架應(yīng)用在javaEE三層結(jié)構(gòu)中的dao層
(2)在dao層里面作對數(shù)據(jù)庫curd操作,使用hibernate實(shí)現(xiàn)crud操作,hibernate底層代碼就是jdbc,hibernate對jdbc進(jìn)行封裝,使用hibernate好處,不需要寫負(fù)責(zé)jdbc代碼了,不需要寫sql語句實(shí)現(xiàn)
(3)hibernate開源的輕量級的框架
(4)hibernate版本

  • hibernate3.x
  • hibernate4.x
  • hibernate5.x(學(xué)習(xí))

3 ORM思想
(1)hibernate使用orm思想對數(shù)據(jù)庫進(jìn)行crud操作
(2)在web階段學(xué)習(xí)javabean,更正確的叫法 “實(shí)體類”
(3)ORM:Object relational mapping ,對象關(guān)系映射

  • 描述:讓我們實(shí)體類和數(shù)據(jù)表進(jìn)行 一 一 對應(yīng)關(guān)系
    讓實(shí)體類首先和數(shù)據(jù)庫表對應(yīng)
    讓實(shí)體類屬性和表里面字段對應(yīng)
  • 不需要直接操作數(shù)據(jù)庫表,而操作表對應(yīng)實(shí)體類對象

二 環(huán)境搭建
1 導(dǎo)入hibernate的jar包

jar包

maven配置

<!-- https://mvnrepository.com/artifact/antlr/antlr -->
    <dependency>
      <groupId>antlr</groupId>
      <artifactId>antlr</artifactId>
      <version>2.7.7</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
    <dependency>
      <groupId>dom4j</groupId>
      <artifactId>dom4j</artifactId>
      <version>1.6.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.geronimo.specs/geronimo-jta_1.1_spec -->
    <dependency>
      <groupId>org.apache.geronimo.specs</groupId>
      <artifactId>geronimo-jta_1.1_spec</artifactId>
      <version>1.1.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.hibernate.common/hibernate-commons-annotations -->
    <dependency>
      <groupId>org.hibernate.common</groupId>
      <artifactId>hibernate-commons-annotations</artifactId>
      <version>5.0.1.Final</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>5.0.7.Final</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>5.0.7.Final</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.1-api -->
    <dependency>
      <groupId>org.hibernate.javax.persistence</groupId>
      <artifactId>hibernate-jpa-2.1-api</artifactId>
      <version>1.0.0.Final</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.jboss/jandex -->
    <dependency>
      <groupId>org.jboss</groupId>
      <artifactId>jandex</artifactId>
      <version>2.0.0.Final</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.jboss.logging/jboss-logging -->
    <dependency>
      <groupId>org.jboss.logging</groupId>
      <artifactId>jboss-logging</artifactId>
      <version>3.3.0.Final</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/log4j/log4j -->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.16</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.6.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.2</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.javassist/javassist -->
    <dependency>
      <groupId>org.javassist</groupId>
      <artifactId>javassist</artifactId>
      <version>3.18.1-GA</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.38</version>
    </dependency>

2 創(chuàng)建實(shí)體類(hibernate要求實(shí)體類有一個(gè)屬性唯一的)
User.java

package entity;

/**
 * Created by pc on 2017/9/22.
 */
public class User {
    private int id;
    private String name;
    private String password;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
  • 使用hibernate時(shí)候,不需要自己手動(dòng)創(chuàng)建表,hibernate可以幫助創(chuàng)建表

3 配置實(shí)體類和數(shù)據(jù)庫表--對應(yīng)關(guān)系(映射關(guān)系)
(1)使用配置文件實(shí)現(xiàn)映射關(guān)系

  • 創(chuàng)建xml格式的配置文件
    • 映射配置文件名稱和位置沒有固定要求
    • 建議:在實(shí)體類所在包里面創(chuàng)建,實(shí)體類名稱.hbm.xml
  • 配置文件是xml格式,在配置文件中首先引入DTD約束
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
  • 映射配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
    <!--
    1 配置類和表對應(yīng)
    class標(biāo)簽
    name屬性
    table屬性:數(shù)據(jù)庫表名稱
    -->
    <class name="entity.User" table="t_user">
        <!--2配置實(shí)體類id和表id對應(yīng)
        hibernate要求實(shí)體類共有一個(gè)屬性唯一值
        hibernate要求表有字段作為唯一值
        -->
        <!--id標(biāo)簽
        name屬性:實(shí)體類里面id屬性名稱
        column屬性:生成的表字段名稱
        -->
        <id name="id" column="id">
            <!--設(shè)置數(shù)據(jù)庫表id增長策略
            native:生成表id值就是主鍵自動(dòng)增長
            -->
            <generator class="native"></generator>
        </id>
        <!--配置其他屬性和表字段對應(yīng)
        name:實(shí)體類屬性名稱
        column:生產(chǎn)表字段名稱
        -->
        <property name="name" column="name"></property>
        <property name="password" column="password"></property>
    </class>
    </hibernate-mapping>

4 創(chuàng)建hibernate的核心配置文件
(1)核心配置文件格式xml,但是核心配置文件名稱和位置固定的

  • 位置:必須在src下
  • 名稱:必須hibernate.cfg.xml

(2)引入DTD約束

<!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>
   </session-factory>
</hibernate-configuration>

(3)hibernate操作過程中,只會加載核心配置文件,其他配置文件不會加載

  • 配置數(shù)據(jù)庫信息
        <!--1. 配置數(shù)據(jù)庫信息-->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
  • 配置hibernate信息
     <!--2. 配置hibernate信息    可選-->
        <!--輸出底層sql語句-->
        <property name="hibernate.show_sql">true</property>
        <!--輸出底層sql語句格式-->
        <property name="hibernate.format_sql">true</property>
        <!--hibernate幫創(chuàng)建表,需要配置之后
        update:如果已經(jīng)有表,更新,如果沒有,創(chuàng)建-->
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!--配置數(shù)據(jù)庫方言
        在mysql里面實(shí)現(xiàn)分頁  關(guān)鍵字limit,只能使用mysql里面
        在oracle數(shù)據(jù)庫,實(shí)現(xiàn)分頁rownum
        讓hibernate框架識別不同數(shù)據(jù)庫自己持有的語句-->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  • 把映射文件放到核心配置文件
        <!--3. 把映射文件放到核心配置文件-->
        <mapping resource="User.hbm.xml"></mapping>

三 測試
1 加載hibernate核心配置文件
2 創(chuàng)建SessionFactory對象
3 使用SessionFactory創(chuàng)建session對象
4 開啟事務(wù)
5 寫具體邏輯crud操作
6 提交事務(wù)
7 關(guān)閉資源

import entity.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

/**
 * Created by pc on 2017/9/22.
 */
public class HibernateDemo {
    @Test
    public void testAdd(){
        /*1 加載hibernate核心配置文件
        * 在src下面找到hibernate.cfg.xml
        * 到hibernate里面封裝對象*/
        Configuration cfg = new Configuration();
        cfg.configure();
        /*2 創(chuàng)建SessionFactory對象
        讀取hibernate核心配置內(nèi)容,創(chuàng)建sessionFactory
        在過程中,根據(jù)映射關(guān)系,在配置數(shù)據(jù)庫里面把表創(chuàng)建*/
        SessionFactory sessionFactory = cfg.buildSessionFactory();
        /*3 使用SessionFactory創(chuàng)建session對象
        * 類似與鏈接
        * */
        Session session = sessionFactory.openSession();
        /*4 開啟事務(wù)*/
        Transaction tx = session.beginTransaction();
        /*5 寫具體邏輯crud操作
        * 添加功能
        * */
        User user = new User();
        user.setName("小明");
        user.setPassword("555555");
        //調(diào)用session的方法實(shí)現(xiàn)添加
        session.save(user);
        /*6 提交事務(wù)*/
        tx.commit();
        /*7 關(guān)閉資源*/
        session.close();
        sessionFactory.close();
    }
}

測試結(jié)果

結(jié)果顯示
數(shù)據(jù)庫
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容