什麼是框架、hibernate框架介紹、hibernate實現增刪改查

目錄

什麼是框架

hibernate框架是什麼

hibernate是一款orm(object relationg mapping對象關係映射)框架

列:書寫第一個hibernate項目實現增刪改查

導包

創建po對象與寫orm元數據(對象與表的映射配置文件)

主配置文件hibernate.cfg.xml

測試方法1(不推薦使用)

測試方法2(推薦)


什麼是框架

  1. 框架是用來提高開發效率
  2. 框架封裝了一些功能,使用這些功能時,直接調用就可,不需要在手動實現
  3. 框架是一個半成品項目,只需要懂得如何駕馭這些功能就好

hibernate框架是什麼

hibernate框架完成數據庫(dao層)的操作

spring框架貫穿三層

hibernate好處:操作數據庫時可以直接面向對象的方式完成,不需要直接操作數據庫

hibernate是一款orm(object relationg mapping對象關係映射)框架

orm分四級

  • hibernate屬於4級:完全面向對象
  • mybatis屬於2級
  • DButils屬於1級

列:書寫第一個hibernate項目實現增刪改查

導包

創建po對象與寫orm元數據(對象與表的映射配置文件)

po對象Customer.java

orm元數據 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">

<!-- orm配置文件:配置表與實體對象的關係 -->
<hibernate-mapping package="com.lws.po">
	<class name="Customer" table="cst_customer">
		<id name="cust_id">
			<generator class="native"></generator> <!-- generator:主鍵生成策略(明天講) -->
		</id>
		
		<!-- property元素:除id之外的普通屬性映射
			name: 填寫屬性名
			column(可選): 填寫列名
			type(可選):填寫列(屬性)的類型.hibernate會自動檢測實體的屬性類型.
					每個類型有三種填法: java類型|hibernate類型|數據庫類型
			not-null(可選):配置該屬性(列)是否不能爲空. 默認值:false
			length(可選):配置數據庫中列的長度. 默認值:使用數據庫類型的最大長度
	    -->
		<property name="cust_name" column="cust_name">
			<!-- <column name="cust_name" sql-type="varchar" ></column> -->
		</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_linkman" column="cust_linkman"></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-configuration>


	<session-factory>
	   	<!-- 
		#hibernate.dialect org.hibernate.dialect.MySQLDialect
		#hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
		#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
		#hibernate.connection.driver_class com.mysql.jdbc.Driver
		#hibernate.connection.url jdbc:mysql:///test
		#hibernate.connection.username gavin
		#hibernate.connection.password
		 -->
		 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		 <property name="hibernate.connection.url">jdbc:mysql:///hibernate_01?characterEncoding=UTF-8</property>
		 <property name="hibernate.connection.username">root</property>
		 <property name="hibernate.connection.password">123</property>
		 
		 
		 <!-- 數據庫方言
			不同的數據庫中,sql語法略有區別. 指定方言可以讓hibernate框架在生成sql語句時.針對數據庫的方言生成.
			sql99標準: DDL 定義語言  庫表的增刪改查
					  DCL 控制語言  事務 權限
					  DML 操縱語言  增刪改查
			注意: MYSQL在選擇方言時,請選擇最短的方言.
		 -->
		 <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		 
		 
		<!-- #hibernate.show_sql true 
			 #hibernate.format_sql true
		-->
		<!-- 將hibernate生成的sql語句打印到控制檯 -->
		<property name="hibernate.show_sql">true</property>
		<!-- 將hibernate生成的sql語句格式化(語法縮進) -->
		<property name="hibernate.format_sql">true</property>
		
		<!-- 
		## auto schema export  自動導出表結構. 自動建表
		#hibernate.hbm2ddl.auto create		自動建表.每次框架運行都會創建新的表.以前表將會被覆蓋,表數據會丟失.(開發環境中測試使用)
		#hibernate.hbm2ddl.auto create-drop 自動建表.每次框架運行結束都會將所有表刪除.(開發環境中測試使用)
		#hibernate.hbm2ddl.auto update(推薦使用) 自動生成表.如果已經存在不會再生成.如果表有變動.自動更新表(不會刪除任何數據).
		#hibernate.hbm2ddl.auto validate	校驗.不自動生成表.每次啓動會校驗數據庫中表是否正確.校驗失敗.
		 -->
		<property name="hibernate.hbm2ddl.auto">update</property>
		<!-- 引入orm元數據
			路徑書寫: 填寫src下的路徑
		 -->
		<mapping resource="com/lws/po/Customer.hbm.xml" />
	</session-factory>

</hibernate-configuration>

測試方法1(不推薦使用)

//測試Hibernate框架
public class Test {

	@org.junit.Test
	//保存客戶
	public void addCustomer(){
		//1創建Configuration並加載主配置文件(加載src下的hibernate.cfg.xml文件)
		Configuration configuration = new Configuration().configure();
		
		//2 讀取指定orm元數據(擴展),如果主配置中已經引入映射配置.不需要手動加載
		//conf.addResource(resourceName);
		//conf.addClass(persistentClass);
		
		SessionFactory sessionFactory = configuration.buildSessionFactory();//3 根據配置信息,創建 SessionFactory對象
		
		Session session = sessionFactory.openSession();//4 獲得session
		Transaction transaction = session.beginTransaction();//5 開啓事務並獲得操作事務的tx對象(建議使用)
		//----------------------------------------------
		Customer c = new Customer();
		c.setCust_name("google1公司");
		session.save(c);//執行保存
		//----------------------------------------------
		transaction.commit();//提交事務
		session.close();//釋放資源
		sessionFactory.close();//釋放資源
	}
	
	/**
	 * 查詢數據
	 */
	@org.junit.Test
	public void queryById() {
		//1創建並加載配置文件(加載src下的hibernate.cfg.xml文件)
		Configuration configuration=new Configuration().configure();
		//2獲取SessionFactory
		SessionFactory sessionFactory=configuration.buildSessionFactory();
		//3打開session
		Session session=sessionFactory.openSession();
		//4開啓事務
		Transaction transaction = session.beginTransaction();
		//查詢------------------------------------------------
		Customer customer= session.get(Customer.class, 1L);
		System.out.println(customer);
		//-------------------------------------
		transaction.commit();//提交事務
		session.close();//關閉session
		sessionFactory.close();//關閉sessionFactory
		
	}
	/**
	 * 修改數據
	 */
	@org.junit.Test
	public void update() {
		//1創建configuration 並加載配置文件(加載src下的hibernate.cfg.xml文件)
		Configuration configuration=new Configuration().configure();
		//2獲取sessionFactory
		SessionFactory sessionFactory=configuration.buildSessionFactory();
		//3打開session
		Session session = sessionFactory.openSession();
		//4開啓事務
		Transaction transaction=session.beginTransaction();
		//修改---------------------------------------
		//獲取修改的對象
		Customer customer=session.get(Customer.class, 2L);
		customer.setCust_name("laowang");
		session.update(customer);
		//------------------------
		transaction.commit();//提交事務
		session.close();//關閉session
		sessionFactory.close();//關閉sessionFarctory
	}
    /**
    * 刪除數據
    */
	
	@org.junit.Test
	public void del() {
		//1創建configuration 並加載配置文件(加載src下的hibernate.cfg.xml文件)
		Configuration configuration=new Configuration().configure();
		//2獲取sessionFactory
		SessionFactory sessionFactory=configuration.buildSessionFactory();
		//3打開session
		Session session = sessionFactory.openSession();
		//4開啓事務
		Transaction transaction=session.beginTransaction();
		//刪除---------------------------------------
		//獲取要刪除的對象
		Customer customer=session.get(Customer.class, 1L);
		session.delete(customer);
		//------------------------
		transaction.commit();//提交事務
		session.close();//關閉session
		sessionFactory.close();//關閉sessionFarctory
	}
	
}

測試方法可以提取重複代碼

測試方法2(推薦)

HibernateUtils.java

public class HibernateUtils {
	private static SessionFactory sessionFactory;
	static {
		// 1創建configuration並獲取配置文件()
		Configuration configuration = new Configuration().configure();
		// 2創建sessionFactory
		sessionFactory = configuration.buildSessionFactory();
	}

	/**
	 * 獲取session對象=>全新的session
	 */
	public static Session openSession() {
		Session session = sessionFactory.openSession();
		return session;

	}

	/**
	 * 獲取session=>獲取與線程綁定的session
	 */
	public static Session getCurrentSession() {
		Session session = sessionFactory.getCurrentSession();
		return session;
	}
}

Test.java

//測試Hibernate框架
public class Test {

	@org.junit.Test
	//保存客戶
	public void addCustomer(){
		//1創建Configuration並加載主配置文件(加載src下的hibernate.cfg.xml文件)
		Configuration configuration = new Configuration().configure();
		
		//2 讀取指定orm元數據(擴展),如果主配置中已經引入映射配置.不需要手動加載
		//conf.addResource(resourceName);
		//conf.addClass(persistentClass);
		
		SessionFactory sessionFactory = configuration.buildSessionFactory();//3 根據配置信息,創建 SessionFactory對象
		
		Session session = sessionFactory.openSession();//4 獲得session
		Transaction transaction = session.beginTransaction();//5 開啓事務並獲得操作事務的tx對象(建議使用)
		//----------------------------------------------
		Customer c = new Customer();
		c.setCust_name("google1公司");
		session.save(c);//執行保存
		//----------------------------------------------
		transaction.commit();//提交事務
		session.close();//釋放資源
		sessionFactory.close();//釋放資源
	}
	
	/**
	 * 查詢數據
	 */
	@org.junit.Test
	public void queryById() {
		//1創建並加載配置文件(加載src下的hibernate.cfg.xml文件)
		Configuration configuration=new Configuration().configure();
		//2獲取SessionFactory
		SessionFactory sessionFactory=configuration.buildSessionFactory();
		//3打開session
		Session session=sessionFactory.openSession();
		//4開啓事務
		Transaction transaction = session.beginTransaction();
		//查詢------------------------------------------------
		Customer customer= session.get(Customer.class, 1L);
		System.out.println(customer);
		//-------------------------------------
		transaction.commit();//提交事務
		session.close();//關閉session
		sessionFactory.close();//關閉sessionFactory
		
	}
	/**
	 * 修改數據
	 */
	@org.junit.Test
	public void update() {
		//1創建configuration 並加載配置文件(加載src下的hibernate.cfg.xml文件)
		Configuration configuration=new Configuration().configure();
		//2獲取sessionFactory
		SessionFactory sessionFactory=configuration.buildSessionFactory();
		//3打開session
		Session session = sessionFactory.openSession();
		//4開啓事務
		Transaction transaction=session.beginTransaction();
		//修改---------------------------------------
		//獲取修改的對象
		Customer customer=session.get(Customer.class, 2L);
		customer.setCust_name("laowang");
		session.update(customer);
		//------------------------
		transaction.commit();//提交事務
		session.close();//關閉session
		sessionFactory.close();//關閉sessionFarctory
	}
    /**
    * 刪除數據
    */
	
	@org.junit.Test
	public void del() {
		//1創建configuration 並加載配置文件(加載src下的hibernate.cfg.xml文件)
		Configuration configuration=new Configuration().configure();
		//2獲取sessionFactory
		SessionFactory sessionFactory=configuration.buildSessionFactory();
		//3打開session
		Session session = sessionFactory.openSession();
		//4開啓事務
		Transaction transaction=session.beginTransaction();
		//刪除---------------------------------------
		//獲取要刪除的對象
		Customer customer=session.get(Customer.class, 1L);
		session.delete(customer);
		//------------------------
		transaction.commit();//提交事務
		session.close();//關閉session
		sessionFactory.close();//關閉sessionFarctory
	}
	
}

 

 

 

 

發佈了137 篇原創文章 · 獲贊 25 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章