Hibernate的基礎

weixin_33913332發表於2018-11-20
13118720-62a6a1e0ebff55fd.png

1.Hibernate的基本使用方式
 1.Hibernate是一個物件關係對映框架
 2.Hibernate的命名規範(儘量使用 類名.hbm.xml的命名規範)

 3.建立實體類與表的對映
Customer.java

package com.wuhaitao.hibernate.demo1;
/**
 * CREATE TABLE `cst_customer` (
  `cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客戶編號(主鍵)',
  `cust_name` varchar(32) NOT NULL COMMENT '客戶名稱(公司名稱)',
  `cust_source` varchar(32) DEFAULT NULL COMMENT '客戶資訊來源',
  `cust_industry` varchar(32) DEFAULT NULL COMMENT '客戶所屬行業',
  `cust_level` varchar(32) DEFAULT NULL COMMENT '客戶級別',
  `cust_phone` varchar(64) DEFAULT NULL COMMENT '固定電話',
  `cust_mobile` varchar(16) DEFAULT NULL COMMENT '行動電話',
  PRIMARY KEY (`cust_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
 * @author wuhaitao
 *
 */
public class Customer {
    //實體類
    private long cust_id;
    private String cust_name;
    private String cust_source;
    private String cust_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 getCust_industry() {
        return cust_industry;
    }
    public void setCust_industry(String cust_industry) {
        this.cust_industry = cust_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;
    }
    
}

Customer.hbm.xml

<?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.wuhaitao.hibernate.demo1.Customer" table="cst_customer">
            <!-- id屬性是建立於表的主鍵對映 -->
            <id name="cust_id" column="cust_id">
                <generator class="native"></generator>
            </id>
            <!-- 表的普通屬性和表的欄位對映 -->
            <property name="cust_name" column="cust_name"/>
            <property name="cust_source" column="cust_source"/>
            <property name="cust_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.cfg.xml
hibernate.cfg.xml

<?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.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <!--為了方便除錯是否在執行hibernate時在日誌中輸出sql語句 -->
        <!-- 資料庫驅動 -->
        <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <!-- 資料庫連線url ,由於用的Mysql8 必須配置時區和SSl,並且驅動也改了-->
        <property name="hibernate.connection.url">jdbc:mysql:///hibernate_day01?useSSL=false&amp;serverTimezone=UTC</property>
        <!-- 資料庫使用者名稱和密碼 -->
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">123456</property>
        <!-- Hibernate 方言 -->
        
        <property name="hibernate.show_sql">true</property>
        <!-- 是否對日誌中輸出的sql語句進行格式化 -->
        <property name="hibernate.format_sql">true</property>
        <!-- 引入對映檔案 -->
        <mapping resource="com/wuhaitao/hibernate/demo1/customer.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

 5.在核心配置檔案中引入對映檔案


13118720-65727087079a1581.png
  1. 案例測試
    HibernateDemo1.java
package com.wuhaitao.hibernate.demo1;

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

public class HibernateDemo1 {
    @Test
    public void test() {
        //1.載入核心配置檔案
        Configuration configuration = new Configuration().configure();
        //2.建立SessionFactory物件
        SessionFactory sessionFactory =   configuration.buildSessionFactory();
        //3.得到session物件
        Session session =  sessionFactory.openSession();
        //4.手動開啟事務
        Transaction beginTransaction = session.beginTransaction();
        //5.編寫程式碼
        Customer customer = new Customer();
        customer.setCust_name("馬雲");
        session.save(customer);
        //6.提交事務
        beginTransaction.commit();
        //7.釋放資源
        session.close();
    }
}

結果


13118720-748a5d8abe170cfb.png
結果

相關文章