Hibernate4學習一

來源:互聯網
上載者:User

標籤:ring   png   close   ann   方言   generated   rgs   使用   student   

1.匯入所有相關jar包

https://pan.baidu.com/s/1i5OakiL

2.配置hibernate.cfg.xml檔案

<?xml version=‘1.0‘ encoding=‘utf-8‘?><!DOCTYPE hibernate-configuration PUBLIC        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration>    <session-factory>        <!-- 資料庫連接設定 -->        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>        <!-- Student為你的資料庫名稱 -->        <property name="connection.url">jdbc:mysql://localhost:3306/QQ_DB</property>        <property name="connection.username">root</property>        <property name="connection.password">root</property>        <!-- JDBC connection pool (use the built-in) -->        <property name="connection.pool_size">1</property>        <!-- SQL方言 -->        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>        <!-- Enable Hibernate‘s automatic session context management -->        <property name="current_session_context_class">thread</property>        <!-- Disable the second-level cache -->        <property name="cache.provider_class">            org.hibernate.cache.NoCacheProvider        </property>        <!-- 顯示SQL語句 -->        <property name="show_sql">true</property>        <!-- 格式化輸出SQL語句 -->        <property name="format_sql">true</property>        <!-- validate 載入hibernate時,驗證建立資料庫表結構 -->        <!-- create 每次載入hibernate,重新建立資料庫表結構,這就是導致資料庫表資料丟失的原因。 -->        <!-- create-drop 載入hibernate時建立,退出是刪除表結構 -->        <!-- update 載入hibernate自動更新資料庫結構 -->        <property name="hbm2ddl.auto">create</property>        <property name="myeclipse.connection.profile"></property>        <!-- 使用annotion註解時,hibernate與資料庫物件關係映射表示 -->        <!-- 推薦使用annotion註解來表示映射關係 -->        <mapping class="QQUser" />    </session-factory></hibernate-configuration>

3.建立映射類

import java.io.Serializable;import java.util.Date;import javax.persistence.Entity;import javax.persistence.Id;import javax.persistence.Table;@Entity@Table(name="tb_user")public class QQUser implements Serializable {    private String qq_num;    private String nick_name;    private String gender;    private Date birthday;    private int star_sit;        @Id    public String getQq_num() {        return qq_num;    }    public void setQq_num(String qq_num) {        this.qq_num = qq_num;    }    public String getNick_name() {        return nick_name;    }    public void setNick_name(String nick_name) {        this.nick_name = nick_name;    }    public String getGender() {        return gender;    }    public void setGender(String gender) {        this.gender = gender;    }    public Date getBirthday() {        return birthday;    }    public void setBirthday(Date birthday) {        this.birthday = birthday;    }    public int getStar_sit() {        return star_sit;    }    public void setStar_sit(int star_sit) {        this.star_sit = star_sit;    }            }

4.測試

import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class Test {    public static void main(String[] args) {        // TODO Auto-generated method stub             QQUser u = new QQUser();             u.setQq_num("7890654");             u.setNick_name("小明");             u.setGender("男");                         Configuration cfg = new Configuration();            // 讀取hibernate.cfg.xml中的配置            cfg.configure();            // 擷取SessionFactory            SessionFactory sf = cfg.buildSessionFactory();            // 擷取Session            Session session = sf.openSession();            // 開啟事務            session.beginTransaction();            // 儲存            session.save(u);            // 提交事務            session.getTransaction().commit();            // 關閉串連            session.close();            sf.close();            }}

Hibernate4學習一

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.