1、 什麼是Hibernate
就是一個持久層ORM的框架
2、 什麼是ORM :Object Relation Mapping
概述:ORM對象關係映射,將Java中的類和數據庫中的表建立一種映射的關係,操作對象就可以操作數據庫中的表。
Hibernate的入門案例
1、 引入jar包
2、 創建實體映射文件
(1)引入Hibernate的映射約束
<?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.lwt.hibernate.domain.Customer" table="cst_customer"> <!-- 建立屬性與表中字段的映射 --> <!-- 建立OID與表中主鍵映射 --> <id name="cust_id" column="cust_id"> <generator class="native"/> </id> <!-- 普通屬性與表中字段建立映射 --> <property name="cust_name" column="cust_name"/> <property name="cust_source" column="cust_source"/> <property name="cust_industry" column="cust_industry"/> <property name="cust_level" column="cust_level"/> <property name="cust_phone" column="cust_phone"/> <property name="cust_mobile" column="cust_mobile"/> </class> </hibernate-mapping>
|
(2)編寫Customer.hbm.xml
壓縮包中:project/etc/
3、 Hibernate的核心配置文件
<?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_01_36</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123</property>
<!-- 配置Hibernate的屬性 -->
<!-- Hibernate的方言: -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 配置Hibernate的其他的屬性 -->
<!-- 顯示SQL -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化SQL -->
<property name="hibernate.format_sql">true</property>
<!--
hbm2ddl.auto :
* create :如果數據庫中有表,將該表刪除,重新創建一個新的表,如果沒有表,創建新表。
* create-drop :如果數據庫中有表,將表刪除,重新創建,使用完畢會刪除該表。如果沒有表,創建新表,用完刪除掉。
* update :如果有表,使用原有表,如果沒有表,創建新表。而且更新表結構。
* validate :如果沒有表報錯,如果有表,使用原有表,對錶進行校驗。
-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 配置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>
<!-- 加載映射文件 -->
<mapping resource="com/lwt/hibernate/domain/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4、 編寫保存客戶代碼
@Test
/**
* 保存客戶
*/
public void demo1(){
// 加載核心配置文件
Configuration configuration = new Configuration().configure();
// 創建一個SessionFactory:類似於連接池.
SessionFactory sessionFactory = configuration.buildSessionFactory();
// 創建一個Session:相當於Connection.
Session session = sessionFactory.openSession();
// 開啓事務:
Transaction transaction = session.beginTransaction();
// 操作
Customer customer = new Customer();
customer.setCust_name("郝寶強");
session.save(customer);
// 提交事務:
transaction.commit();
// 釋放資源
session.close();
}