Hibernate概述(5.0.7版本)
- 是一個標(biāo)準(zhǔn)的ORM框架
- 操作對象就相當(dāng)于操作表結(jié)構(gòu)通過映射文件把對象和表關(guān)聯(lián)起來
- 對JDBC的一個封裝
優(yōu)點:
1、 簡化了jdbc的繁瑣編碼
2、對面向?qū)ο筇匦缘牧己弥С?/p>
缺點:
1、不支持動態(tài)sql,靈活性較低
2、 不適合大規(guī)模的數(shù)據(jù)處理
開發(fā)環(huán)境的搭建
1、導(dǎo)入jar包
Hibernate壓縮包下lib包下的required包下所有的jar包+log4j+mysql驅(qū)動包
2、配置映射文件
寫在實體類同包下 推薦命名規(guī)則 類名.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>
<!-- name實體類全路徑 table表名-->
<class name="com.hemi.bean.User" table="user">
<!-- name實體類屬性名 column表中字段名 -->
<id name="id" column="id">
<!-- 規(guī)定主鍵生成策略 -->
<generator class="native"></generator>
</id>
<property name="name" column="name"></property>
<property name="age" column="age"></property>
</class>
</hibernate-mapping>
主鍵生成策略:最常用兩種
native(根據(jù)本地數(shù)據(jù)庫支持主鍵生成方式)
uuid(根據(jù)uuid算法生成一個32位16進(jìn)制的字符串)
3、配置文件
寫在src根目錄下 推薦命名規(guī)則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>
<!-- 配置連接數(shù)據(jù)庫4大參數(shù) -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<!-- 配置方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 選配 -->
<!-- 打印sql語句 -->
<property name="show_sql">true</property>
<!-- 格式化sql語句 -->
<property name="format_sql">true</property>
<!-- 映射生成user表 -->
<property name="hbm2ddl.auto">update</property>
<!-- 加載映射配置文件 -->
<mapping resource="com/hemi/bean/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4、使用Hibernate APi操作
a、得到工廠對象
//創(chuàng)建Configuration對象
Configuration configuration = new Configuration();
//加載配置文件 默認(rèn)加載名為 hibernate.cfg.xml的文件
Configuration configure = configuration.configure();
//創(chuàng)建SessionFactory
SessionFactory factory = configure.buildSessionFactory();
System.out.println(factory);
b、打開session
Session session = factory.openSession();
c、開啟事務(wù)
Transaction transaction = session.beginTransaction();
d、crud操作
//創(chuàng)建對象并設(shè)置值
User user=new User();
user.setAge(20);
user.setName("marry1");
//通過save方法添加數(shù)據(jù)到數(shù)據(jù)庫
session.save(user);
e、提交事務(wù)
transaction.commit();
f、關(guān)閉session
session.close();
session方法
增和改
save(object);//增
save執(zhí)行過后對象為持久態(tài)
saveOrUpdate(object);
1、插入或修改
2、當(dāng)沒設(shè)置存入數(shù)據(jù)的主鍵執(zhí)行save
3、設(shè)置了 執(zhí)行update
4、當(dāng)主鍵不存在報錯
merge(object);
1、插入或修改 先查詢
2、當(dāng)沒設(shè)置存入數(shù)據(jù)的主鍵執(zhí)行save
3、設(shè)置了 先去查詢 如果主鍵存在 則修改 不存在則插入
4、瞬時態(tài)的數(shù)據(jù)被merge操作后變成游離態(tài),返回的數(shù)據(jù)為持久態(tài)
update(object);//改 建議先查詢后修改
查詢
get(實體類.class,主鍵);
一執(zhí)行馬上發(fā)生sql語句查詢
load(實體類.class,主鍵);
延遲查詢 執(zhí)行后不馬上去查詢 當(dāng)有用到查詢結(jié)果時才發(fā)生sql語句去查詢
刪
delete(object); //建議先查再刪
對象三大狀態(tài)
1、瞬時態(tài): 剛創(chuàng)建
2、持久態(tài): 被保存到數(shù)據(jù)庫中且被持久化(session緩存中) [既數(shù)據(jù)庫中有session緩存中也有]
3、游離態(tài): 被持久化過后 數(shù)據(jù)庫中有之對應(yīng)的記錄但是脫離了session

快照機制
從數(shù)據(jù)庫查詢出的數(shù)據(jù)會在緩存和快照區(qū)各有一個 當(dāng)調(diào)用clear或者close方法刷新緩存時,來和快照區(qū)數(shù)據(jù)比對,有更新就推入數(shù)據(jù)庫