Hibernate框架導入

1. 創建Web項目

這裏寫圖片描述

2. 導入框架中所需要的包

這裏寫圖片描述
3. 創建數據庫、表

/*
     * 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;
     */
4. 創建實體類
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;
    }
    @Override
    public String toString() {
        return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + "]";
    }
}
5. 創建映射文件
<?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">
   <!-- 配置表與實體對象的關係 -->
   <!-- package屬性:填寫一個包名.在元素內部凡是需要書寫完整類名的屬性,可以直接寫簡答類名了. -->
<hibernate-mapping package="com.zjl.domain" >

    <class name="com.zjl.domain.Customer" table="cst_customer" >
        <id name="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>
6. 在 src 目錄下創建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>
         <!-- 數據庫url -->
        <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生成的sql語句打印到控制檯 -->
        <property name="hibernate.show_sql">true</property>
        <!-- 將hibernate生成的sql語句格式化(語法縮進) -->
        <property name="hibernate.format_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!-- 引入orm元數據
            路徑書寫: 填寫src下的路徑
         -->
        <mapping resource="com/zjl/domain/Customer.hbm.xml" />

    </session-factory>
</hibernate-configuration>
7.  編寫測試類
public class DBTester {

    @Test
    public void test01() {

        Configuration conf = new Configuration();
        // 默認加載 src 目錄下的hibernate.cfg.xml文件
        conf.configure();
        SessionFactory sessionFactory = conf.buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();

        Customer c = new Customer();
        c.setCust_name("Ali");
        session.save(c);

        transaction.commit();
        session.close();

    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章