Hibernate框架介紹以及入門 【一】Hibernate 快速入門 框架的概述 什麼是 Hibernate Hibernate 持久層的ORM框架 下載Hibernate 官網 編寫測試代碼

Hibernate框架
Hibernate框架入門:
在這裏插入圖片描述

一、框架的概述

1、什麼是框架

框架:指的是軟件的半成品,已經完成的部分功能。
二、EE的三層結構
在這裏插入圖片描述

三、什麼是 Hibernate

Hibernate (開放源代碼的對象關係映射框架)
Hibernate是一個開放源代碼的對象關係映射框架,它對JDBC進行了非常輕量級的對象封裝,它將POJO與數據庫表建立映射關係,是一個全自動的orm框架,hibernate可以自動生成SQL語句,自動執行,使得Java程序員可以隨心所欲的使用對象編程思維來操縱數據庫。
Hibernate可以應用在任何使用JDBC的場合,既可以在Java的客戶端程序使用,也可以在Servlet/JSP的Web應用中使用,最具革命意義的是,Hibernate可以在應用EJB的JaveEE架構中取代CMP,完成數據持久化的重任。

1、Hibernate 持久層的ORM框架

什麼是ORM Object Relational Mapping(對象關係映射)。指的是對象與關係型數據庫中的表建立一種映射關係。從而來操作我們的對象就可以操作數據庫當中的表。
在這裏插入圖片描述

四、Hibernate 特點

在這裏插入圖片描述
在這裏插入圖片描述

五、Hibernate 入門

1、下載Hibernate 官網

https://sourceforge.net/projects/hibernate/
在這裏插入圖片描述
下載好解壓

2、Hibernate 目錄文件當中各自作用

在這裏插入圖片描述
documentation: Hibernate 開發文檔
lib:Hibernate 開發包
—》required : Hibernate 開發的必須依賴的包
—》optional: Hibernate 開發的可選的jar包
project:提供的測試的項目

3、創建一個項目引入jar包

1、引入數據庫的驅動包
2、Hibernate 開發必須的jar包
3、Hibernate 引入日誌記錄包:
在這裏插入圖片描述
然後Builder path

4、創建表

在數據庫當中創建表

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;

5、創建實體類Customer

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;
	}
	public Customer() {
		// TODO Auto-generated constructor stub
	}
	public Customer(Long cust_id, String cust_name, String cust_source, String cust_industry, String cust_level,
			String cust_phone, String cust_mobile) {
		super();
		this.cust_id = cust_id;
		this.cust_name = cust_name;
		this.cust_source = cust_source;
		this.cust_industry = cust_industry;
		this.cust_level = cust_level;
		this.cust_phone = cust_phone;
		this.cust_mobile = cust_mobile;
	}
	@Override
	public String toString() {
		return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + ", cust_source=" + cust_source
				+ ", cust_industry=" + cust_industry + ", cust_level=" + cust_level + ", cust_phone=" + cust_phone
				+ ", cust_mobile=" + cust_mobile + "]";
	}
}

6、創建映射

映射需要通過XML配置文件來完成,這個配置文件可以任意命名。儘量統一命名規範(類名.hbm.xml)
複製該頭,引入jar當中的配置
在這裏插入圖片描述
在這裏插入圖片描述

7、創建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.itzheng.hibernate.demo01.Customer"
		table="cst_customer">
		<!-- 建立類當中的屬性與表當中的主鍵對應的 -->
		<id name="cust_id" column="cust_id">
			<generator class="native" />
		</id>
		<!-- 建立類當中普通的屬性和表的字段的對應 -->
		<property name="cust_name" column="cust_name"></property>
		<property name="cust_source" column="cust_source"></property>
		<property name="cust_industry" column="cust_industry"></property>
		<property name="cust_level" column="cust_level"></property>
		<property name="cust_phone" column="cust_phone"></property>
		<property name="cust_mobile" column="cust_mobile"></property>
	</class>
</hibernate-mapping>

8、創建hibernate的核心配置文件*

hibernate核心配置文件的名稱: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.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql:///hibernate_day01</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>
		<mapping resource="com/itzheng/hibernate/demo01/Customer.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

配置文件與hibernate提供資料的聯繫
在這裏插入圖片描述

9、編寫測試代碼

在這裏可能會遇到jdk新版本和hibernate不兼容的問題

真正解決方案:java.lang.ClassNotFoundException: javax.xml.bind.JAXBException

參考解決辦法https://blog.csdn.net/hadues/article/details/79188793

public class HibernateDemo01 {
	@Test
	// 保存客戶的案例
	public void demo01() {
		// 1、加載Hibernate核心配置文件
		Configuration configuration = new Configuration().configure();// 該方法加載了Customer.hbm.xml
		// 2、創建SessionFactory對象:類似於JDBC中的鏈接池
		SessionFactory sessionFactory = configuration.buildSessionFactory();// sessionFactory工廠
		// 3、通過SessionFactory獲取到Session對象:類似於我們JDBC中的Connection
		Session session = sessionFactory.openSession();// Hibernate以及和mysql數據庫建立起鏈接
		// 4、手動開啓事務:
		Transaction transaction = session.beginTransaction();
		// 5、編寫代碼
		
		Customer customer = new Customer();
		customer.setCust_name("張三");
		session.save(customer);
		// 6、事務提交
		transaction.commit();
		// 7、資源釋放
		session.close();
	}
}

輸出該結果表示插入數據庫數據成功
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章