





















准备:
1.apache-ant-1.7.0.
2.hibernate-3.2.1.ga.zip,HibernateTools-3.2.1.ga.zip.
3.数据库的jdbc驱动程序,我使用的是mysql,驱动程序为mysql-connector-java-5.1.6。
项目布置:
1.建立Project-Name文件夹,其下建立:config,java,schema,lib文件夹以及build.xml文件.
2.加入jar包:hibernate3.jar,hibernate-tools.jar,freemarker.jar,mysql-connector-java.jar,...
3.src/hibernate.cfg.xml:
<?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"><hibernate-configuration>
<session-factory>
<property name="connection.username">root</property>
<property name="connection.password">cosmo</property><!--Your DB password here.-->
<property name="connection.url">jdbc:mysql://localhost:3306/MasteryHibernate</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="chapter3/Customer.hbm.xml" />
</session-factory>
</hibernate-configuration>
4.src/your-package/Customer.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping>
<class name="chapter3.Customer" table="CUSTOMERS">
<meta attribute="class-description">
Represents a single customer.
@author Cosmo
</meta><meta attribute="class-scope">public</meta><id name="id" type="long" column="ID">
<meta attribute="scope-set">protected</meta>
<generator class="native" />
</id><property name="name" type="string">
<meta attribute="use-in-tostring">true</meta>
<column name="NAME" length="15" not-null="true" unique="true" />
</property><property name="registeredTime" type="timestamp">
<meta attribute="field-description">When the customer was registered</meta>
<meta attribute="use-in-tostring">true</meta>
<column name="REGISTERED_TIME" index="IDX_REGISTERED_TIME"
sql-type="timestamp" />
</property>
<property name="age" type="int">
<meta attribute="field-description">How old is the customer</meta>
<meta attribute="use-in-tostring">true</meta>
<column name="AGE" check="AGE>10" not-null="true" />
</property>
<property name="sex" type="char" column="SEX" />
<property name="married" type="boolean" column="IS_MARRIED">
<meta attribute="field-description">Is the customer married</meta>
<meta attribute="use-in-tostring">true</meta>
</property>
<property name="description" type="string">
<meta attribute="use-in-tostring">true</meta>
<column name="DESCRIPTION" sql-type="text" />
</property>
</class>
</hibernate-mapping>
5.build.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!-- ======================================================================
2008-12-12 下午02:47:47
Hbm2javaAndHbm2ddl
Useing hibernate-tools
Cosmo
======================================================================
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。