1. 程式人生 > >Hibernate基礎入門

Hibernate基礎入門

加載 bean ddl data 創建web工程 source private avi main

  Hibernate是一個開放源代碼的對象關系映射框架,它將POJO與數據庫表之間建立映射關系。是全自動的ORM框架,可以自動生成SQL語句並自動執行。它對JDBC進行了非常輕量級的封裝,使程序員可以隨心所欲地以操作對象的編程思維操作數據庫。

  ORM :對象關系模型。O指面向對象領域的JavaBean,R指關系數據庫領域的Relational(數據庫表結構),M指映射Mapping(XML的配置文件)。

技術分析之Hibernate的快速入門:

    第一步:下載Hibernate5的運行環境**

    1. 下載相應的jar包等
http://sourceforge.net/projects/hibernate/files/hibernate-orm/5.0.7.Final/hibernate-release-5.0.7.Final.zip/download

2. 解壓後對目錄結構有一定的了解

    第二步:創建表結構

    Create database hibernate_day01;
Use hibernate_day01;
CREATE TABLE `cst_customer` (
`cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT ‘客戶編號(主鍵)‘,
`cust_name` varchar(32) NOT NULL COMMENT ‘客戶名稱(公司名稱)‘,
`cust_user_id` bigint(32) DEFAULT NULL COMMENT ‘負責人id‘,
`cust_create_id` bigint(32) DEFAULT NULL COMMENT ‘創建人id‘,
`cust_source` varchar(32) DEFAULT NULL COMMENT ‘客戶信息來源‘,
`cust_industry` varchar(32) DEFAULT NULL COMMENT ‘客戶所屬行業‘,
`cust_level` varchar(32) DEFAULT NULL COMMENT ‘客戶級別‘,
`cust_linkman` varchar(64) 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=94 DEFAULT CHARSET=utf8;

    第三步:搭建Hibernate的開發環境

    1. 創建WEB工程,引入Hibernate開發所需要的jar包
* MySQL的驅動jar包
* Hibernate開發需要的jar包(資料/hibernate-release-5.0.7.Final/lib/required/所有jar包)
* 日誌jar包(資料/jar包/log4j/所有jar包)

    第四步:編寫Java實體類

    1. Customer類的代碼如下:
public class Customer {
private Long cust_id;
private String cust_name;
private Long cust_user_id;
private Long cust_create_id;
private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_linkman;
private String cust_phone;
private String cust_mobile;
// 省略get和set方法
}

   **第五步:創建類與表結構的映射**

1. 在JavaBean所在的包下創建映射的配置文件
* 默認的命名規則為:實體類名.hbm.xml
* 在xml配置文件中引入約束(引入的是hibernate3.0的dtd約束,不要引入4的約束)
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

2. 如果不能上網,編寫配置文件是沒有提示的,需要自己來配置
* 先復制http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd --> window --> preferences --> 搜索xml --> 選擇xml catalog --> 點擊add --> 現在URI --> 粘貼復制的地址 --> 選擇location,選擇本地的DTD的路徑

3. 編寫映射的配置文件
<?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.itheima.domain.Customer" table="cst_customer">
<id name="cust_id" column="cust_id">
<generator class="native"/>
</id>
<property name="cust_name" column="cust_name"/>
<property name="cust_user_id" column="cust_user_id"/>
<property name="cust_create_id" column="cust_create_id"/>
<property name="cust_source" column="cust_source"/>
<property name="cust_industry" column="cust_industry"/>
<property name="cust_level" column="cust_level"/>
<property name="cust_linkman" column="cust_linkman"/>
<property name="cust_phone" column="cust_phone"/>
<property name="cust_mobile" column="cust_mobile"/>
</class>
</hibernate-mapping>
 

    **第六步:編寫Hibernate核心的配置文件**

1. 在src目錄下,創建名稱為hibernate.cfg.xml的配置文件
2. 在XML中引入DTD約束
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

3. 打開:資料/hibernate-release-5.0.7.Final/project/etc/hibernate.properties,可以查看具體的配置信息
* 必須配置的4大參數
#hibernate.connection.driver_class com.mysql.jdbc.Driver
#hibernate.connection.url jdbc:mysql:///test
#hibernate.connection.username gavin
#hibernate.connection.password

* 數據庫的方言(必須配置的)
#hibernate.dialect org.hibernate.dialect.MySQLDialect

* 可選的配置
#hibernate.show_sql true
#hibernate.format_sql true
#hibernate.hbm2ddl.auto update

* 引入映射配置文件(一定要註意,要引入映射文件,框架需要加載映射文件)
* <mapping resource="com/itheima/domain/Customer.hbm.xml"/>

4. 具體的配置如下
<?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">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

<mapping resource="com/itheima/domain/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>

----------

**第七步:編寫Hibernate入門代碼**

1. 具體的代碼如下
/**
* 測試保存客戶
*/
@Test
public void testSave(){
// 先加載配置文件
Configuration config = new Configuration();
// 默認加載src目錄下的配置文件
config.configure();
// 創建SessionFactory對象
SessionFactory factory = config.buildSessionFactory();
// 創建session對象
Session session = factory.openSession();
// 開啟事務
Transaction tr = session.beginTransaction();
// 編寫保存代碼
Customer c = new Customer();
// c.setCust_id(cust_id); 已經自動遞增
c.setCust_name("測試名稱");
c.setCust_mobile("110");
// 保存客戶
session.save(c);
// 提交事務
tr.commit();
// 釋放資源
session.close();
factory.close();
}

Hibernate基礎入門