hibernate tool 教下新手

顺便研究了下hibernatetool 发现已经到了4.0beta,我用的是3.2的没有解决oracle的注释问题,4.0已经解决了

 源代码获取

http://anonsvn.jboss.org/repos/jbosstools/tags/

找一个你适合的分支

用svn工具下载下来

我用maven2创建了一个项目

添加了如下依赖

 

[html] view plaincopy

  1. <project xmlns=”http://maven.apache.org/POM/4.0.0″ xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>
  2.   <modelVersion>4.0.0</modelVersion>
  3.   <parent>
  4.     <artifactId>ling2.parent</artifactId>
  5.     <groupId>com.ling</groupId>
  6.     <version>0.0.1-SNAPSHOT</version>
  7.   </parent>
  8.   <groupId>com.ling</groupId>
  9.   <artifactId>ling2.autoproject</artifactId>
  10.   <version>0.0.1-SNAPSHOT</version>
  11.   <dependencies>
  12.     <dependency>
  13.       <groupId>org.hibernate</groupId>
  14.       <artifactId>hibernate-tools</artifactId>
  15.       <version>4.0.0-CR1</version>
  16.       <scope>compile</scope>
  17.     </dependency>
  18.     <dependency>
  19.         <groupId>org.eclipse.equinox</groupId>
  20.         <artifactId>registry</artifactId>
  21.         <version>3.3.0-v20070522</version>
  22.         <type>jar</type>
  23.         <scope>compile</scope>
  24.     </dependency>
  25.     <dependency>
  26.         <groupId>org.eclipse.core</groupId>
  27.         <artifactId>jobs</artifactId>
  28.         <version>3.3.0-v20070423</version>
  29.         <type>jar</type>
  30.         <scope>compile</scope>
  31.     </dependency>
  32.   </dependencies>
  33. </project>

parent的依赖是我的公共依赖

[html] view plaincopy

  1. <project xmlns=”http://maven.apache.org/POM/4.0.0″ xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
  2.     xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>
  3.     <modelVersion>4.0.0</modelVersion>
  4.     <groupId>com.ling</groupId>
  5.     <artifactId>ling2.parent</artifactId>
  6.     <packaging>pom</packaging>
  7.     <version>0.0.1-SNAPSHOT</version>
  8.     <modules>
  9.         <module>ling2.core</module>
  10.         <module>ling2.core.d5</module>
  11.         <module>ling2.projecttemplate</module>
  12.         <module>ling2.autoproject</module>
  13.     </modules>
  14.     <build>
  15.         <defaultGoal>compile</defaultGoal>
  16.         <finalName>ling2.core</finalName>
  17.         <sourceDirectory>src/main/java</sourceDirectory>
  18.         <resources>
  19.             <!– 控制资源文件的拷贝 –>
  20.             <resource>
  21.                 <directory>src/main/resources</directory>
  22.                 <targetPath>${project.build.directory}</targetPath>
  23.             </resource>
  24.         </resources>
  25.         <plugins>
  26.             <!– 设置源文件编码方式 –>
  27.             <plugin>
  28.                 <groupId>org.apache.maven.plugins</groupId>
  29.                 <artifactId>maven-compiler-plugin</artifactId>
  30.                 <configuration>
  31.                     <defaultLibBundleDir>lib</defaultLibBundleDir>
  32.                     <source>1.6</source>
  33.                     <target>1.6</target>
  34.                     <encoding>UTF-8</encoding>
  35.                 </configuration>
  36.             </plugin>
  37.             <!– 打包jar文件时,配置manifest文件,加入lib包的jar依赖 –>
  38.             <plugin>
  39.                 <groupId>org.apache.maven.plugins</groupId>
  40.                 <artifactId>maven-jar-plugin</artifactId>
  41.                 <configuration>
  42.                     <archive>
  43.                         <manifest>
  44.                             <addClasspath>true</addClasspath>
  45.                             <classpathPrefix>lib/</classpathPrefix>
  46.                             <mainClass>…..MonitorMain</mainClass>
  47.                         </manifest>
  48.                     </archive>
  49.                 </configuration>
  50.             </plugin>
  51.             <!– 拷贝依赖的jar包到lib目录 –>
  52.             <plugin>
  53.                 <groupId>org.apache.maven.plugins</groupId>
  54.                 <artifactId>maven-dependency-plugin</artifactId>
  55.                 <executions>
  56.                     <execution>
  57.                         <id>copy</id>
  58.                         <phase>package</phase>
  59.                         <goals>
  60.                             <goal>copy-dependencies</goal>
  61.                         </goals>
  62.                         <configuration>
  63.                             <outputDirectory>
  64.                                 ${project.build.directory}/lib
  65.                             </outputDirectory>
  66.                         </configuration>
  67.                     </execution>
  68.                 </executions>
  69.             </plugin>
  70.             <!– 解决资源文件的编码问题 –>
  71.             <plugin>
  72.                 <groupId>org.apache.maven.plugins</groupId>
  73.                 <artifactId>maven-resources-plugin</artifactId>
  74.                 <version>2.3</version>
  75.                 <configuration>
  76.                     <encoding>UTF-8</encoding>
  77.                 </configuration>
  78.             </plugin>
  79.             <!– 打包source文件为jar文件 –>
  80.             <plugin>
  81.                 <artifactId>maven-source-plugin</artifactId>
  82.                 <version>2.1</version>
  83.                 <configuration>
  84.                     <attach>true</attach>
  85.                     <encoding>UTF-8</encoding>
  86.                 </configuration>
  87.                 <executions>
  88.                     <execution>
  89.                         <phase>compile</phase>
  90.                         <goals>
  91.                             <goal>jar</goal>
  92.                         </goals>
  93.                     </execution>
  94.                 </executions>
  95.             </plugin>
  96.         </plugins>
  97.     </build>
  98.     <repositories>
  99.         <repository>
  100.             <id>bsdn-maven-repository</id>
  101.             <url>http://nexus.bsdn.org/content/groups/public/</url>
  102.         </repository>
  103.     </repositories>
  104.     <dependencies>
  105.         <dependency>
  106.             <artifactId>spring-orm</artifactId>
  107.             <groupId>org.springframework</groupId>
  108.             <version>3.2.1.RELEASE</version>
  109.         </dependency>
  110.         <dependency>
  111.             <groupId>org.springframework</groupId>
  112.             <artifactId>spring-context-support</artifactId>
  113.             <version>3.2.1.RELEASE</version>
  114.         </dependency>
  115.         <dependency>
  116.             <groupId>org.springframework</groupId>
  117.             <artifactId>spring-tx</artifactId>
  118.             <version>3.2.1.RELEASE</version>
  119.         </dependency>
  120.         <dependency>
  121.             <groupId>org.springframework</groupId>
  122.             <artifactId>spring-webmvc</artifactId>
  123.             <version>3.2.1.RELEASE</version>
  124.         </dependency>
  125.         <dependency>
  126.             <groupId>org.springframework</groupId>
  127.             <artifactId>spring-web</artifactId>
  128.             <version>3.2.1.RELEASE</version>
  129.         </dependency>
  130.         <dependency>
  131.             <groupId>org.springframework</groupId>
  132.             <artifactId>spring-jdbc</artifactId>
  133.             <version>3.2.1.RELEASE</version>
  134.         </dependency>
  135.         <dependency>
  136.             <groupId>javax.servlet</groupId>
  137.             <artifactId>servlet-api</artifactId>
  138.             <version>2.5</version>
  139.         </dependency>
  140.         <dependency>
  141.             <groupId>com.bstek.dorado</groupId>
  142.             <artifactId>dorado-core</artifactId>
  143.             <version>7.2.4</version>
  144.             <exclusions>
  145.                 <exclusion>
  146.                     <groupId>com.h2database</groupId>
  147.                     <artifactId>h2</artifactId>
  148.                 </exclusion>
  149.                 <exclusion>
  150.                     <artifactId>spring-context</artifactId>
  151.                     <groupId>org.springframework</groupId>
  152.                 </exclusion>
  153.                 <exclusion>
  154.                     <artifactId>spring-aop</artifactId>
  155.                     <groupId>org.springframework</groupId>
  156.                 </exclusion>
  157.                 <exclusion>
  158.                     <artifactId>spring-core</artifactId>
  159.                     <groupId>org.springframework</groupId>
  160.                 </exclusion>
  161.                 <exclusion>
  162.                     <artifactId>spring-expression</artifactId>
  163.                     <groupId>org.springframework</groupId>
  164.                 </exclusion>
  165.                 <exclusion>
  166.                     <artifactId>spring-beans</artifactId>
  167.                     <groupId>org.springframework</groupId>
  168.                 </exclusion>
  169.                 <exclusion>
  170.                     <artifactId>spring-context-support</artifactId>
  171.                     <groupId>org.springframework</groupId>
  172.                 </exclusion>
  173.                 <exclusion>
  174.                     <artifactId>spring-web</artifactId>
  175.                     <groupId>org.springframework</groupId>
  176.                 </exclusion>
  177.                 <exclusion>
  178.                     <artifactId>spring-webmvc</artifactId>
  179.                     <groupId>org.springframework</groupId>
  180.                 </exclusion>
  181.                 <exclusion>
  182.                     <artifactId>javassist</artifactId>
  183.                     <groupId>javassist</groupId>
  184.                 </exclusion>
  185.             </exclusions>
  186.         </dependency>
  187.         <dependency>
  188.             <groupId>javax.mail</groupId>
  189.             <artifactId>mail</artifactId>
  190.             <version>1.4.1</version>
  191.         </dependency>
  192.         <dependency>
  193.             <groupId>jexcelapi</groupId>
  194.             <artifactId>jxl</artifactId>
  195.             <version>2.6</version>
  196.             <scope>compile</scope>
  197.         </dependency>
  198.         <dependency>
  199.             <groupId>org.hibernate</groupId>
  200.             <artifactId>hibernate-core</artifactId>
  201.             <version>4.2.1.Final</version>
  202.             <type>jar</type>
  203.             <scope>compile</scope>
  204.         </dependency>
  205.         <dependency>
  206.             <groupId>ognl</groupId>
  207.             <artifactId>ognl</artifactId>
  208.             <version>3.0.6</version>
  209.             <type>jar</type>
  210.             <scope>compile</scope>
  211.         </dependency>
  212.         <dependency>
  213.             <groupId>org.springframework</groupId>
  214.             <artifactId>spring-test</artifactId>
  215.             <version>3.2.2.RELEASE</version>
  216.             <type>jar</type>
  217.             <scope>compile</scope>
  218.         </dependency>
  219.         <dependency>
  220.             <groupId>org.springframework.security</groupId>
  221.             <artifactId>spring-security-web</artifactId>
  222.             <version>3.1.4.RELEASE</version>
  223.             <type>jar</type>
  224.             <scope>compile</scope>
  225.         </dependency>
  226.         <dependency>
  227.             <groupId>org.springframework.security</groupId>
  228.             <artifactId>spring-security-core</artifactId>
  229.             <version>3.1.4.RELEASE</version>
  230.             <type>jar</type>
  231.             <scope>compile</scope>
  232.         </dependency>
  233.         <dependency>
  234.             <groupId>org.springframework.security</groupId>
  235.             <artifactId>spring-security-config</artifactId>
  236.             <version>3.1.3.RELEASE</version>
  237.             <type>jar</type>
  238.             <scope>compile</scope>
  239.         </dependency>
  240.         <dependency>
  241.             <groupId>aspectj</groupId>
  242.             <artifactId>aspectjweaver</artifactId>
  243.             <version>1.5.4</version>
  244.             <scope>compile</scope>
  245.         </dependency>
  246.         <dependency>
  247.             <groupId>org.hibernate</groupId>
  248.             <artifactId>hibernate-c3p0</artifactId>
  249.             <version>4.2.1.Final</version>
  250.             <type>jar</type>
  251.             <scope>compile</scope>
  252.         </dependency>
  253.         <dependency>
  254.             <groupId>org.hibernate</groupId>
  255.             <artifactId>hibernate-ehcache</artifactId>
  256.             <version>4.2.1.Final</version>
  257.             <type>jar</type>
  258.             <scope>compile</scope>
  259.         </dependency>
  260.         <dependency>
  261.             <groupId>net.sf.ehcache</groupId>
  262.             <artifactId>ehcache-core</artifactId>
  263.             <version>2.4.3</version>
  264.             <type>jar</type>
  265.             <scope>compile</scope>
  266.         </dependency>
  267.     </dependencies>
  268. </project>

 

 

创建好的ling2.autoproject目录结构如下

运行maven命令

maven -install

 

相关jar包会自动放到../target/lib下

script 下的配置很重要,决定你能不能正常运行项目

我会当附件上传上去供大家借鉴

 

src.dir=../result
packagename=com.tplife.smp.channel.employee

决定生成的文件到那个目录,那个包下,

这些内容决定你编译需要哪些jar包和执行哪些动作

 

[html] view plaincopy

  1. <?xml version=”1.0″ encoding=”UTF-8″?>
  2. <project basedir=”.”>
  3.     <property file=”build.properties”>
  4.     </property>
  5.     <path id=”toolslib”>
  6.         <!–<path location=”../resources/libs/hibernate-tools.jar” />–>
  7.         <pathelement location=”../target/classes”/>
  8.         <fileset dir=”../target/lib”>
  9.             <include name=”**/*.jar” />
  10.         </fileset>
  11.     </path>
  12.     <target name=”hibernatecode”>
  13.         <taskdef name=”hibernatetool” classname=”org.hibernate.tool.ant.HibernateToolTask” classpathref=”toolslib” />
  14.         <!– destdir属性可以在各输出属性中分别指定,这里是全局指定 –>
  15.         <hibernatetool destdir=”${src.dir}” templatepath=”../src/main/java”><!–你模板的相对相对位置–>
  16.             <!– packagename表生成文件的存放包名 –>
  17.             <jdbcconfiguration propertyfile=”hibernate.properties” revengfile=”hibernate.reveng.xml” packagename=”${packagename}” />
  18.             <!– 生成.java文件 –>
  19.             <hbm2java jdk5=”true” ejb3=”true”/>
  20.             <hbm2hbmxml/>
  21.             <!–<hbm2idao />
  22.             <hbm2daoimpl />
  23.             <hbm2iservice />
  24.             <hbm2serviceimpl />
  25.             <hbm2view />
  26.             <hbm2viewmanager />
  27.             <hbm2viewjsp />–>
  28.         </hibernatetool>
  29.     </target>
  30.     <target name=”schemaexport”>
  31.         <taskdef name=”schemaexport” classname=”org.hibernate.tool.hbm2ddl.SchemaExportTask” classpathref=”toolslib” />
  32.         <schemaexport properties=”hibernate.properties” quiet=”no” text=”no” drop=”no” delimiter=”;” output=”schema-export.sql”>
  33.             <fileset dir=”${src.dir}”>
  34.                 <include name=”**/*.hbm.xml” />
  35.             </fileset>
  36.         </schemaexport>
  37.     </target>
  38.     <target name=”schemaupdate”>
  39.         <taskdef name=”schemaupdate” classname=”org.hibernate.tool.hbm2ddl.SchemaUpdateTask” classpathref=”toolslib” />
  40.         <schemaupdate properties=”hibernate.properties” quiet=”no”>
  41.             <fileset dir=”${src.dir}”>
  42.                 <include name=”**/*.hbm.xml” />
  43.             </fileset>
  44.         </schemaupdate>
  45.     </target>
  46.     <target name=”schemavalidate”>
  47.         <taskdef name=”schemavalidator” classname=”org.hibernate.tool.hbm2ddl.SchemaValidatorTask” classpathref=”toolslib” />
  48.         <schemavalidator properties=”hibernate.properties”>
  49.             <fileset dir=”${src.dir}”>
  50.                 <include name=”**/*.hbm.xml” />
  51.             </fileset>
  52.         </schemavalidator>
  53.     </target>
  54. </project>

这些配置决定你的数据源配置

 

#hibernate.connection.driver_class=com.mysql.jdbc.Driver
#hibernate.connection.url=jdbc:mysql://localhost:3306/zhonghong?useUnicode=true&amp;characterEncoding=utf8
#hibernate.connection.username=root
#hibernate.connection.password=2009
#hibernate.dialect=org.hibernate.dialect.MySQLDialect
#hibernatetool.metadatadialect=org.hibernate.cfg.reveng.dialect.MySQLMetaDataDialect
hibernatetool.metadatadialect=org.hibernate.cfg.reveng.dialect.OracleMetaDataDialect
#If neither MySQL nor Oracle,please try below
#hibernatetool.metadatadialect=org.hibernate.cfg.reveng.dialect.JDBCMetaDataDialect
hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver
hibernate.connection.url=jdbc:oracle:thin:@localhost:1521:orclwin7
#hibernate.connection.url=jdbc:oracle:thin:@10.1.105.1:1521:ram
hibernate.connection.username=train
hibernate.connection.password=train
hibernate.dialect=org.hibernate.dialect.Oracle9iDialect

这些配置决定你要生成什么表

 

[html] view plaincopy

  1. <?xml version=”1.0″ encoding=”UTF-8″?>
  2. <!DOCTYPE hibernate-reverse-engineering PUBLIC “-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN” “http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd” >
  3. <hibernate-reverse-engineering>
  4.     <type-mapping>
  5.         <sql-type jdbc-type=”NUMERIC” precision=”15″ hibernate-type=”big_decimal”/>
  6.         <sql-type jdbc-type=”NUMERIC” not-null=”true” hibernate-type=”long” />
  7.         <sql-type jdbc-type=”NUMERIC” not-null=”false” hibernate-type=”java.lang.Long” />
  8.         <sql-type jdbc-type=”VARCHAR” length=”1″ hibernate-type=”yes_no” />
  9.         <sql-type jdbc-type=”CHAR” length=”1″ hibernate-type=”java.lang.String” />
  10.         <sql-type jdbc-type=”VARCHAR” hibernate-type=”java.lang.String” />
  11.         <sql-type jdbc-type=”TIMESTAMP” length=”6″ hibernate-type=”java.util.Date” />
  12.         <!– mysql –>
  13.         <sql-type jdbc-type=”SMALLINT” hibernate-type=”java.lang.Integer” />
  14.         <sql-type jdbc-type=”INTEGER” length=”1″ hibernate-type=”yes_no” />
  15.         <sql-type jdbc-type=”BIGINT” hibernate-type=”java.lang.Long” />
  16.     </type-mapping>
  17.     <table-filter match-name=”TBL_FILE”></table-filter>
  18.      <table-filter match-schema=”.*” match-name=”SS_.*”/>
  19.     <table-filter match-schema=”.*” match-name=”.*” exclude=”true”/>
  20.  –>
  21. </hibernate-reverse-engineering>

按如下方式执行生成代码

 


然后你就可以在这个基础上编写自己的内容了,比如我的dorado5模板

<hbm2idao />
<hbm2daoimpl />
<hbm2iservice />
<hbm2serviceimpl />
<hbm2view />
<hbm2viewmanager />
<hbm2viewjsp />

顺序是

HibernateToolTask中注册你自定义的新模板

这里的方法命名决定你

<hbm2idao />
<hbm2daoimpl />
<hbm2iservice />
<hbm2serviceimpl />
<hbm2view />
<hbm2viewmanager />
<hbm2viewjsp />

 

[java] view plaincopy

  1. public ExporterTask createHbm2DAO(){
  2.         ExporterTask generator= new Hbm2DAOExporterTask(this);
  3.         addGenerator( generator );
  4.         return generator;
  5.     }
  6.     public ExporterTask createHbm2IDAO(){
  7.         ExporterTask generator= new Hbm2IDAOExporterTask(this);
  8.         addGenerator( generator );
  9.         return generator;
  10.     }
  11.     public ExporterTask createHbm2DAOImpl(){
  12.         ExporterTask generator= new Hbm2DAOImplExporterTask(this);
  13.         addGenerator( generator );
  14.         return generator;
  15.     }
  16.     public ExporterTask createHbm2IService(){
  17.         ExporterTask generator= new Hbm2IServiceExporterTask(this);
  18.         addGenerator( generator );
  19.         return generator;
  20.     }
  21.     public ExporterTask createHbm2ServiceImpl(){
  22.         ExporterTask generator= new Hbm2ServiceImplExporterTask(this);
  23.         addGenerator( generator );
  24.         return generator;
  25.     }
  26.     public ExporterTask createHbm2View(){
  27.         ExporterTask generator= new Hbm2ViewExporterTask(this);
  28.         addGenerator( generator );
  29.         return generator;
  30.     }
  31.     public ExporterTask createHbm2ViewManager(){
  32.         ExporterTask generator= new Hbm2ViewTemplateExporterTask(this);
  33.         addGenerator( generator );
  34.         return generator;
  35.     }
  36.     public ExporterTask createHbm2ViewJsp(){
  37.         ExporterTask generator= new Hbm2ViewJspExporterTask(this);
  38.         addGenerator( generator );
  39.         return generator;
  40.     }

以下以Hbm2ViewJsp为例子说明

 

 

[java] view plaincopy

  1. /*
  2.  * Created on 13-Feb-2005
  3.  *
  4.  */
  5. package org.hibernate.tool.ant;
  6. import java.io.File;
  7. import java.util.ArrayList;
  8. import java.util.Iterator;
  9. import java.util.List;
  10. import java.util.Properties;
  11. import org.apache.tools.ant.AntClassLoader;
  12. import org.apache.tools.ant.BuildException;
  13. import org.apache.tools.ant.Project;
  14. import org.apache.tools.ant.Task;
  15. import org.apache.tools.ant.types.Environment;
  16. import org.apache.tools.ant.types.Path;
  17. import org.apache.tools.ant.types.PropertySet;
  18. import org.hibernate.MappingException;
  19. import org.hibernate.MappingNotFoundException;
  20. import org.hibernate.cfg.Configuration;
  21. import org.hibernate.util.StringHelper;
  22. /**
  23.  * @author max
  24.  *
  25.  */
  26. public class HibernateToolTask extends Task {
  27.     ConfigurationTask configurationTask;
  28.     private File destDir;
  29.     private List generators = new ArrayList();
  30.     private Path classPath;
  31.     private Path templatePath;
  32.     private Properties properties = new Properties();
  33.     private void checkConfiguration() {
  34.         if(configurationTask!=null) {
  35.             throw new BuildException(“Only a single configuration is allowed.”);
  36.         }
  37.     }
  38.     public ConfigurationTask createConfiguration() {
  39.         checkConfiguration();
  40.         configurationTask = new ConfigurationTask();
  41.         return configurationTask;
  42.     }
  43.     public JDBCConfigurationTask createJDBCConfiguration() {
  44.         checkConfiguration();
  45.         configurationTask = new JDBCConfigurationTask();
  46.         return (JDBCConfigurationTask) configurationTask;
  47.     }
  48.     public AnnotationConfigurationTask createAnnotationConfiguration() {
  49.         checkConfiguration();
  50.         configurationTask = new AnnotationConfigurationTask();
  51.         return (AnnotationConfigurationTask) configurationTask;
  52.     }
  53.     public JPAConfigurationTask createEjb3Configuration() {
  54.         log( “<ejb3configuration> is deprecated. Use <jpaconfiguration> instead.”, Project.MSG_WARN);
  55.         checkConfiguration();
  56.         configurationTask = new JPAConfigurationTask();
  57.         return (JPAConfigurationTask) configurationTask;
  58.     }
  59.     public JPAConfigurationTask createJpaConfiguration() {
  60.         checkConfiguration();
  61.         configurationTask = new JPAConfigurationTask();
  62.         return (JPAConfigurationTask) configurationTask;
  63.     }
  64.     public ExporterTask createHbm2DDL() {
  65.         ExporterTask generator = new Hbm2DDLExporterTask(this);
  66.         addGenerator( generator );
  67.         return generator;
  68.     }
  69.     public ExporterTask createHbmTemplate() {
  70.         ExporterTask generator = new GenericExporterTask(this);
  71.         addGenerator( generator );
  72.         return generator;
  73.     }
  74.     public ExporterTask createHbm2CfgXml() {
  75.         ExporterTask generator = new Hbm2CfgXmlExporterTask(this);
  76.         addGenerator( generator );
  77.         return generator;
  78.     }
  79.     protected boolean addGenerator(ExporterTask generator) {
  80.         return generators.add(generator);
  81.     }
  82.     public ExporterTask createHbm2Java() {
  83.         ExporterTask generator = new Hbm2JavaExporterTask(this);
  84.         addGenerator( generator );
  85.         return generator;
  86.     }
  87.     public ExporterTask createHbm2HbmXml() {
  88.         ExporterTask generator= new Hbm2HbmXmlExporterTask(this);
  89.         addGenerator( generator );
  90.         return generator;
  91.     }
  92.     public ExporterTask createHbm2Doc() {
  93.         ExporterTask generator= new Hbm2DocExporterTask(this);
  94.         addGenerator( generator );
  95.         return generator;
  96.     }
  97.     /*public ExporterTask createHbm2Jsf(){
  98.         ExporterTask generator= new Hbm2JsfGeneratorTask(this);
  99.         generators.add(generator);
  100.         return generator;
  101.     }*/
  102.     public ExporterTask createHbm2DAO(){
  103.         ExporterTask generator= new Hbm2DAOExporterTask(this);
  104.         addGenerator( generator );
  105.         return generator;
  106.     }
  107.     public ExporterTask createHbm2IDAO(){
  108.         ExporterTask generator= new Hbm2IDAOExporterTask(this);
  109.         addGenerator( generator );
  110.         return generator;
  111.     }
  112.     public ExporterTask createHbm2DAOImpl(){
  113.         ExporterTask generator= new Hbm2DAOImplExporterTask(this);
  114.         addGenerator( generator );
  115.         return generator;
  116.     }
  117.     public ExporterTask createHbm2IService(){
  118.         ExporterTask generator= new Hbm2IServiceExporterTask(this);
  119.         addGenerator( generator );
  120.         return generator;
  121.     }
  122.     public ExporterTask createHbm2ServiceImpl(){
  123.         ExporterTask generator= new Hbm2ServiceImplExporterTask(this);
  124.         addGenerator( generator );
  125.         return generator;
  126.     }
  127.     public ExporterTask createHbm2View(){
  128.         ExporterTask generator= new Hbm2ViewExporterTask(this);
  129.         addGenerator( generator );
  130.         return generator;
  131.     }
  132.     public ExporterTask createHbm2ViewManager(){
  133.         ExporterTask generator= new Hbm2ViewTemplateExporterTask(this);
  134.         addGenerator( generator );
  135.         return generator;
  136.     }
  137.     public ExporterTask createHbm2ViewJsp(){
  138.         ExporterTask generator= new Hbm2ViewJspExporterTask(this);
  139.         addGenerator( generator );
  140.         return generator;
  141.     }
  142.     public QueryExporterTask createQuery() {
  143.         QueryExporterTask generator = new QueryExporterTask(this);
  144.         generators.add(generator);
  145.         return generator;
  146.     }
  147.     public HbmLintExporterTask createHbmLint() {
  148.         HbmLintExporterTask generator = new HbmLintExporterTask(this);
  149.         generators.add(generator);
  150.         return generator;
  151.     }
  152.     /**
  153.      * Set the classpath to be used when running the Java class
  154.      *
  155.      * @param s an Ant Path object containing the classpath.
  156.      */
  157.     public void setClasspath(Path s) {
  158.         classPath = s;
  159.     }
  160.     /**
  161.      * Adds a path to the classpath.
  162.      *
  163.      * @return created classpath
  164.      */
  165.     public Path createClasspath() {
  166.         classPath = new Path(getProject() );
  167.         return classPath;
  168.     }
  169.     public void execute() {
  170.         if(configurationTask==null) {
  171.             throw new BuildException(“No configuration specified. <” + getTaskName() + “> must have one of the following: <configuration>, <jpaconfiguration>, <annotationconfiguration> or <jdbcconfiguration>”);
  172.         }
  173.         log(“Executing Hibernate Tool with a ” + configurationTask.getDescription() );
  174.         validateParameters();
  175.         Iterator iterator = generators.iterator();
  176.         AntClassLoader loader = getProject().createClassLoader(classPath);
  177.         ExporterTask generatorTask = null;
  178.         int count = 1;
  179.         try {
  180.             loader.setParent(this.getClass().getClassLoader() ); // if this is not set, classes from the taskdef cannot be found – which is crucial for e.g. annotations.
  181.             loader.setThreadContextLoader();
  182.             while (iterator.hasNext() ) {
  183.                 generatorTask = (ExporterTask) iterator.next();
  184.                 log(count++ + “. task: ” + generatorTask.getName() );
  185.                 generatorTask.execute();
  186.             }
  187.         } catch (RuntimeException re) {
  188.             reportException(re, count, generatorTask);
  189.         }
  190.         finally {
  191.             if (loader != null) {
  192.                 loader.resetThreadContextLoader();
  193.                 loader.cleanup();
  194.             }
  195.         }
  196.     }
  197.     private void reportException(Throwable re, int count, ExporterTask generatorTask) {
  198.         log(“An exception occurred while running exporter #” + count + “:” + generatorTask.getName(), Project.MSG_ERR);
  199.         log(“To get the full stack trace run ant with -verbose”, Project.MSG_ERR);
  200.         log(re.toString(), Project.MSG_ERR);
  201.         String ex = new String();
  202.         Throwable cause = re.getCause();
  203.         while(cause!=null) {
  204.             ex += cause.toString() + “\n”;
  205.             if(cause==cause.getCause()) {
  206.                 break; // we reached the top.
  207.             } else {
  208.                 cause=cause.getCause();
  209.             }
  210.         }
  211.         if(StringHelper.isNotEmpty(ex)) {
  212.             log(ex, Project.MSG_ERR);
  213.         }
  214.         String newbieMessage = getProbableSolutionOrCause(re);
  215.         if(newbieMessage!=null) {
  216.             log(newbieMessage);
  217.         }
  218.         if(re instanceof BuildException) {
  219.             throw (BuildException)re;
  220.         } else {
  221.             throw new BuildException(re, getLocation());
  222.         }
  223.     }
  224.     private String getProbableSolutionOrCause(Throwable re) {
  225.         if(re==null) return null;
  226.         if(re instanceof MappingNotFoundException) {
  227.             MappingNotFoundException mnf = (MappingNotFoundException)re;
  228.             if(“resource”.equals(mnf.getType())) {
  229.                 return “A ” + mnf.getType() + ” located at ” + mnf.getPath() + ” was not found.\n” +
  230.                         “Check the following:\n” +
  231.                         “\n” +
  232.                         “1) Is the spelling/casing correct ?\n” +
  233.                         “2) Is ” + mnf.getPath() + ” available via the classpath ?\n” +
  234.                         “3) Does it actually exist ?\n”;
  235.             } else {
  236.                 return “A ” + mnf.getType() + ” located at ” + mnf.getPath() + ” was not found.\n” +
  237.                 “Check the following:\n” +
  238.                 “\n” +
  239.                 “1) Is the spelling/casing correct ?\n” +
  240.                 “2) Do you permission to access ” + mnf.getPath() + ” ?\n” +
  241.                 “3) Does it actually exist ?\n”;
  242.             }
  243.         }
  244.         if(re instanceof ClassNotFoundException || re instanceof NoClassDefFoundError) {
  245.             return “A class were not found in the classpath of the Ant task.\n” +
  246.                     “Ensure that the classpath contains the classes needed for Hibernate and your code are in the classpath.\n”;
  247.         }
  248.         if(re instanceof UnsupportedClassVersionError) {
  249.             return “You are most likely running the ant task with a JRE that is older than the JRE required to use the classes.\n” +
  250.                     “e.g. running with JRE 1.3 or 1.4 when using JDK 1.5 annotations is not possible.\n” +
  251.                     “Ensure that you are using a correct JRE.”;
  252.         }
  253.         if(re.getCause()!=re) {
  254.             return getProbableSolutionOrCause( re.getCause() );
  255.         }
  256.         return null;
  257.     }
  258.     private void validateParameters() {
  259.         if(generators.isEmpty()) {
  260.             throw new BuildException(“No exporters specified in <hibernatetool>. There has to be at least one specified. An exporter is e.g. <hbm2java> or <hbmtemplate>. See documentation for details.”, getLocation());
  261.         } else {
  262.             Iterator iterator = generators.iterator();
  263.             while (iterator.hasNext() ) {
  264.                 ExporterTask generatorTask = (ExporterTask) iterator.next();
  265.                 generatorTask.validateParameters();
  266.             }
  267.         }
  268.     }
  269.     /**
  270.      * @return
  271.      */
  272.     public File getDestDir() {
  273.         return destDir;
  274.     }
  275.     public void setDestDir(File file) {
  276.         destDir = file;
  277.     }
  278.     /**
  279.      * @return
  280.      */
  281.     public Configuration getConfiguration() {
  282.         return configurationTask.getConfiguration();
  283.     }
  284.     public void setTemplatePath(Path path) {
  285.         templatePath = path;
  286.     }
  287.     public Path getTemplatePath() {
  288.         if(templatePath==null) {
  289.             templatePath = new Path(getProject()); // empty path
  290.         }
  291.         return templatePath;
  292.     }
  293.     public Properties getProperties() {
  294.         Properties p = new Properties();
  295.         p.putAll(getConfiguration().getProperties());
  296.         p.putAll(properties);
  297.         return p;
  298.     }
  299.     public void addConfiguredPropertySet(PropertySet ps) {
  300.         properties.putAll(ps.getProperties());
  301.     }
  302.     public void addConfiguredProperty(Environment.Variable property) {
  303.         properties.put(property.getKey(), property.getValue());
  304.     }
  305. }
[java] view plaincopy

  1. 写一个继承
[java] view plaincopy

  1. Hbm2ViewJspExporterTask  <span style=”font-family: Arial, Helvetica, sans-serif;”>extends Hbm2JavaExporterTask </span>
[java] view plaincopy

  1. package org.hibernate.tool.ant;
  2. import org.hibernate.tool.hbm2x.DAOExporter;
  3. import org.hibernate.tool.hbm2x.DAOImplExporter;
  4. import org.hibernate.tool.hbm2x.Exporter;
  5. import org.hibernate.tool.hbm2x.IDAOExporter;
  6. import org.hibernate.tool.hbm2x.ServiceImplExporter;
  7. import org.hibernate.tool.hbm2x.ViewExporter;
  8. import org.hibernate.tool.hbm2x.ViewJspExporter;
  9. import org.hibernate.tool.hbm2x.ViewTemplateExporter;
  10. /**
  11.  * @author Dennis Byrne
  12.  */
  13. public class Hbm2ViewJspExporterTask extends Hbm2JavaExporterTask {
  14.     public Hbm2ViewJspExporterTask(HibernateToolTask parent) {
  15.         super(parent);
  16.     }
  17.     protected Exporter configureExporter(Exporter exp) {
  18.         ViewJspExporter exporter = (ViewJspExporter)exp;
  19.         super.configureExporter(exp);
  20.         return exporter;
  21.     }
  22.     protected Exporter createExporter() {
  23.         return new ViewJspExporter(parent.getConfiguration(), parent.getDestDir()) ;
  24.     }
  25.     public String getName() {
  26.         return “hbm2viewjsp (Generates a set of viewjsp)”;
  27.     }
  28. }

这里决定你ftl的模板的相对位置

 

 

[java] view plaincopy

  1. package org.hibernate.tool.hbm2x;
  2. import java.io.File;
  3. import java.util.Map;
  4. import org.hibernate.cfg.Configuration;
  5. import org.hibernate.tool.hbm2x.pojo.POJOClass;
  6. public class ViewJspExporter extends POJOExporter {
[java] view plaincopy

  1. <span style=”white-space:pre”>  </span>//模板相对位置
  2.     private static final String DAO_DAOHOME_FTL = “dao/ViewJsp.ftl”;
  3.     private String sessionFactoryName = “SessionFactory”;
  4.     public ViewJspExporter() {
  5.     }
  6.     public ViewJspExporter(Configuration cfg, File outputdir) {
  7.         super(cfg, outputdir);
  8.     }
  9.     protected void init() {
  10.         super.init();
  11.         setTemplateName(DAO_DAOHOME_FTL);
[java] view plaincopy

  1. //生成好的文件存放的相对位置
  2.         setFilePattern(“{package-name}/view/jsp/{class-name}Manager.jsp”);
  3.     }
  4.     protected void exportComponent(Map additionalContext, POJOClass element) {
  5.         // noop – we dont want components
  6.     }
  7.     public String getSessionFactoryName() {
  8.         return sessionFactoryName;
  9.     }
  10.     public void setSessionFactoryName(String sessionFactoryName) {
  11.         this.sessionFactoryName = sessionFactoryName;
  12.     }
  13.     protected void setupContext() {
  14.         getProperties().put(“sessionFactoryName”, getSessionFactoryName());
  15.         super.setupContext();
  16.     }
  17.     public String getName() {
  18.         return “hbm2view”;
  19.     }
  20. }

ftl模板内容

[java] view plaincopy

  1. <#assign classbody>
  2. <#assign declarationName = pojo.importType(pojo.getDeclarationName())>
  3. <%@ page contentType=”text/html; charset=UTF-8″%>
  4. <%@ taglib uri=”http://www.bstek.com/dorado” prefix=”d”%>
  5. <html>
  6. <head>
  7. <title>管理</title>
  8. </head>
  9. <body scroll=”yes”>
  10. <d:View config=”${pojo.getPackageName()}.view.${declarationName}Manager”>
  11.     <d:Layout type=”border” height=”100%”>
  12.         <d:Pane position=”top”>
  13.                     <d:Layout type=”Vflow”>
  14.                         <d:Pane>
  15.                             <d:AutoForm id=”frmQuery” />
  16.                         </d:Pane>
  17.                         <d:Pane>
  18.                             <d:Layout type=”Hflow”>
  19.                                 <d:Pane>
  20.                             <d:Button id=”btnQuery” />
  21.                             </d:Pane>
  22.                             <d:Pane>
  23.                                 <d:Button id=”btnReset” />
  24.                             </d:Pane>
  25.                             <d:Pane>
  26.                                 <d:Button id=”btnNew” />
  27.                             </d:Pane>
  28.                             <d:Pane>
  29.                                 <d:Button id=”btnDelete” />
  30.                             </d:Pane>
  31.                             <d:Pane>
  32.                                 <d:Button id=”buttonModify” />
  33.                             </d:Pane>
  34.                             </d:Layout>
  35.                         </d:Pane>
  36.                     </d:Layout>
  37.         </d:Pane>
  38.         <d:Pane position=”center”>
  39.             <d:DataTable id=”tblEntity” />
  40.         </d:Pane>
  41.         <d:Pane position=”bottom”>
  42.             <d:PagePilot id=”pageEntity” />
  43.             <table cellpadding=”5″>
  44.                 <tr>
  45.                     <td nowrap bgcolor=”#F5F7F9″>数据提取模式:</td>
  46.                     <td bgcolor=”#F5F7F9″><d:RadioGroup id=”editorDataMode” /></td>
  47.                     <td bgcolor=”#F5F7F9″><d:Button id=”buttonExcel” /></td>
  48.                 </tr>
  49.             </table>
  50.         </d:Pane>
  51.     </d:Layout>
  52.     <d:SubWindow id=”win1″ width=”450px” height=”150px” draggable=”true”
  53.         resizable=”true” status=”hidden” showMinimizeButton=”false”
  54.         showMaximizeButton=”false” showCloseButton=”false”>
  55.         <d:Layout type=”border”>
  56.             <d:Pane position=”top”>
  57.                 <d:AutoForm id=”frmEntity” />
  58.             </d:Pane>
  59.             <d:Pane position=”center” align=”right”>
  60.                 <d:Layout type=”Hflow”>
  61.                     <d:Pane>
  62.                         <d:Button id=”btnSave” />
  63.                     </d:Pane>
  64.                     <d:Pane>
  65.                         <d:Button id=”btnCancel” />
  66.                     </d:Pane>
  67.                 </d:Layout>
  68.             </d:Pane>
  69.         </d:Layout>
  70.     </d:SubWindow>
  71. </d:View>
  72. </body>
  73. </html>
  74. </#assign>
  75. ${classbody}

 

 

以上内容是3.2的tool下的模板,修改过不少东西.仅供思路指导

标签