作者:老康
其实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 rcc:\workspace\my1sthibernate\classesc:\workspace\my1sthibernate\libc:\workspace\my1sthibernate\lib\hibernatec:\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下。
c:\workspace\my1sthibernate\c:\workspace\my1sthibernate rcc:\workspace\my1sthibernate\classesc:\workspace\my1sthibernate\libc:\workspace\my1sthibernate\lib\hibernatec:\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名为customercreate 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() { return id; } public string getpassword() { return password; } public string getusername() { return username; } public void setid(int id) { this.id = id; } public void setpassword(string password) { this.password = password; } public void setusername(string username) { this.username = username; }} 将此类存为c:\workspace\my1sthibernate rc\customer.java文件。 编写test类import net.sf.hibernate.*;import net.sf.hibernate.cfg.*;public class test { public static void main(string[] args) { try { sessionfactory sf = new configuration().configure().buildsessionfactory(); session session = sf.opensession(); transaction tx = session.begintransaction(); for (int i = 0; i < 200; i++) { customer customer = new customer(); customer.setusername("customer" + i); customer.setpassword("customer"); session.save(customer); } tx.commit(); session.close(); } catch (hibernateexception e) { e.printstacktrace(); } }} 将此类存为c:\workspace\my1sthibernate rc\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><class name="customer" table="customer"><id name="id" column="cid"><generator class="increment" /></id><property name="username" column="username" /><property name="password" column="password" /></class></hibernate-mapping> 把此文件存为c:\workspace\my1sthibernate rc\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="." /><property name="src.dir" value="src" /><property name="lib.dir" value="lib" /><property name="build.dir" value="classes" /><path id="myclasspath"><fileset dir="${lib.dir}"><include name="**/*.jar" /></fileset><pathelement location="${build.dir}" /></path><target name="init"><mkdir dir="${build.dir}" /></target><target name="build" depends="init" description="compile the source files"><javac classpathref="myclasspath" srcdir="${src.dir}" destdir="${build.dir}" /><copy todir="${build.dir}" ><fileset dir="${src.dir}" ><exclude name="**/*.java"/></fileset></copy></target><target name="run" depends="build"><java classpathref="myclasspath" classname="test" fork="true" /></target><target name="clean"><delete includeemptydirs="true"><fileset dir="${build.dir}" /></delete></target></project> 配置hibernate描述文件 hibernate描述文件可以是一个properties或xml 文件,其中最重要的是定义数据库的连接。我这里列出的是一个xml格式的hibernate.cfg.xml描述文件。<?xml version="1.0" encoding="utf-8" ?><!doctype hibernate-configurationpublic "-//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><property name="connection.driver_class">oracle.jdbc.driver.oracledriver <!– 这里是oracle 9i的jdbc driver class名 –></property><property name="connection.url">jdbc:oracle:oci8:@hibernate_test <!– 这里是oracle的hibernate_test数据库url –> </property><property name="connection.username">你的数据库用户名</property><property name="connection.password">你的数据库密码</property><property name="dialect">net.sf.hibernate.dialect.oracle9dialect <!– 这里是oracle 9i的dialect –></property><mapping resource="customer.hbm.xml" /> <!– 指定customer的映射文件 –></session-factory></hibernate-configuration> 如果你用的不是oracle 9i,可到c:\dev\<你的hibernate包所在目录> rc\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描述文件里相应的值即可。
