1. 环境配置

1.1 hiberante环境配置

hibernate可实现面向对象的数据存储。hibernate的官网:http://hibernate.org/ 官网上选择hibernate ORM,可以下载最新的hibernate,还有配套的document教程 http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html_single/ 。下载到的hibernate文件夹中有document文档(hibernate\documentation\manual\en-US\html_single)。发现hibernate4.3.1的文件夹中的文档和网站上提供的答案不太一样,官网的文档更详细一些,还附有toturial的源代码。

打开eclipse->windows->preferences->java->build path->user libraries,点击new,新建一个library,可取名为hibernate。点击Add JARs,选择hibernate->lib->required中的所有jar文件,另外还需要加上数据库的connector文件。因为使用的是mysql,所以我这里用到的mysql-connector-java的jar文件。这个jar文件从哪里得到呢?安装mysql服务器产生的文件夹里面是没有jar文件的。我们需要另下载一个MySQL的JDBC jar包。可以从mysql的官网上下载:http://dev.mysql.com/downloads/connector/j/ 下载后得到一个msi文件,双击及可安装。安装后,默认会产生文件夹C:\Program Files (x86)\MySQL\MySQL Connector J ,这里就有一个mysql-connector-java-x.x.x-bin.jar包了。

1.2 mysql数据库配置

安装mysql服务器,设置root的密码为root。启动mysql服务器,新建数据库hibernate,并新建表student。

1 # mysql -uroot -proot
2 > create database hiberante;
3 > use hibernate;
4 > create table student(id int primary key, name varchar(20), age int);
5 > quit;

2. 实例代码

新建一个java工程,假设取名为HibernateHelloWorld。在src下新那一个package,可取名为com.sun.hibernate.model

2.1 类代码

新建一个简单的类,放在com.sun.hibernate.model包下。内容如下:

 1 package com.sun.hibernate.model;
 2  
 3 public class Student {
 4     private int id;
 5     private String name;
 6     private int age;
 7  
 8     public int getId() {
 9         return id;
10     }
11     public void setId(int id) {
12         this.id = id;
13     }
14     public String getName() {
15         return name;
16     }
17     public void setName(String name) {
18         this.name = name;
19     }
20     public int getAge() {
21         return age;
22     }
23     public void setAge(int age) {
24         this.age = age;
25     }
26  
27 }

2.2 配置hibernate配置文件

hibernate\projec\etc中的hiberante.cfg.xml可以作为hibernate的配置文档,或者可使用hibernate\documentation\manual\en-US\html_single\index.html作为模板。在src文件夹下新建一个文件,并命名为hibernate.cfg.xml。(不可命名为其他文件名)最基础的配置文件可参考如下:

 1 <?xml version='1.0' encoding='utf-8'?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 5      
 6 <hibernate-configuration>
 7     <session-factory>
 8      
 9     <!-- Database connection settings -->
10     <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
11     <property name="connection.url">jdbc:mysql://localhost/hibernate</property>
12     <property name="connection.username">root</property>
13     <property name="connection.password">root</property>
14          
15     <!-- JDBC connection pool (use the built-in) -->
16     <!-- <property name="connection.pool_size">1</property> -->
17          
18     <!-- SQL dialect -->
19     <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
20          
21     <!-- Echo all executed SQL to stdout -->
22     <property name="show_sql">true</property>
23          
24     <!-- Enable Hibernate's automatic session context management -->
25     <!--<property name="current_session_context_class">thread</property>-->
26          
27     <!-- Drop and re-create the database schema on startup -->
28     <!-- <property name="hbm2ddl.auto">create</property> -->
29          
30     <!-- Disable the second-level cache -->
31     <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
32      
33     <mapping resource="com/sun/hibernate/model/Student.hbm.xml"/>
34          
35     </session-factory>
36 </hibernate-configuration>

mapping resource处的值可根据包名和类名做修改。若类名为Student,则此处的类配置文件必为:Student.hbm.xml

2.3 类mapping文件

新建一个文件,命名为Student.hbm.xml,放在com.sun.hibernate.model包下。内容如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5  
 6 <hibernate-mapping package="com.sun.hibernate.model">
 7     <class name="Student">
 8         <id name="id"></id>
 9         <property name="name"></property>
10         <property name="age"></property>
11     </class>
12 </hibernate-mapping>
Student.hbm.xml

相关文章: