一个简单的例子。适合入门者参考。其实hibernate本身是个独立的框架,它不需要任何web server或application server的支持。然而,大多数的hibernate入门介绍都加入了很多非hibernate的东西,比如: tomcat, eclipse, log4j,struts, xdoclet, 甚至jboss。这容易让人产生hibernate复杂难懂的误解,特别是打击了初学者的积极性。
在这篇文章将不涉及eclipse, log4j, struts, tomcat, xdoclet,和jboss。本文的目的是演示一下hibernate的安装过程以及最基本的功能,从而给初学者一个低得不能再低的入门门槛。
下载文件
你需要java sdk、 hibernate包、ant包、和jdbc driver。
1、hibernate包下载地址:
http://prdownloads.sourceforge.net/hibernate/?sort_by=date&sort=desc
2、ant包下载地址:
http://apache.130th.net/ant/binaries/apache-ant-1.6.1-bin.zip
3、jdbc driver要根据你用的database来定,一般database官方网站上都会有。hibernate支持常用的database,比如 mysql, oracle, postgresql, 和ms-sql server。这些数据库都有jdbc driver:
oracle jdbc driver下载地址(下载前必须同意oracle协议书)
http://otn.oracle.com/software/htdocs/distlic.html?/software/tech/java/sqlj_jdbc/htdocs/jdbc9201.html
mysql jdbc driver下载地址
http://dev.mysql.com/downloads/connector/j/3.0.html
postgresql jdbc driver下载地址
http://jdbc.postgresql.org/download.html
ms-sql server jdbc driver下载地址
http://www.microsoft.com/downloads/details.aspx?familyid=9f1874b6-f8e1-4bd6-947c-0fc5bf05bf71&displaylang=en
4、将hibernate包和ant包分别解压至c:\dev\下(此目录不重要,你可以换其它任何目录)。
配置环境
1、你需要添加一个新的环境变量: ant_home,让它指向c:\dev\<你的ant包所在目录>。并在path环境变量里添加%ant_home%\bin。
2、你需要添加一个新的环境变量: java_home,让它指向你的j2sdk根目录。并在path环境变量里添加%java_home%\bin。
3、创建一个项目目录,比如c:\workspace\my1sthibernate。
在项目目录下,另外创建三个目录: src, classes, lib。
在lib目录下,创建两个目录: hibernate和db。
这样你有了如下的文件结构:
c:\workspace\my1sthibernate\ c:\workspace\my1sthibernate\src c:\workspace\my1sthibernate\classes c:\workspace\my1sthibernate\lib c:\workspace\my1sthibernate\lib\hibernate c:\workspace\my1sthibernate\lib\db |
4、将c:\dev\<你的hibernate包所在目录>\hibernate2.jar文件copy到c:\workspace\my1sthibernate\lib\hibernate下。
将c:\dev\<你的hibernate包所在目录>\lib\下的所有文件同样copy到c:\workspace\my1sthibernate\lib\hibernate下。
将你的jdbc driver文件(一般是一个jar文件)copy到c:\workspace\my1sthibernate\lib\db下。
创建数据库
1、用你最喜爱的database软件,创建一个hibernate_test的数据库。
2、在此数据库下,新建一个table名为customer
create table customer ( cid integer not null primary key, username varchar(12) not null, password varchar(12) ); |
编写java文件
public class customer { private int id; private string username; private string password; public int getid() { public string getpassword() { public string getusername() { public void setid(int id) { public void setpassword(string password) { public void setusername(string username) { } |
将此类存为c:\workspace\my1sthibernate\src\customer.java文件。
编写test类
import net.sf.hibernate.*; import net.sf.hibernate.cfg.*; public class test { public static void main(string[] args) { for (int i = 0; i < 200; i++) { tx.commit(); |
将此类存为c:\workspace\my1sthibernate\src\test.java文件。
创建hibernate映射文件
因为这里只有一个class — customer 和一个table — customer,你只需要建立一个映射文件— customer.hbm.xml,来对应customer类和customer表之间的关系。
<?xml version=”1.0″?> <!doctype hibernate-mapping public “-//hibernate/hibernate mapping dtd//en” “http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd”> <hibernate-mapping> |
把此文件存为c:\workspace\my1sthibernate\src\customer.hbm.xml,和customer.java放在同一目录下。
编写ant build.xml文件
你不一定要知道这个build.xml的细节,其实ant也不是hibernate所必须的。这里用ant是为了简化一些任务,比如: 编译、copy、运行,等。
<?xml version=”1.0″ ?>
<project name=”my1sthibernate” default=”build” basedir=”.”> <property name=”base.dir” value=”.” /> <path id=”myclasspath”> <target name=”init”> <target name=”build” depends=”init” description=”compile the source files”> <target name=”run” depends=”build”> <target name=”clean”> </project> |
配置hibernate描述文件
hibernate描述文件可以是一个properties或xml 文件,其中最重要的是定义数据库的连接。我这里列出的是一个xml格式的hibernate.cfg.xml描述文件。
<?xml version=”1.0″ encoding=”utf-8″ ?> <!doctype hibernate-configuration public “-//hibernate/hibernate configuration dtd//en” “http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd”> <hibernate-configuration> <session-factory name=”java:/hibernate/hibernatefactory”> <property name=”show_sql”>true</property> <mapping resource=”customer.hbm.xml” /> <!– 指定customer的映射文件 –> </session-factory> </hibernate-configuration> |
如果你用的不是oracle 9i,可到c:\dev\<你的hibernate包所在目录>\src\hibernate.properties文件里找到你的数据库,然后替换以上相对应的值。
开始运行
到c:\workspace\my1sthibernate下,运行ant run。如果你严格依照以上步骤,应该看到
run: [java] log4j:warn no appenders could be found for logger (net.sf.hibernate.cfg.environment). [java] log4j:warn please initialize the log4j system properly. [java] hibernate: insert into customer (username, password, cid) values (?, ?, ?) build successful |
到你的hibernate_test数据库看一下,在custmor表里新添了200条记录,但你没有写任何jdbc code。
以后如果你要更换数据库,只需要改变hibernate.cfg.xml描述文件里相应的值即可。