標籤: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學習一