Hibernate
介紹一、Hibernate
是一個持久層的ORM
框架java
二、ORM
:Object Relational Mapping
(對象關係映射),指的是將一個java
中的對象與關係型數據庫創建一種映射關係,從而操做對象就能夠操做數據庫中的表。mysql
Hibernate
優勢 (摘自百度百科)一、封裝了jdbc
,簡化了不少重複性代碼。sql
二、簡化了DAO
層編碼工做,使開發更對象化了。數據庫
三、移植性好,支持各類數據庫,若是換個數據庫只要在配置文件中變換配置就能夠了,不用改變hibernate
代碼。bash
四、支持透明持久化,由於hibernate
操做的是純粹的(pojo)java
類,沒有實現任何接口,沒有侵入性。因此說它是一個輕量級框架。session
Hibernate
使用教程documentation
:Hibernate
開發的文檔lib
:Hibernate
開發包
required
:Hibernate
開發的必須的依賴包optional
:Hibernate
開發的可選的jar包project
:Hibernate
提供的項目jar
包以下①️、開發必須的依賴包app
②、項目須要鏈接數據庫,因此要導入jdbc
包框架
③、日誌記錄包單元測試
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;
複製代碼
自動生成get、set方法,重寫toString方法
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;
}
複製代碼
這個文件是類與表的映射,建立在與Customer
類相同的包下,切記位置不要放錯測試
<?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.tfq.hibernate.demo1.Customer" table="cst_customer">
<!-- 創建類中的屬性與表中的主鍵對應 -->
<id name="cust_id" column="cust_id">
<!-- 主鍵的生成機制 -->
<generator class="native"></generator>
</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>
複製代碼
Hibernate
核心配置文件這個文件裏包含了與數據庫的鏈接、與映射文件的關聯、Hibernate
的其它配置,放在src
目錄下,切記位置一樣不可放錯
<?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?useUnicode=true&characterEncoding=UTF-8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">admin</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/tfq/hibernate/demo1/hibernate.hbm.xml"/>
</session-factory>
</hibernate-configuration>
複製代碼
經過單元測試的方式編寫代碼,測試程序
@Test
public void demo(){
//1.加載hibernate核心配置文件
Configuration con = new Configuration().configure();
//2.建立一個sessionFactory對象,相似與JDBC中的鏈接池
SessionFactory sf = con.buildSessionFactory();
//3.經過SessionFactory獲取到Session對象,相似與JDBC中Connection
Session session = sf.openSession();
//4.開啓事務
Transaction ts = session.beginTransaction();
//5.編寫代碼
Customer ct = new Customer();
ct.setCust_name("張三");
session.save(ct);
//6.事物提交
ts.commit();
//7.資源釋放
session.close();
}
複製代碼
Hibernate
的CRUD
方便起見,這裏只寫關鍵代碼,具體代碼見三-6
// 保存數據
// Customer customer3 = new Customer();
// customer3.setCust_name("李四");
// session.save(customer3);
複製代碼
// 刪除數據
// Customer customer5 = new Customer();
// customer5.setCust_id(1L);
// session.delete(customer5);
複製代碼
// 更新數據 通常都是先查出來而後更新,不然就會覆蓋,
// Customer customer4 = new Customer();//這種會覆蓋原有數據
// Customer customer = session.get(Customer.class, 12L);//這種就是更新數據
// customer4.setCust_id(1L);
// customer4.setCust_name("哈哈哈");
// session.update(customer4);
複製代碼
// 查詢單條數據
// Customer customer = session.get(Customer.class, 12L);
// Customer customer2 = session.load(Customer.class, 2L);
// System.out.println(customer);
// System.out.println(customer2);
複製代碼
兩種查詢方式的區別:
load
一、懶加載,真正使用的時候纔會發送sql語句
二、查詢後返回的是代理對象,利用javassist
技術產生的代理
三、查詢找不到對象的時候返回ObjectNotFoundException
get
一、當即加載
二、查詢後返回的是真實對象自己
三、查詢一個找不到的對象返回null
// 查詢全部數據
// 接收HQL Hibernate Query Language 面向對象的查詢語言
// 注意這裏 的 Customer是類名 不是表名
Query query = session.createQuery("from Customer");
List<Customer> list = query.list();
for (Customer customer : list) {
System.out.println(customer);
}
// 接收SQL
SQLQuery sqlQuery = session.createSQLQuery("select * from cst_customer");
List<Object[]> list2 = sqlQuery.list();
for (Object[] objects : list2) {
System.out.println(Arrays.toString(objects));
}
複製代碼