hibernate的環境搭建及使用

一、hibernate簡介

1.什麼是hibernate

  • hibernate是一個開放源代碼的對象關係映射框架,對JDBC進行了非常輕量級的對象封裝,將POJO(plain ordinary Java object)與數據庫表建立映射關係,是一個全自動的持久層的ORM框架。
  • hibernate是一個基於JDBC的主流持久化框架,是一個優秀的ORM實現,很大程度上簡化了DAO(data access object)層編碼工作。

2.什麼是ORM

  • ORM(object relational mapping)對象關係映射,指的是將一個Java中的對象與關係型數據庫中的表建立一種映射,從而操作對象就可以操作數據庫中的表。

二、環境搭建

1.工程環境

2.hibernate映射配置

  • class標籤,用來建立類與表之間的關係name:類名,table:表名
  • id標籤,建立中的屬性與表中的主鍵的對應關係
  • property,建立類中的普通屬性與表的字段的對應關係

3.hibernate的核心配置

  • 必須的配置:連接數據庫的基本參數:驅動類、URL路徑、用戶名、密碼,方言的配置以及映射文件的引入
  • 可選的配置顯示化SQL語句、格式化SQL語句、自動建表

三、代碼測試

1.代碼目錄結構

1

2.持久化類的編寫

package com.itheima.hibernate.demo1;

/**
 * @author 若風
 * @version 1.0
 */
public class Customer {
    private Long cust_id;
    private String cust_name;
    private String cust_source;
    private String cuso_industry;
    private String cust_level;
    private String cust_phone;
    private String cust_mobile;

    public Long getCust_id() {
        return cust_id;
    }

    public void setCust_id(Long cust_id) {
        this.cust_id = cust_id;
    }

    public String getCust_name() {
        return cust_name;
    }

    public void setCust_name(String cust_name) {
        this.cust_name = cust_name;
    }

    public String getCust_source() {
        return cust_source;
    }

    public void setCust_source(String cust_source) {
        this.cust_source = cust_source;
    }

    public String getCuso_industry() {
        return cuso_industry;
    }

    public void setCuso_industry(String cuso_industry) {
        this.cuso_industry = cuso_industry;
    }

    public String getCust_level() {
        return cust_level;
    }

    public void setCust_level(String cust_level) {
        this.cust_level = cust_level;
    }

    public String getCust_phone() {
        return cust_phone;
    }

    public void setCust_phone(String cust_phone) {
        this.cust_phone = cust_phone;
    }

    public String getCust_mobile() {
        return cust_mobile;
    }

    public void setCust_mobile(String cust_mobile) {
        this.cust_mobile = cust_mobile;
    }
    
}

3.hibernate映射文件的編寫

<?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>
    <!--建立類與表的映射-->
    <class name="com.itheima.hibernate.demo1.Customer" table="cst_customer">
        <!--建立類中的屬性與表中的屬性主鍵對應-->
        <id name="cust_id" column="cust_id">
            <generator class="native"/>
        </id>
        <property name="cust_name" column="cust_name"/>
        <property name="cust_source" column="cust_source"/>
        <property name="cuso_industry" column="cust_industry"/>
        <property name="cust_level" column="cust_level"/>
        <property name="cust_phone" column="cust_phone"/>
        <property name="cust_mobile" column="cust_mobile"/>
    </class>
</hibernate-mapping>

4.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.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate?useUnicode=true&amp;characterEncoding=utf-8</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <!-- 配置Hibernate的方言 -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- 可選配置================ -->
        <!-- 打印SQL -->
        <property name="hibernate.show_sql">true</property>
        <!-- 格式化SQL -->
        <property name="hibernate.format_sql">true</property>
        <!-- 自動創建表 -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!-- 映射文件的引入 -->
        <mapping resource="com/itheima/hibernate/demo1/Customer.hbm.xml"/>
    </session-factory>
</hibernate-configuration>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章