我學Hibernate - 01 (最基本的xml映射配置)

到官網下載JAR包: https://www.hibernate.org/

引入主要JAR包如下:
required

並加入依賴包:slf4j-nop-1.7.21.jar

加入mysql驅動包:mysql-connector-java-5.1.26-bin.jar


創建Hibernate配置文件
注意:約定命名爲hibernate.cfg.xml,保存到classpath根目錄;

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

        <!-- Assume test is the database name -->
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/fy_work</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">ndxfke</property>

        <!-- JDBC connection pool 連接池-->
        <property name="connection.pool_size">10</property>

        <!-- 當前session的上下文,選項有: jta | thread(當前線程,常用) | managed(手工操控session) | custorm.Class(自已寫類來操作) -->
        <property name="current_session_context_class">thread</property>

        <!-- 處理二級緩存 -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <property name="show_sql">true</property>

        <property name="hbm2ddl.auto">update</property>

        <!-- 類與數據表映射關係配置 -->
        <mapping resource="com/zeke/model/Student.hbm.xml"></mapping>

    </session-factory>
</hibernate-configuration>

創建一個Student.java類;

/**
 * 
 */
package com.zeke.model;

/**
 * @author zhong
 *
 */
public class Student {


    private int id;
    private String name;
    private String card;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getCard() {
        return card;
    }
    public void setCard(String card) {
        this.card = card;
    }





}

創建java類與數據庫關聯的配置文件:Student.hbm.xml
建議:直接保存到與類同級package,便於維護和查找

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.zeke.model">
    <class name="Student" table="STUDENTS">
        <meta attribute="class-description">This class contains the employee detail.</meta>
        <!-- 定義主鍵字段 -->
        <id name="id" column="id_" type="int">
            <!-- <generator class="native" /> -->
        </id>

        <!-- 其它變通字段 -->
        <property name="name" column="name_" type="string" />
        <property name="card" column="card_" type="string" />
    </class>
</hibernate-mapping>

注意:type=“string” ,不能寫成String;只能寫成小寫的string或java.lang.String


創建一個普通的測試類:StudentTest.java

/**
 * 
 */
package com.zeke.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.zeke.model.Student;

/**
 * @author zhong
 *
 */
public class StudentTest {

    public static void main(String[] args) {
        Student s = new Student();
        s.setId(1);
        s.setName("zeke");
        s.setCard("003607");

        Configuration cfg = new Configuration();
        SessionFactory sf = cfg.configure().buildSessionFactory(); //默認加載classPath下的hibernate.cfg.xml

        Session session = sf.openSession();

        //事務開始
        session.beginTransaction();

        session.save(s);

        //提交事務
        session.getTransaction().commit();

        //關閉session
        session.close();

        sf.close();


    }

}

手動創建數據庫及數據表:
1.數據庫名:fy_work;
2.數據表:students
3.新建字段id_、name_、card_


OK,如果你沒有敲錯的話,即可通過測試把student的值保存到數據庫了。

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