標籤:setter 寫入 客戶 img www aik tor long 程式員
一、Hibernate簡介
hibernate是一個開源的,輕量級的,持久成ORM架構。
Hibernate是一個開放原始碼的對象關係映射架構,它對JDBC進行了非常輕量級的對象封裝,它將POJO與資料庫表建立映射關係,是一個全自動的 orm架構,hibernate可以自動產生SQL語句,自動執行,使得Java程式員可以隨心所欲的使用對象編程思維來操縱資料庫。 Hibernate可以應用在任何使用JDBC的場合,既可以在Java的用戶端程式使用,也可以在Servlet/JSP的Web應用中使用,最具革命 意義的是,Hibernate可以在應用EJB的J2EE架構中取代CMP,完成資料持久化的重任。
POJO(Plain Ordinary Java Object)簡單的Java對象,實際就是普通JavaBeans,是為了避免和EJB混淆所創造的簡稱。使用POJO名稱是為了避免和EJB混淆起來, 而且簡稱比較直接. 其中有一些屬性及其getter setter方法的類,沒有商務邏輯,有時可以作為VO(value -object)或dto(Data Transform Object)來使用.當然,如果你有一個簡單的運算屬性也是可以的,但不允許有業務方法,也不能攜帶有connection之類的方法。二、實體類和設定檔2.1實體類
public class Customer {
private Long cust_id;
private String cust_name;
private String cust_source;
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;
}
}
2.2實體類Customer對應的Customer.hbm.xml映射設定檔
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<?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>
<!-- ORM:Object Relational Mapping,將實體類O和資料庫的表R 建立映射關係 -->
<class name="com.itheima.hibernate.domain.Customer" table="cst_customer">
<!-- 類中的屬性與表中的主鍵對應 -->
<!--
id標籤中的屬性:name,column, length, type
name:實體類中oid的名稱,(必須)
column:資料庫表中主鍵欄位;(非必須, 實體類中oid與表中主鍵欄位名一致時, 可以省略column屬性)
length:欄位長度, hibernate自動建表時用到(非必須);
type:資料類型(非必須)
java的資料類型 ----> type="java.lang.String"
hibernate的資料類型 ----> type="String"
sql的資料類型 ----> type="varchar(32)"
-->
<id name="cust_id" column="cust_id">
<!--hibernate中的主鍵建置原則:increment, identity, sequence, native, uuid, assigned
-->
<generator class="native"/>
</id>
<!-- 類中的屬性與表中的欄位對應 -->
<!--如果類中屬性名稱, 與資料庫中標的欄位名相同時, column可以省略-->
<!--
property標籤中欄位名與id標籤中的一樣:name(必須),column(非必須), length(非必須), type(非必須)
-->
<property name="cust_name" column="cust_name"/>
<property name="cust_source" column="cust_source"/>
</class>
</hibernate-mapping>
三、hibernate的核心設定檔hibernate.cfg.xml
(在ssh整合的時候,hibernate的核心設定檔可以寫入spring的設定檔中)
<?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_day01</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123</property>
<!-- 資料庫的方言:根據底層的資料庫產生不同的SQL -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 配置顯示SQL -->
<property name="hibernate.show_sql">true</property>
<!-- 配置格式化SQL -->
<property name="hibernate.format_sql">true</property>
<!-- 配置hbm2ddl -->
<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/itheima/hibernate/domain/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Hibernate入門----設定檔