工程目錄結構:
650) this.width=650;" width="344" height="392" title="hibernate工程目錄結構" style="width:344px;height:400px;" src="http://www.bkjia.com/uploads/allimg/131228/152Q45624-0.png" />
hibernate.cfg.xml設定檔:
因為我使用的mysql資料庫,在串連資料庫時,添加如下紅色標記的編碼格式,這樣往庫中添加中文資料時不會產生亂碼。
hibernate.cfg.xml檔案內容為:
<?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="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- JDBC URL -->
<property name="connection.url">jdbc:mysql://localhost/schoolmanage?characterEncoding=utf8</property>
<!-- 資料庫使用者名稱 -->
<property name="connection.username">root</property>
<!-- 資料庫密碼 -->
<property name="connection.password">admin</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">8</property>
<!-- 資料庫SQL方言,這邊設定的是MySQL -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 一次讀的資料庫記錄數
<property name="jdbc.fetch_size">50</property>
-->
<!-- 設定對資料庫進行大量刪除
<property name="jdbc.batch_size">30</property>
-->
<!-- Enable Hibernate's current session context -->
<property name="current_session_context_class">thread</property>
<!-- 二級緩衝
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
-->
<!-- 為true表示將Hibernate發送給資料庫的sql顯示出來 -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- 無表情況下自動建表
<property name="hbm2ddl.auto">create</property>
-->
<!-- 對應檔 -->
<mapping resource="com/wujing/config/GuestBook.hbm.xml"/>
</session-factory>
</hibernate-configuration>
bean類:
package com.wujing.bean;
public class GuestBook {
private Integer id;
private String name;
private String email;
private String phone;
private String title;
private String content;
private String createdTime;
public GuestBook() {
}
public GuestBook(Integer id, String name, String email, String phone,
String title, String content, String createdTime) {
this.id = id;
this.name = name;
this.email = email;
this.phone = phone;
this.title = title;
this.content = content;
this.createdTime = createdTime;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getCreatedTime() {
return createdTime;
}
public void setCreatedTime(String createdTime) {
this.createdTime = createdTime;
}
}
GuestBook.hbm.xml映射關係設定檔:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.wujing.bean.GuestBook" table="guestbook" catalog="schoolmanage">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="increment" />
</id>
<property name="name" type="java.lang.String">
<column name="name" not-null="true" />
</property>
<property name="email" type="java.lang.String">
<column name="email" />
</property>
<property name="phone" type="java.lang.String">
<column name="phone" />
</property>
<property name="title" type="java.lang.String">
<column name="title" />
</property>
<property name="content" type="java.lang.String">
<column name="content" />
</property>
<property name="createdTime" type="java.lang.String">
<column name="created_time" />
</property>
</class>
</hibernate-mapping>
TestGuestBook 測試類別:
package com.test.bean;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.wujing.bean.GuestBook;
public class TestGuestBook {
public static void main(String[] args) {
Session session = new Configuration().configure().buildSessionFactory().getCurrentSession();
GuestBook gb = new GuestBook();
gb.setName("雷鋒3");
gb.setContent("好小夥!");
GuestBook gb2 = new GuestBook();
Transaction tx = session.beginTransaction();
try {
session.save(gb);
gb2 = (GuestBook) session.get(GuestBook.class, new Integer(1));
session.delete(gb2);
tx.commit();
} catch (Exception e) {
tx.rollback();
}
}
}
本文出自 “奮鬥的IT小北漂” 部落格,請務必保留此出處http://wuguojun.blog.51cto.com/2311759/1291018