hibernate的詳解

一,環境的搭建html

1)建立maven項目java

 

 

 

 2)導入依賴的jar包.pom.xml和建立實體類Usermysql

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.hibernate</groupId>
  <artifactId>hibernate004</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>hibernate004</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.1.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>5.3.3.Final</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator -->
    <dependency>
      <groupId>org.hibernate.validator</groupId>
      <artifactId>hibernate-validator</artifactId>
      <version>6.0.7.Final</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>5.3.6.Final</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-c3p0 -->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-c3p0</artifactId>
      <version>5.3.6.Final</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
    <dependency>
      <groupId>com.mchange</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.5.2</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.35</version>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.7.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.20.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

實體類web

package com.hibernate;

public class User {
    private Integer id;
    private Integer age;
    private String name;
    private String password;

    public User() {
    }

    public Integer getId() {
        return id;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    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;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", age=" + age +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

3)建立resources的文件夾和建立hibernate.cfg.xml的配置文件以及hibernate.hbm.xml文件sql

hibernate.cfg.xml文件數據庫

<?xml version="1.0" encoding="GBK"?>
<!-- 指定Hibernate配置文件的DTD信息 -->
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- hibernate- configuration是鏈接配置文件的根元素 -->
<hibernate-configuration>
    <session-factory>
        <!-- 指定鏈接數據庫所用的驅動 -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <!-- 指定鏈接數據庫的url,hibernate鏈接的數據庫名 -->
        <property name="connection.url">jdbc:mysql://localhost/hibernate001</property>
        <!-- 指定鏈接數據庫的用戶名 -->
        <property name="connection.username">root</property>
        <!-- 指定鏈接數據庫的密碼 -->
        <property name="connection.password">123456</property>
        <!-- 指定鏈接池裏最大鏈接數 -->
        <property name="hibernate.c3p0.max_size">20</property>
        <!-- 指定鏈接池裏最小鏈接數 -->
        <property name="hibernate.c3p0.min_size">1</property>
        <!-- 指定鏈接池裏鏈接的超時時長 -->
        <property name="hibernate.c3p0.timeout">5000</property>
        <!-- 指定鏈接池裏最大緩存多少個Statement對象 -->
        <property name="hibernate.c3p0.max_statements">100</property>
        <property name="hibernate.c3p0.idle_test_period">3000</property>
        <property name="hibernate.c3p0.acquire_increment">2</property>
        <property name="hibernate.c3p0.validate">true</property>
        <!-- 指定數據庫方言 -->
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <!-- 根據須要自動建立數據表 -->
        <property name="hbm2ddl.auto">update</property>
        <!-- 顯示Hibernate持久化操做所生成的SQL -->
        <property name="show_sql">true</property>
        <!-- 將SQL腳本進行格式化後再輸出 -->
        <property name="hibernate.format_sql">true</property>
        <!--設置數據源,因爲上述使用的屬性有hibernate.c3p0.*,會自動設置爲C3P0可不設。默認爲hibernate數據源。-->
        <property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property>
        <!--current_session做用域,thread表示當前線程用同一個session-->
        <property name="current_session_context_class">thread</property>
        <!-- 羅列全部的映射文件,resources爲文件路徑,最好和主配置文件同一目錄,本身開始沒同一目錄報錯了.緣由未知,就是找不到 -->
        <mapping resource="hibernate.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

hibernate.hbm.xml文件apache

<?xml version="1.0"?>
        <!DOCTYPE hibernate-mapping PUBLIC
                "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
        <!--package爲實體類包名-->
<hibernate-mapping package="com.hibernate">
    <!--name爲類名,若是沒寫包名,這裏的類名要寫成權限定性類名.
    table爲表名,不寫默認爲簡單類名。tips:Windows系統mysql數據庫默認不區分大小寫。-->
    <class name="User" table="user">
        <!--id標籤表示這是主鍵,name爲實體類屬性名,column爲對應表的
        gernarator參考https://blog.csdn.net/ye1992/article/details/19632001說明
        這裏native就行了。-->
        <id name="id" column="id">
            <generator class="native"/>
        </id>
        <!--name爲屬性名,colunmn就是列名-->
        <property name="age" column="age"/>
        <property name="name" column="name"/>
        <property name="password" column="password"/>
    </class>
</hibernate-mapping>

 

4) 建立一個hibernateUtile工具類和測試類緩存

hibernateUtile類session

package com.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


public class HibernateUtil {
    private static SessionFactory sf;
    static{
        Configuration configure = new Configuration().configure();
        sf= configure.buildSessionFactory();
    }

    //opensession
    public static Session getOpenSession(){
        return sf.openSession();
    }
    //currentsession
    public static Session getCurrentSession(){
        return sf.getCurrentSession();
    }
}

測試類併發

package com.hibernate;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

public class Hibernate_test {
    @Test
    public void test01(){
        Session session=HibernateUtil.getOpenSession();
        Transaction transaction = session.beginTransaction();

        User user=session.get(User.class,2);
        System.out.println(user);

        transaction.commit();
        session.close();
    }
}

測試結果

說明hibernate的環境搭建成功了

二,配置文件說明

hibernate有兩個配置文件 hibernate.cfg.xml和hibernate.hbm.xml

配置文件中的主要內容,已經加上了註釋信息,能夠網上查看

三,什麼是hibernate?

Hibernate是一個操做數據庫的持久層框架,實現了對JDBC的封裝,.Hibernate是一個ORM(對象關係映射)框架,咱們在寫程序時 ,用的時面向對象的方法,可是在關係型數據庫裏,存的是一條條的數據,爲了用純面向對象的思想解決問題,全部須要將程序中的對象和數據庫的記錄創建起映射關係,ORM就是這樣的技術,而Hibernate就是這樣一個框架,以操做對象的方式操做數據庫。

四,hibernate相關API

configuration:配置對象

在使用hibernate的時候,首先要建立configuration的實例,Configuration實例主要用於啓動,加載,管理hibernate的配置文件信息,在啓動hibernate的過程當中,configuration首先肯定hibernate的配置文件的位置,而後讀取相關配置,最後建立一個惟一的SessionFactory實例,configuration值存在於最初的系統初始化階段,它將sessionFactory建立完成以後,就完成它的使命了.hibernate一般會使用new Configuration().configure(),來建立一個配置類對象,默認的是在resources下的hibernate.cfg.xml配置文件,若是不想建立和該文件名同樣的xml配置文件,能夠在cofigure()中添加一個參數,參數的內容是配置文件的名稱

 sessionFactory 是session的工廠對象

sessionFactory接口負責將Hibernate的初始化和創建session對象,它在hibernate中起到一個緩衝區的做用,hibernate能夠將自動生成的sql語句,映射數據以及某些可重複利用的數據放在這個緩衝區中,同時它還保存了對數據庫配置的全部映射關係,維護了當前的二級緩存

 

 

session對象

五,transaction事務

六,

1)什麼是持久化類

 2)持久化的編寫規則

3)hibernate的主鍵生成策略

 

 

七,持久化狀態的三種描述

區分對象的三種狀態

hibernate三種轉態之間的相互轉換

 

 

持久化對象可以自動更新數據庫

hibernate的一級緩存

什麼是hibernate的一級緩存

測試一級緩存

 

 

一級緩存的快照

hibernate的事務控制

1)什麼是事務

2)事務的四個特性

3)事務的併發問題

4)事務的隔離級別

 

5)事務管理

HQL

1)query

SQLQuery

相關文章
相關標籤/搜索