工程目录结构:
hibernate.cfg.xml配置文件:
因为我使用的mysql数据库,在连接数据库时,添加如下红色标记的编码格式,这样往库中添加中文数据时不会产生乱码。
<?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="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/schoolmanage?characterEncoding=utf8</property>
<property name="connection.username">root</property>
<property name="connection.password">admin</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="current_session_context_class">thread</property>
<mapping resource="com/wujing/config/Guestbook.hbm.xml" />
</session-factory>
</hibernate-configuration>
GuestBook.java 实体类:
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"?> TestGuestBook.java 测试类: package com.test.bean; import org.hibernate.Session; import com.wujing.bean.GuestBook; public class TestGuestBook { 因为数据库比较简单,建表语句就没有往出贴了。
<!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>
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
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();
}
}
}