####1.1Hibernate框架的學習路線
?第一天:Hibernate的入門(Hibernate的環境搭建、Hibernate的API、Hibernate的CRUD)
?第二天:Hibernate的一級緩存、其他的API
?第三天:Hibernate的一對多配置、Hibernate的多對多的配置
?第四天:Hibernate的查詢方式、抓取策略
?
####1.2CRM的案例
1.2.1CRM的概述(了解)
1.2.1.1什么是CRM 客戶關系管理
1.2.1.2CRM有哪些模塊
#####1.3Hibernate的框架的概述
1.3.1.1什么是框架
框架:指的是軟件的半成品,已經完成了部分功能。
1.3.2EE的三層架構
1.3.2.1EE的經典三層結構
ORM:對象關系映射,指的是將一個java中的對象與關系型數據庫中的表建立一種映射關系,從而操作對象就可以完成數據庫的相關操作
?
###實例如下:
首先先看下數據庫的創造
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;
?
這樣就創建了一個數據表
然后就是根據這個數據表創建一個對應的類
public class HibernateDemo1 {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;}}
?
?
下面就是配置類和數據表之間的關聯。命名的規則是xxx.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的導包中找到--><!--下面就是開始創建對應的映射關系--><hibernate-mapping><!-- 建立類與表的映射 --><class name="com.hibernate.learn.HibernateDemo1" table="cst_customer"><!-- 建立類中的屬性與表中的主鍵對應--><id name="cust_id" column="cust_id"><generator class="native"></generator></id><!-- 建立類中的普通的屬性和表中字段的對應。除了主鍵用id其他都用property --><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.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的相關配置--> <hibernate-configuration><session-factory><!-- 連接數據庫的基本配置。這塊屬于數據庫的固定配置模式,這里的數據庫是mysql數據庫--><property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property><property name="hibernate.connection.username">root</property><property name="hibernate.connection.password">root</property><!-- 配置Hibernate的方言。意思是指你采用的是什么數據庫,因為mysql,orcle等很多數據庫的要求是不一樣的。所以要在此處進行配置 --><property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property><!-- 打印sql語句到控制臺,不是必須要有的配置--><property name="hibernate.show_sql">true</property><!-- 自動建表的配置 --><property name="hibernate.hbm2ddl.auto">update</property><!-- 配置C3P0連接池 .如果需要使用c3p0可以通過這種方式來完成導入--><property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property><!--在連接池中可用的數據庫連接的最少數目 --><property name="c3p0.min_size">5</property><!--在連接池中所有數據庫連接的最大數目 --><property name="c3p0.max_size">20</property><!--設定數據庫連接的過期時間,以秒為單位,如果連接池中的某個數據庫連接處于空閑狀態的時間超過了timeout時間,就會從連接池中清除 --><property name="c3p0.timeout">120</property><!--每3000秒檢查所有連接池中的空閑連接 以秒為單位--><property name="c3p0.idle_test_period">3000</property><!-- 格式化sql,是控制臺輸出的數據庫語句更加美觀,不是必須要有的配置 --><property name="hibernate.format_sql">true</property><!--最終映射到剛剛的類與數據表映射的配置文件xxx.hbm.xml--><mapping resource="com/hibernate/learn/Hibernate.hbm.xml"></mapping></session-factory> </hibernate-configuration>
?
至此Hibernate的相關配置完成
最后開始對數據庫經行操作
public static void main(String[] args) throws SecurityException, HeuristicMixedException, HeuristicRollbackException, RollbackException, SystemException {//保存客戶的案例//1.加載Hibernate核心配置文件Configuration configuration=new Configuration().configure();//2.創建一個SessionFactory對象:類似于JDBC中連接池SessionFactory sessionFactory=configuration.buildSessionFactory();//3.通過SessionFactory獲取到Session對象,類似于JDBC中的ConnectionSession session=sessionFactory.openSession();//4.手動開啟事務:Transaction transaction=(Transaction) session.beginTransaction();//5.編寫代碼HibernateDemo1 hibernateDemo1=new HibernateDemo1();hibernateDemo1.setCust_name("zyz");session.save(hibernateDemo1);//6.事務提交 transaction.commit();//7.釋放資源 session.close();}OK至此相關的簡單的案例完成
?
?
?
#####1.5Hibernate的相關配置
###【class標簽的配置】
?標簽用來建立類與表的映射關系
?屬性:
? name :類的全路徑
? table :表名(類名與表名一致,table可以省略)
? catalog :數據庫名
###【id標簽的配置】
?標簽用來建立類中的屬性與表中的主鍵的對應關系
? 屬性:
? name :類中的屬性名
? column :表中的字段名(類中的屬性名和表中的字段名如果一致,column可以省略)
? length :長度
? type :類型
###【property標簽的配置】
?標簽用來建立類中的普通屬性與表的字段的對應關系
?屬性:
?name :類中的屬性名
?column :表中的字段名
?length :長度
?type :類型
?not-null :設置非空
?unique :設置唯一
?
#######核心配置
?#######必須的配置
? 連接數據庫的基本的參數
? 驅動類
? url路徑
? 用戶名
? 密碼
? 方言
####可選的配置
? 顯示SQL :hibernate.show_sql
? 格式化SQL :hibernate.format_sql
#####自動建表 :hibernate.hbm2ddl.auto
?none :不使用hibernate的自動建表
?create :如果數據庫中已經有表,刪除原有表,重新創建,如果沒有表,新建表。(測試)
?create-drop :如果數據庫中已經有表,刪除原有表,執行操作,刪除這個表。如果沒有表,新建一個,使用完了刪除該表。(測試)
?update :如果數據庫中有表,使用原有表,如果沒有表,創建新表(更新表結構)
?validate :如果沒有表,不會創建表。只會使用數據庫中原有的表。(校驗映射和表結構)。
?映射文件的引入
?引入映射文件的位置 <mapping resource="com/hibernate/learn/Hibernate.hbm.xml"></mapping>
?
#######1.6Hibernate的核心API
####作用:
###加載核心配置文件
? hibernate.properties
Configuration cfg = new Configuration();
?hibernate.cfg.xml
Configuration cfg = new Configuration().configure();
?加載映射文件
// 手動加載映射
configuration.addResource("com/itheima/hibernate/demo1/Customer.hbm.xml");
?
##########SessionFactory:Session工廠
SessionFactory內部維護了Hibernate的連接池和Hibernate的二級緩存(不講)。是線程安全的對象。一個項目創建一個對象即可。
<!-- 配置C3P0連接池 --> <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> <!--在連接池中可用的數據庫連接的最少數目 --> <property name="c3p0.min_size">5</property> <!--在連接池中所有數據庫連接的最大數目 --> <property name="c3p0.max_size">20</property> <!--設定數據庫連接的過期時間,以秒為單位, 如果連接池中的某個數據庫連接處于空閑狀態的時間超過了timeout時間,就會從連接池中清除 --> <property name="c3p0.timeout">120</property> <!--每3000秒檢查所有連接池中的空閑連接 以秒為單位--> <property name="c3p0.idle_test_period">3000</property>
?
?
#############Session:類似Connection對象是連接對象
#####Session代表的是Hibernate與數據庫的鏈接對象。不是線程安全的。與數據庫交互橋梁。
? #####Session中的API
? 保存方法:
? Serializable save(Object obj);
public void test(){Session session=HibernateUtils.GetSession();//手動開啟事務Transaction transaction=session.beginTransaction();HibernateDemo1 hibernateDemo1=new HibernateDemo1();hibernateDemo1.setCust_name("宋培闖");session.save(hibernateDemo1);transaction.commit();//事件的提交session.close();//會話關閉}
?
? 查詢方法:
? T get(Class c,Serializable id);
? T load(Class c,Serializable id);
? get方法和load方法的區別?
//使用get的方法進行查詢public void TestDemo2(){Session session=HibernateUtils.GetSession();//手動開啟事務Transaction transaction=session.beginTransaction();//使用get方式查詢HibernateDemo1 hibernateDemo1=new HibernateDemo1();hibernateDemo1=session.get(HibernateDemo1.class,1l);System.out.println(hibernateDemo1.toString());transaction.commit();//事件的提交session.close();//會話關閉 }//使用load方式查詢public void TestLoad(){Session session=HibernateUtils.GetSession();//手動開啟事務Transaction transaction=session.beginTransaction();HibernateDemo1 hibernateDemo1=new HibernateDemo1();hibernateDemo1=session.load(HibernateDemo1.class,2l);System.out.println(hibernateDemo1.toString());transaction.commit();//事件的提交session.close();//會話關閉}
?
?
區別
?
? 修改方法
? void update(Object obj);
//修改的方法public void updateTest(){Session session=HibernateUtils.GetSession();//手動開啟事務Transaction transaction=session.beginTransaction();//直接創建對象,進行修改:不推薦使用,因為會導致為設置的值全部變為空/* HibernateDemo1 hibernateDemo1=new HibernateDemo1();hibernateDemo1.setCust_id(1l);hibernateDemo1.setCust_name("修改名字");session.update(hibernateDemo1);*///先查詢在修改HibernateDemo1 hibernateDemo1=new HibernateDemo1();hibernateDemo1=session.get(HibernateDemo1.class,1l);hibernateDemo1.setCust_name("修改名字");session.update(hibernateDemo1);transaction.commit();//事件的提交session.close();//會話關閉}
?
?
? 刪除方法
? void delete(Object obj);
//刪除的方法public void DeleteTest(){Session session=HibernateUtils.GetSession();//手動開啟事務Transaction transaction=session.beginTransaction();//直接創建對象刪除/*HibernateDemo1 hibernateDemo1=new HibernateDemo1();hibernateDemo1.setCust_id(1l);session.delete(hibernateDemo1);*///先查詢再刪除,推薦使用這一種方式HibernateDemo1 hibernateDemo1=new HibernateDemo1();hibernateDemo1=session.get(HibernateDemo1.class,1l);session.delete(hibernateDemo1);transaction.commit();//事件的提交session.close();//會話關閉 }
?
?
?保存或更新
? void saveOrUpdate(Object obj)
?
//保存或更新的方法(不常用)public void TestDemo1(){Session session=HibernateUtils.GetSession();//手動開啟事務Transaction transaction=session.beginTransaction();//如果數據庫中沒有這條數據就是添加,如果有這條數據,那就是修改HibernateDemo1 hibernateDemo1=new HibernateDemo1();hibernateDemo1.setCust_name("testets");session.saveOrUpdate(hibernateDemo1);transaction.commit();//事件的提交session.close();//會話關閉}
?
?
?
?
//查詢所有的信息public void TestDemo03(){Session session=HibernateUtils.GetSession();//手動開啟事務Transaction transaction=session.beginTransaction();//接收HQL:hibernate Query Language 面向對象的查詢語句/*org.hibernate.Query query=session.createQuery("from HibernateDemo1");List<HibernateDemo1> list=query.list();for(HibernateDemo1 hibernateDemo1:list){System.out.println(hibernateDemo1.toString());}*///接收SQLSQLQuery query=session.createSQLQuery("select *from cst_customer");List <Object[]> list=query.list();for(Object[] objects:list){System.out.println(Arrays.toString(objects));}transaction.commit();//事件的提交session.close();//會話關閉}
?
?
?其中HibernateUtils代碼如下
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration;public class HibernateUtils {//這個包的主要重用是為了放置工具類//這個工具類是Hibernate的工具類public static final Configuration cfg;public static final SessionFactory sf;//創建一個SessionFactory對象:類似于JDBC中連接池static{//加載Hibernate核心配置文件cfg=new Configuration().configure();sf=cfg.buildSessionFactory();//此處相當于創建工廠 }//對外設置一個接口public static Session GetSession(){//通過SessionFactory獲取到Session對象,類似于JDBC中的Connectionreturn sf.openSession();} }