工程目录结构:

hibernate 实例一_hibernate

 

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();
  }
 }
}