Java 如何更改Hibernate工具反向工程DAO类的名称?
因此,我试图使用Hibernate工具对数据库进行反向工程,而我只是开始使用Freemarker模板来弱化它生成的代码。问题是我想更改它生成的DAO类的名称。默认情况下,DAO类以PersonHome的形式命名,但是要将名称更改为PersonDAO,我修改了DAO/daohome.ftlJava 如何更改Hibernate工具反向工程DAO类的名称?,java,hibernate,reverse-engineering,freemarker,hibernate-tools,Java,Hibernate,Reverse Engineering,Freemarker,Hibernate Tools,因此,我试图使用Hibernate工具对数据库进行反向工程,而我只是开始使用Freemarker模板来弱化它生成的代码。问题是我想更改它生成的DAO类的名称。默认情况下,DAO类以PersonHome的形式命名,但是要将名称更改为PersonDAO,我修改了DAO/daohome.ftl </dependencies> <build> <plugins> <plugin> <groupId&g
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>install</phase>
<configuration>
<target>
<ant antfile="${basedir}/build.xml">
<target name="gen_hibernate" />
</ant>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
虽然这确实将生成的类名更改为PersonDAO,但java文件仍然称为PersonHome.java
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>install</phase>
<configuration>
<target>
<ant antfile="${basedir}/build.xml">
<target name="gen_hibernate" />
</ant>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
是否有一个地方我可以更改生成的文件名以匹配源代码?我没有仔细研究这个问题,但我认为您必须修改
daowenexporter
类(请参阅以获取一些灵感) 好吧,我自己已经弄清楚了。虽然hibernate工具确实支持更改文件名,但Eclipse的hibernate工具插件中并未公开该功能,这令人沮丧。相反,我必须创建一个ant构建脚本来运行逆向工程,如下所示
<project name="Reverse Engineer" basedir=".">
<path id="toolslib">
<path location="lib/hibernate3.jar" />
<path location="lib/hibernate-tools.jar" />
<path location="lib/freemarker.jar" />
...
<path location="${jdbc.driver.jar}" />
</path>
<taskdef name="hibernatetool"
classname="org.hibernate.tool.ant.HibernateToolTask"
classpathref="toolslib" />
<hibernatetool destdir="src">
<jdbcconfiguration
configurationfile="src/hibernate.cfg.xml"
packagename="my.package.name"
revengfile="hibernate.reveng.xml">
</jdbcconfiguration>
<hbmtemplate destdir="src"
templatepath="templates"
template="dao/daohome.ftl"
filepattern="{package-name}/{class-name}DAO.java">
<property key="ejb3" value="false" />
<property key="jdk5" value="true" />
<property key="sessionFactoryName" value="my.HibernateSessionFactory" />
</hbmtemplate>
</hibernatetool>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>install</phase>
<configuration>
<target>
<ant antfile="${basedir}/build.xml">
<target name="gen_hibernate" />
</ant>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
我正在添加丢失的位
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>install</phase>
<configuration>
<target>
<ant antfile="${basedir}/build.xml">
<target name="gen_hibernate" />
</ant>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
用例:
修改hibernate工具生成的DAO名称类
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>install</phase>
<configuration>
<target>
<ant antfile="${basedir}/build.xml">
<target name="gen_hibernate" />
</ant>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
解决方案:
我使用了mvn和ant来实现这一点。这是一种迂回的方式,但对于那些设置了mvn的人来说更容易
<taskdef name="hibernatetool"
classname="org.hibernate.tool.ant.HibernateToolTask">
<classpath>
<fileset dir="${basedir}/lib">
<include name="*.jar"/>
</fileset>
</classpath>
</taskdef>
<target name="gen_hibernate"
description="generate hibernate classes">
<hibernatetool destdir="${basedir}/src/main/java">
<jdbcconfiguration
configurationfile="${basedir}/hibernate.cfg.xml"
packagename="com.bcbsmt.eie.framework.dto"
revengfile="${basedir}/hibernate.reveng.xml">
</jdbcconfiguration>
<hbmtemplate destdir="${basedir}/src/main/java"
templatepath="${basedir}/src/main/resources/template"
template="dao/daohome.ftl"
filepattern="{package-name}/{class-name}DAO.java">
<property key="ejb3" value="false" />
<property key="jdk5" value="true" />
</hbmtemplate>
<hbmtemplate destdir="${basedir}/src/main/java"
templatepath="${basedir}/src/main/resources/template"
template="pojo/Pojo.ftl"
filepattern="{package-name}/{class-name}.java">
<property key="ejb3" value="false" />
<property key="jdk5" value="true" />
</hbmtemplate>
</hibernatetool>
</target>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>install</phase>
<configuration>
<target>
<ant antfile="${basedir}/build.xml">
<target name="gen_hibernate" />
</ant>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>install</phase>
<configuration>
<target>
<ant antfile="${basedir}/build.xml">
<target name="gen_hibernate" />
</ant>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>install</phase>
<configuration>
<target>
<ant antfile="${basedir}/build.xml">
<target name="gen_hibernate" />
</ant>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
maven POM:
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>install</phase>
<configuration>
<target>
<ant antfile="${basedir}/build.xml">
<target name="gen_hibernate" />
</ant>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.bcbsmt
HibernateanNotationSample
0.0.1-快照
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>install</phase>
<configuration>
<target>
<ant antfile="${basedir}/build.xml">
<target name="gen_hibernate" />
</ant>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
org.apache.maven.plugins
maven antrun插件
1.7
生成源
安装
跑
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>install</phase>
<configuration>
<target>
<ant antfile="${basedir}/build.xml">
<target name="gen_hibernate" />
</ant>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
所需罐子:
这些JAR应该在${basedir}/lib文件夹中。版本根据您的意愿
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>install</phase>
<configuration>
<target>
<ant antfile="${basedir}/build.xml">
<target name="gen_hibernate" />
</ant>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
asm-1.5.3.jar
cglib-2.1_3.jar
commons-collections-20030418.083655.jar
commons-logging-1.1.1.jar
dom4j-1.6.1.jar
freemarker-2.3.8.jar
hibernate-3.3.2.jar
hibernate-annotations-3.5.6-Final.jar
hibernate-tools-3.2.4.GA.jar
jtidy-r8-20060801.jar
log4j-1.2.17.jar
slf4j-api-1.5.8.jar
slf4j-log4j12-1.5.8.jar
sqljdbc-4.0.jar使用“通用导出器
”工具,而不是“DAO代码(.java)
”。
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>install</phase>
<configuration>
<target>
<ant antfile="${basedir}/build.xml">
<target name="gen_hibernate" />
</ant>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
同时,在
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>install</phase>
<configuration>
<target>
<ant antfile="${basedir}/build.xml">
<target name="gen_hibernate" />
</ant>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
templatename[customtemplate.ftl]
为此,我们可以使用现有的daohome.ftl
,它可以在hibernate tools.jar
中找到${package name}\${classname}DAO.java
sessionFactoryName
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>install</phase>
<configuration>
<target>
<ant antfile="${basedir}/build.xml">
<target name="gen_hibernate" />
</ant>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>