首页 > 代码库 > hbm2java和hbm2ddl工具
hbm2java和hbm2ddl工具
hbm2java工具根据映射文件自动生成Java源文件,而hbm2ddl工具则根据映射文件自动生成数据库Schema,下面是这两种工具的使用:
1.建立Java工程,加入Jar包,创建创建hibernate配置文件以及响应的映射文件,相关jar包及工程目录如下:
2.相关文件代码如下:
build.xml:
1 <?xml version="1.0"?> 2 <project name="Learning Hibernate" default="prepare" basedir=""> 3 <property name="source.root" value="http://www.mamicode.com/src"> 4 </property> 5 <property name="class.root" value="http://www.mamicode.com/classes"> 6 </property> 7 <property name="lib.dir" value="http://www.mamicode.com/lib"> 8 </property> 9 <property name="schema" value="http://www.mamicode.com/schema"> 10 </property> 11 <path id="project.class.path"> 12 <!-- Include our own classes,of course --> 13 <pathelement location="${class.root}" /> 14 <!-- Include jars in the project library directory --> 15 <fileset dir="${lib.dir}"> 16 <include name="*.jar" /> 17 </fileset> 18 </path> 19 <target name="prepare" description="Sets up build structures"> 20 <delete dir="${class.root}"> 21 </delete> 22 <mkdir dir="${class.root}" /> 23 <copy todir="${class.root}"> 24 <fileset dir="${source.root}"> 25 <include name="**/*.properties" /> 26 <include name="**/*.hbm.xml" /> 27 <include name="**/*.cfg.xml" /> 28 </fileset> 29 </copy> 30 </target> 31 <!-- hbm2java任务--> 32 <target name="codegen" depends="prepare"> 33 <taskdef name="hbm2javaTask" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="http://www.mamicode.com/project.class.path" /> 34 <hbm2javaTask destdir="${source.root}"> 35 <configuration configurationfile="${class.root}/hibernate.cfg.xml" /> 36 <hbm2java /> 37 </hbm2javaTask> 38 </target> 39 <!--编译Java源文件 --> 40 <target name="compile" depends="codegen" description="Compiles all java classes"> 41 <javac srcdir="${source.root}" destdir="${class.root}" debug="on" optimize="off" deprecation="on" includeAntRuntime="false"> 42 <classpath refid="project.class.path"> 43 </classpath> 44 </javac> 45 </target> 46 <!--hbm2ddl任务--> 47 <target name="schema" depends="compile"> 48 <taskdef name="hbm2ddlTask" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="http://www.mamicode.com/project.class.path"> 49 </taskdef> 50 <hbm2ddlTask destdir="${schema.dir}"> 51 <configuration configurationfile="${class.root}/hibernate.cfg.xml" /> 52 <hbm2ddl export="true" console="true" create="true" drop="true" /> 53 </hbm2ddlTask> 54 </target> 55 </project>
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://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6 <session-factory> 7 <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 8 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 9 <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property> 10 <property name="connection.username">root</property> 11 <property name="connection.password"></property> 12 <property name="show_sql">true</property> 13 <mapping resource="cn/xieyuyan/Customer.hbm.xml" /> 14 </session-factory> 15 </hibernate-configuration>
customer.hbm.xml:
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping> 6 <class name="cn.xieyuyan.Customer" table="CUSTOMERS"> 7 <id name="id" column="ID" type="int"> 8 <generator class="increment" /> 9 </id> 10 <property name="name" column="NAME" type="string" not-null="true" /> 11 <property name="email" column="EMAIL" type="string" not-null="true" /> 12 <property name="password" column="PASSWORD" type="string" 13 not-null="true" /> 14 <property name="phone" column="PHONE" type="string"></property> 15 <property name="address" column="ADDRESS" type="string"></property> 16 <property name="description" column="DESCRIPTION" type="text" /> 17 <property name="birthday" column="BIRTHDAY" type="date" /> 18 <property name="registeredTime" column="REGISTERED_TIME" 19 type="timestamp" /> 20 </class> 21 </hibernate-mapping>
3.运行项目:
run as,ant build,刷新项目,工程目录如下:
其中Customer.java即为根据映射文件生成的java源文件,classes目录下为编译后的classes文件
查看数据库,发现新生成了一个customers表,即为根据映射文件生成的数据库文件
hbm2java和hbm2ddl工具
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。