hibernate框架搭建步驟
阿新 • • 發佈:2018-11-29
hibernate框架搭建步驟
1.建立 web project
2.匯入所需要的包antlr-2.7.7.jar
dom4j-1.6.1.jar
geronimo-jta_1.1_spec-1.1.1.jar
hibernate-commons-annotations-5.0.1.Final.jar
hibernate-core-5.0.7.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
jandex-2.0.0.Final.jar
javassist-3.18.1-GA.jar
jboss-logging-3.3.0.Final.jar
//連線資料庫驅動包
mysql-connector-java-5.1.7-bin.jar
3.建立資料庫 準備表和實體
CREATE DATABASE customer_management
CREATE TABLE `cst_customer` ( `cust_id` BIGINT(32) NOT NULL AUTO_INCREMENT COMMENT '客戶編號(主鍵)', `cust_name` VARCHAR(32) NOT NULL COMMENT '客戶名稱(公司名稱)', `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=1 DEFAULT CHARSET=utf8;
public class Customer { private Long cust_id; private String cust_name; private String cust_source; private String cust_industry; private String cust_level; private String cust_linkman; private String cust_phone; private String cust_mobile; 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; } public String getCust_industry() { return cust_industry; } public void setCust_industry(String cust_industry) { this.cust_industry = cust_industry; } public String getCust_level() { return cust_level; } public void setCust_level(String cust_level) { this.cust_level = cust_level; } public String getCust_linkman() { return cust_linkman; } public void setCust_linkman(String cust_linkman) { this.cust_linkman = cust_linkman; } public String getCust_phone() { return cust_phone; } public void setCust_phone(String cust_phone) { this.cust_phone = cust_phone; } public String getCust_mobile() { return cust_mobile; } public void setCust_mobile(String cust_mobile) { this.cust_mobile = cust_mobile; }
4.1匯入約束
在hibernate-core-5.0.7.Final.jar中的org.hibernate 下的 hibernate-configuration-3.0.dtd<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
配置約束hibernate-mapping-3.0.dtd
windows--preferences--輸入cata-->XML Catalog--add--location:目錄中的hibernate-mapping-3.0.dtd檔案--
key type:選url--key:http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd
配置hibernate-configuration-3.0.dtd
windows--preferences--輸入cata-->XML Catalog--add--location:目錄中的hibernate-configuration-3.0.dtd檔案--
key type:選url--key:http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd
4.2寫對映檔案
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.itheima.domain.Customer" table="cst_customer">//類名 對應 表名
<id name="cust_id" column="cust_id">//主鍵名 對應 欄位名
<generator class="native"></generator>
</id>
<property name="cust_name" column="cust_name"></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>
5.寫主對映檔案 (連線資料庫)
<?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_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>
<!--
#hibernate.show_sql true
#hibernate.format_sql true
-->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<!--
## auto schema export
#hibernate.hbm2ddl.auto create-drop
#hibernate.hbm2ddl.auto create
#hibernate.hbm2ddl.auto update
#hibernate.hbm2ddl.auto validate
-->
<property name="hibernate.hbm2ddl.auto ">update</property>
<mapping resource="cn/itheima/domain/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
6.測試程式碼(junit)
@Test
//儲存客戶
public void fun1(){
//配置例項允許應用程式指定屬性和對映文件
Configuration conf = new Configuration().configure();
//建立Session
SessionFactory sessionFactory = conf.buildSessionFactory();
//獲得Session
Session session = sessionFactory.openSession();
//開啟事務
Transaction tx = session.beginTransaction();
//----------------------------------------------
Customer c = new Customer();
c.setCust_name("google公司");
session.save(c);//執行儲存
//----------------------------------------------
//提交事務
tx.commit();
//關閉資源
session.close();
sessionFactory.close();
}