Hibernate 學習(一)
一、Hibernate 簡介
1、Hibernate 簡介
Hibernate是一個開放源代碼的對象關系映射(ORM)框架,它對JDBC進行了非常輕量級的對象封裝,它將POJO與數據庫表建立映射關系,是一個全自動的orm框架,hibernate可以自動生成SQL語句,自動執行,使得Java程序員可以隨心所欲的使用對象編程思維來操縱數據庫。Hibernate 是傳統 Java 對象和數據庫服務器之間的橋梁,用來處理基於 O/R 映射機制和模式的那些對象。
2、Hibernate 優缺點
優點與缺點都是相對的,那麽講一個東西的優缺點我們都需要一個參數對比,而咱們要來進行對比的咱們操作數據的原生的JDBC:
1、JDBC的優缺點
本質:處理Java對象和關系型數據庫表之間的轉換。
優點:
操作數據庫最底層,性能最高(需要你是有相應的經驗,並且是一個數據庫高手)。
缺點:
1、使用復雜(重復代碼太多)。
2、移植數據庫很麻煩,改動比較多;主鍵的生成方式不同(mysql使用自增,oracle使用序列);分頁的sql語句也是不同(mysql使用limit,oracle使用ROWNUM)。
3、性能優化得自己處理,沒有提供數據的緩存。
4、面向sql語句操作,不是面向對象的。
2、hibernate的優缺點
本質:處理Java對象和關系型數據庫表之間的轉換,只是對JDBC再次做了一層封裝。
優點:
1、程序員操作很簡單,代碼簡單 session.save(user);
2、直接面向對象操作。
3、提供世界級數據緩存(現在幾乎所有的ORM框架的緩存都是學的Hibernate);一級緩存,二級緩存,查詢緩存。
4、數據庫移植性很強,很少的修改;把各種數據庫抽取了一個方言接口;不同數據庫實現一個方言接口,如果換了數據庫,必須修改方言實現,驅動jar文件,連接數據庫信息。
缺點:
1、不能幹預sql語句的生成;session.get(User.class,id); 默認查詢t_user表的所有字段,自動生成select user0_.id,user0_.name,user0_.age from t_user user0_ where user0_.id=?
2、一個項目中,如果對sql語句的優化要求比較高,不適合用hibernate(不過可以使用 Hibernate 對原生sql 的支持來解決)。
3、如果一張表中有上億級別的數據量,也不適合用hibernate,其實也不適合用jdbc(可以使用數據庫讀寫分離,分庫分表方案解決)。
二、Hibernate 入門
1、步驟
1、拷貝jar包
2、建立 pojo
Product 類:
1 package com.hibernate.pojo; 2 3 /** 4 * @author zt1994 2018/3/6 14:16 5 */ 6 public class Product { 7 private Integer id; 8 private String name; 9 private float price; 10 11 public Integer getId() { 12 return id; 13 } 14 15 public void setId(Integer id) { 16 this.id = id; 17 } 18 19 public String getName() { 20 return name; 21 } 22 23 public void setName(String name) { 24 this.name = name; 25 } 26 27 public float getPrice() { 28 return price; 29 } 30 31 public void setPrice(float price) { 32 this.price = price; 33 } 34 }
3、建立映射文件
Product.hbm.xml 映射文件:
1 <!DOCTYPE hibernate-mapping PUBLIC 2 "-//Hibernate/Hibernate Mapping DTD//EN" 3 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 4 5 <!--映射文件--> 6 <hibernate-mapping package="com.hibernate.pojo"> 7 <!--class表示一個由hibernate管理的持久對象,對應數據庫中一個表--> 8 <!--table數據庫的表名--> 9 <class name="Product" table="product"> 10 <id name="id" type="int" column="id"> 11 <!--generator表示主鍵的生成方式,native自動選擇數據庫本地的策略--> 12 <generator class="native"/> 13 </id> 14 <!--非主鍵屬性--> 15 <property name="name" column="name" type="string"/> 16 <property name="price" column="price" type="float"/> 17 </class> 18 </hibernate-mapping>
4、建立hibernate核心配置文件
hibernate.cfg.xml 配置文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 6 <!--hibernate核心配置文件--> 7 <hibernate-configuration> 8 <session-factory> 9 <property name="dialect"> 10 org.hibernate.dialect.MySQLDialect 11 </property> 12 <!--鏈接池配置--> 13 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 14 <property name="connection.url">jdbc:mysql://localhost:3306/how2java</property> 15 <property name="connection.username">root</property> 16 <property name="connection.password">admin</property> 17 <!--顯示sql語句--> 18 <property name="show_sql">true</property> 19 20 <!-- 映射文件--> 21 <mapping resource="mapper/Product.hbm.xml"/> 22 </session-factory> 23 </hibernate-configuration>
5、編輯dao實現類
dao 接口:
1 package com.hibernate.dao; 2 3 import com.hibernate.pojo.Product; 4 5 import java.util.List; 6 7 public interface IProductDao { 8 9 /** 10 * 添加產品 11 * @param product 12 * @return 13 */ 14 void addProduct(Product product); 15 }
dao 實現:
1 package com.hibernate.dao.impl; 2 3 import com.hibernate.dao.IProductDao; 4 import com.hibernate.pojo.Product; 5 import org.hibernate.Query; 6 import org.hibernate.Session; 7 import org.hibernate.SessionFactory; 8 import org.hibernate.Transaction; 9 import org.hibernate.cfg.Configuration; 10 11 import java.util.List; 12 13 /** 14 * @author zt1994 2018/3/6 14:40 15 */ 16 public class ProductDaoImpl implements IProductDao { 17 18 @Override 19 public void addProduct(Product product) { 20 //1.讀取並解析配置文件 21 Configuration configuration = new Configuration(); 22 //2.加載配置文件,如果不設置加載默認配置文件hibernate.cfg.xml 23 configuration.configure("hibernate.cfg.xml"); 24 //3.生成會話工廠 25 SessionFactory sessionFactory = configuration.buildSessionFactory(); 26 //4.獲取session 27 Session session = sessionFactory.openSession(); 28 //5.開啟事務 29 Transaction transaction = session.getTransaction(); 30 transaction.begin(); 31 //6.操作CRUD 32 session.save(product); 33 transaction.commit(); 34 //7.關閉資源 35 session.close(); 36 sessionFactory.close(); 37 } 38 }
6、測試
1 package com.hibernate.test; 2 3 import com.hibernate.dao.IProductDao; 4 import com.hibernate.dao.impl.ProductDaoImpl; 5 import com.hibernate.pojo.Product; 6 import org.junit.Test; 7 8 /** 9 * @author zt1994 2018/3/6 14:47 10 */ 11 public class TestProductDao { 12 private IProductDao productDao = new ProductDaoImpl(); 13 14 /** 15 * 測試添加商品 16 */ 17 @Test 18 public void testAddProduct(){ 19 Product product = new Product(); 20 product.setName("test"); 21 product.setPrice(2222); 22 productDao.addProduct(product); 23 } 24 }
Hibernate 學習(一)