利用Hibernate工具,通过xml文件自动创建表

利用Hibernate工具,通过xml文件自动创建表

执行ant hbm2java,可以在src目录中生成java文件;

执行ant hbm2ddl,可以通过*.hbm.xml文件生成*.ddl文件,并在数据库当中自动创建表。

内容如下:

<?xml version="1.0" ?>

 

<project name="firstbuild" default="hbm2ddl" basedir=".">

  <property environment="env" />

  <property name="src" value="src" />

  <property name="lib" value="lib" />

  <property name="doc" value="doc" />

  <property name="xml" value="xml" />

  <property name="classes" value="classes" /> 

  <property name="hibernate2_jar" value="hibernate2.jar" />

  <property name="hibernateTool_jar" value="hibernate-tools.jar" />

 

  <property name="CodeGenerator" value="net.sf.hibernate.tool.hbm2java.CodeGenerator" />

  <property name="SchemaExport" value="net.sf.hibernate.tool.hbm2ddl.SchemaExport" />

 

  <path id="task.classpath">

    <pathelement location="${classes}" />

    <pathelement location="${lib}" />

   

    <!-- hibernate.properties -->

    <pathelement location="${lib}/hibernate.properties" />

   

    <!-- log4j.properties -->

    <pathelement location="${lib}/log4j.properties" />

   

    <pathelement location="${lib}/ant.jar" />

    <pathelement location="${lib}/odmg.jar" />

   

    <!-- XML parser -->

    <pathelement location="${lib}/jdom.jar" />

    <pathelement location="${lib}/dom4j.jar" />

 

    <!-- Jakarta Log4j API -->

    <pathelement location="${lib}/log4j-1.2.8.jar" />   

   

    <!-- Jakarta Commons API -->

    <pathelement location="${lib}/commons-lang.jar" />

    <pathelement location="${lib}/commons-logging.jar" /> 

    <pathelement location="${lib}/commons-beanutils.jar" /> 

    <pathelement location="${lib}/commons-collections.jar" />

   

    <!-- hibernate 2.0.2 -->

    <pathelement location="${lib}/hibernate2.jar" />

    <pathelement location="${lib}/hibernate-tools.jar" />

   

    <!-- Struts 1.1 API -->

    <pathelement location="${lib}/struts.jar" />

   

    <!-- Servlet 2.3 API -->

    <pathelement location="${lib}/jsdk23.jar" />

   

    <!-- mySQL Driver -->

    <pathelement location="${lib}/mysql-connector-java-3.0.10-stable-bin.jar" />  

   

    <!-- htmltable -->

    <pathelement location="${lib}/htmltable.jar" />

    

  </path>

 

  <!-- Hibernate mapping files -->

  <fileset id="hibernate.mapping.files" dir="${xml}">

    <include name="*.hbm.xml" />

  </fileset>

   

  <target name="init" >

    <mkdir dir="${src}" />

    <mkdir dir="${xml}" />

    <mkdir dir="${lib}" />

    <mkdir dir="${classes}" />

    <echo>Init Complete !</echo>           

  </target>

 

  <target name="hbm2java" >

    <pathconvert refid="hibernate.mapping.files" property="hibernate.mappings" pathsep=" "/>

    <echo>Run hbm2java !</echo>

    <java classpathref="task.classpath" classname="${CodeGenerator}" fork="true" >       

      <arg value="--output=${src}" />

      <arg line="${hibernate.mappings}"/>

    </java>

    <echo>hbm2java Complete !</echo>

  </target>

 

  <target name="compile" depends="hbm2java" description="Generates PO Java">

    <javac classpathref="task.classpath" srcdir="${src}" destdir="${classes}" />       

    <echo>Compilation Complete !</echo>

  </target>

 

  <target name="hbm2ddl" depends="init" description="Generates DDL.">

    <java classpathref="task.classpath" classname="${SchemaExport}" fork="true" >

      <arg line="--output=schema.ddl"/>

      <arg file="${xml}/*.hbm.xml" />

    </java>

    <echo>hbm2ddl Complete !</echo>

  </target>

 

  <target name="make-docs" description="Generates JavaDoc.">

    <mkdir dir="${doc}"/>

    <javadoc sourcepath="${src}"

         destdir="${doc}"

         packagenames="demo.*"

         classpathref="task.classpath"

             private="true"  

         author="true"/>

  </target>

 </project>

你可能感兴趣的:(xml,log4j,Hibernate,mysql,ant)