Hibernate3之Hello, Word程序 这个板块太冷清了,记得范智曾经转贴过一个网上的“史上最简单的Hibernate入门”,内容却是乱起八糟。 现在Falcon要开始使用Hibernate了,我就勉为其难,出个cauchy版本的“Hibernate3之Hello, Word程序”吧! Hibernate运行时依赖于以下软件包: cglib-full-2.0.2.jar commons-collections-2.1.1.jar commons-logging-1.0.4.jar dom4j-1.5.jar ehcache-0.9.jar hibernate3.jar jta.jar 使用Hibernate首先要定义主配置文件hibernate.cfg.xml,再定义数据库映射文件*.hbm.xml,编写或产生DAO类,最后使用DAO类。 其中数据库映射文件和DAO类在实践中有3种使用方法: 1. 全部手工编写 2. 编写数据库映射文件,使用Hibernate工具,产生DAO类 3. 编写DAO类,使用类似XDoclet的标签技术,产生数据库映射文件 为了简化流程,下面以第一种方法为例说明: 1. Hibernate 配置文件 [code] < ? xml version='1.0' encoding='utf-8'?> < !DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" > <hibernate-configuration> <session-factory> <!-- <property name="connection.datasource">java:comp/env/jdbc/mydb</property> --> <property name="generate_statistics">true</property> <property name="dialect">org.hibernate.dialect.SQLServerDialect</property> <property name="connection.driver_class">com.inet.tds.TdsDriver</property> <property name="connection.pool_size">150</property> <property name="connection.url">jdbc:inetdae7:127.0.0.1:1433?database=pubs</property> <property name="connection.username">Falcon </property> <property name="connection.password">Falcon</property> <property name="show_sql">false</property> <mapping resource="cauchy/Region.hbm.xml"/> </session-factory> </hibernate-configuration> [/code] 2. 数据库映射定义 [code] < ? xml version="1.0"?> < !DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > <hibernate-mapping> <!-- table created by: CREATE TABLE REGION ( RECORD_ID VARCHAR(64) PRIMARY KEY, NAME VARCHAR(64) NOT NULL, CODE VARCHAR(12) NOT NULL ); --> <class name="cauchy.Region" table="REGION"> <id type="string" name="ID" column="RECORD_ID" length="64"> <generator class="uuid.hex"/> </id> <property type="string" name="name" column="NAME" length="64" not-null="true" unique="false"/> <property type="string" name="code" column="CODE" length="12" not-null="true" unique="true"/> </class> </hibernate-mapping> [/code] 3. 自动产生或者手工编写DAO对象 [code] package cauchy; import java.io.Serializable; public class Region implements Serializable { private String id; private String code; private String name; /** * @param id The id to set. */ public void setID(String id) { this.id = id; } /** * @return Returns the id. */ public String getID() { return id; } /** * @param code The code to set. */ public void setCode(String code) { this.code = code; } /** * @return Returns the code. */ public String getCode() { return code; } /** * @param name The name to set. */ public void setName(String name) { this.name = name; } /** * @return Returns the name. */ public String getName() { return name; } } [/code] 4. 运行测试 [code] package cauchy; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class TestRegion { public static void main(String[] args) { SessionFactory sf = new Configuration().configure().buildSessionFactory(); Session session = sf.openSession(); long t = System.currentTimeMillis(); Transaction tx = session.beginTransaction(); for(int i = 1000; i <= 9999; i++) { Region region = new Region(); region.setCode("CODE_" + i); region.setName("NAME_" + i); session.save(region); } tx.commit(); t = System.currentTimeMillis() - t; System.out.println("\nTotal Use " + t + " ms\n"); session.close(); sf.close(); } } [/code] 5. 程序代码 它是用Hibernate3和Eclipse3开发的,打包为<a href="Hibernate3Test.zip">Hibernate3Test.zip</a>