准备阶段:

Hibernate可用于各种类型的项目,要在项目中使用Hibernate,需要下载Hibernate的API.
通过http://www.hibernate.org/网址可以下载到Hibernate的API和一些帮助文档.

Hibernate入门心得_Hibernate入门

点击downloads —> release bundles即可下载

下载解压后,在lib -> required目录下可找到项目中需要的jar包(API).

官网上好像没有API的帮助文档,大家可以到这里下载:http://115.com/file/bensl2vy#hibernate api帮助文档.chm

 

开发阶段:

新建一个JAVA项目,目录结构如下:

Hibernate入门心得_Hibernate入门_02

将准备的jar包引入到项目中。

 

hibernate.cfg.xml:(Hibernate的配置文件,放在项目的src目录下):

<?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">

 

     <!-- Generated by MyEclipse Hibernate Tools.                   -->

 <hibernate-configuration>

 

     <session-factory>

         <!-- 设置驱动类 -->

         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>

         <!-- 设置连接池默认个数 -->

         <property name="connection.pool_size">20</property>

         <!-- 设置JDBC URL -->

         <property name="connection.url">jdbc:mysql://localhost:3306/book</property>

         <!-- 设置数据库用户名 -->

         <property name="connection.username">username</property>

         <!-- 设置数据库用户密码 -->

         <property name="connection.password">passwrod</property>

         <!-- 设置SQL方言 -->

         <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

         <!-- 设置是否显示SQL语句 -->

         <property name="show_sql">true</property>

         <!-- 设置格式化SQL语句 -->

         <property name="format_sql">true</property>

         <property name="current_session_context_class">thread</property>

 

         <mapping resource="com/sunflower/bean/Person.hbm.xml" />

     </session-factory>

 

 </hibernate-configuration>

这里配置的是MySQL数据库的驱动和连接,在开始的时候忘了加26行,结果调试的时候出错,得不到session,关于这个标签的使用,请参考如下文章:http://blog.sina.com.cn/s/blog_5ecfe46a0100e467.html 第28行是对象关系映射文件的配置.

Person.java:(人的实体类,对应到关系数据库中的blog表)

package com.sunflower.bean;

 

 import java.io.Serializable;

 

 /**

  * 博客用户的实体类

  * 

  * @author hanyuan

  * @time 2012-7-8 下午11:53:23

  */

 public class Person implements Serializable {

     private Integer id;

     private String username;

     private String password;

     private String caption;

     private String content;

 

     public Integer getId() {

         return id;

     }

 

     public void setId(Integer id) {

         this.id = id;

     }

 

     public String getUsername() {

         return username;

     }

 

     public void setUsername(String username) {

         this.username = username;

     }

 

     public String getPassword() {

         return password;

     }

 

     public void setPassword(String password) {

         this.password = password;

     }

 

     public String getCaption() {

         return caption;

     }

 

     public void setCaption(String caption) {

         this.caption = caption;

     }

 

     public String getContent() {

         return content;

     }

 

     public void setContent(String content) {

         this.content = content;

     }

 

 }

 第12~16行中的属性对应到数据库中的blog表的字段,其中id是主键索引.

Hibernate入门心得_Hibernate入门_03

Person.hbm.xml:(对象关系映射配置文件)

<?xml version="1.0"?>

 <!DOCTYPE hibernate-mapping PUBLIC 

     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

 

 <hibernate-mapping>

     <!-- 设置实体类与数据库中表的对应关系 -->

     <class name="com.sunflower.bean.Person" table="blog">

 

         <id name="id" type="java.lang.Integer">

             <column name="id"></column>

             <!-- 将逐渐设置为自增 -->

             <generator class="increment"></generator>

         </id>

 

         <property name="username" type="java.lang.String" not-null="true">

             <column name="username"></column>

         </property>

 

         <property name="password" type="java.lang.String">

             <column name="password"></column>

         </property>

 

         <property name="caption" type="java.lang.String">

             <column name="caption"></column>

         </property>

 

         <property name="content" type="java.lang.String">

             <column name="content"></column>

         </property>

 

     </class>

 </hibernate-mapping>

<class name="com.sunflower.bean.Person" table="blog">表示对象对应的表,<id>字段是必须要有的,表示主键。开始的时候没有加第13行,结果调试出错,需要加上<generator>标签,这里设置为自增类型.关于<generator>标签的使用,参考下列文章:http://sarin.iteye.com/blog/605712

这个配置文件的是以hbm.xml结尾的,名字建议命名为对象名.hbm.xml,并且和实体类对象放在同一个包下,这样比较好找.

Test.java:(调用Hibernate API 插入数据)

package com.sunflower.main;

 

 import org.hibernate.Session;

 import org.hibernate.SessionFactory;

 import org.hibernate.Transaction;

 import org.hibernate.cfg.Configuration;

 

 import com.sunflower.bean.Person;

 

 public class Test {

     @SuppressWarnings("deprecation")

     public static void main(String[] args) {

         // 读取Hibernate的配置信息

         Configuration config = new Configuration();

         config.configure();

 

         // 读取配置里面的SessionFactory

         SessionFactory sessionFactory = config.buildSessionFactory();

         Session session = sessionFactory.getCurrentSession();

         Transaction transaction = session.beginTransaction();

 

         Person person = new Person();

         person.setUsername("邓光桥");

         person.setPassword("123");

         person.setCaption("Hibernate");

         person.setContent("Hibernate 入门实例");

 

         // 提交事务

         session.save(person);

         transaction.commit();

         sessionFactory.close();

     }

 }

转载:http://www.cnblogs.com/hanyuan/archive/2012/07/09/Hibernate.html