【问题标题】:Custom hibernate tool exporter自定义休眠工具导出器
【发布时间】:2013-12-18 15:53:58
【问题描述】:

我使用maven插件生成pojo和dao:

<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.2</version>
<executions>
    <execution>
        <phase>generate-sources</phase>
        <goals>
            <goal>hbm2java</goal>
            <goal>hbm2dao</goal> 
            <goal>hbm2ddl</goal>
        </goals>
    </execution>
</executions>
<configuration>
    <components>
        <component>
            <name>hbm2java</name>
            <implementation>configuration</implementation>
            <outputDirectory>src/main/java</outputDirectory>
        </component>
        <component>
            <name>hbm2dao</name>
            <implementation>configuration</implementation>
            <outputDirectory>src/main/java</outputDirectory>
        </component>
        <component>
            <name>hbm2ddl</name>
            <implementation>configuration</implementation>
            <outputDirectory>src/main/resources/sql</outputDirectory>
        </component>
    </components>
    <componentProperties>
        <jdk5>true</jdk5>
        <format>true</format>
        <configurationfile>src/main/resources/hibernate/hibernate.cfg.xml</configurationfile>

        <drop>true</drop>
        <create>true</create>
        <outputfilename>init.sql</outputfilename>
        <templatepath>src/main/resources/templateftl</templatepath>

    </componentProperties>
</configuration>

dao 和 pojo 是在同一个包中生成的

在休眠工具中它是硬编码的

DAOExporter :  setFilePattern("{package-name}/{class-name}Home.java");
POJOExporter : setFilePattern("{package-name}/{class-name}.java");

我觉得它非常难看,我想把 pojo 和 dao 放在不同的包里,而且不要在 Dao 后面加上“Home”,而只是“Dao”

您是否知道是否有某种方法可以提供自定义导出器实现或在插件中配置某些东西来实现这一点?

谢谢

【问题讨论】:

    标签: hibernate maven hibernate-tools hibernate3-maven-plugin


    【解决方案1】:

    最后,我使用 JD-gui 反编译并读取插件的代码,并且我设法运行了 2 次插件,目标是 hbmtemplate,每次使用不同的模板:一个用于实体,一个用于 dao。

    我发现可以通过在 exporterclass 中指定自定义导出器来使用它。 maven 插件缺少一些 xsd 之王...

        <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>hibernate3-maven-plugin</artifactId>
        <version>2.2</version>
        <executions>
            <execution>
                <phase>generate-sources</phase>
                <id>genpojo</id>
                <goals>
                    <goal>hbmtemplate</goal> 
                    <goal>hbm2ddl</goal>
                </goals>
                <configuration>
                <components>
                    <component>
                        <name>hbmtemplate</name>
                        <implementation>configuration</implementation>
                        <outputDirectory>src/main/java</outputDirectory>
                    </component>                        
                    <component>
                        <name>hbm2ddl</name>
                        <implementation>configuration</implementation>
                        <outputDirectory>src/main/resources/sql</outputDirectory>
                    </component>
                </components>
                <componentProperties>
                    <jdk5>true</jdk5>
                    <format>true</format>
                    <configurationfile>src/main/resources/hibernate/hibernate.cfg.xml</configurationfile>
                    <drop>true</drop>
                    <create>true</create>
                    <outputfilename>init.sql</outputfilename>
                    <templatepath>src/main/resources/templateftl</templatepath>
                    <filepattern>{package-name}/pojogen/{class-name}.java</filepattern>
                    <template>pojo/Pojo.ftl</template>
                </componentProperties>
            </configuration>
            </execution>
            <execution>
                <phase>generate-sources</phase>
                <id>gendao</id>
                <goals>                         
                    <goal>hbmtemplate</goal>
                </goals>
                <configuration>
                <components>                        
                     <component>
                        <name>hbmtemplate</name>
                        <implementation>configuration</implementation>
                        <outputDirectory>src/main/java</outputDirectory>
                    </component>
                </components>
                <componentProperties>
                    <jdk5>true</jdk5>
                    <format>true</format>
                    <configurationfile>src/main/resources/hibernate/hibernate.cfg.xml</configurationfile>
                    <drop>true</drop>
                    <create>true</create>
                    <templatepath>src/main/resources/templateftl</templatepath>
                    <!--  <exporterclass>com.db.exporter.MyDAOExporter</exporterclass>-->
                    <filepattern>{package-name}/daogen/{class-name}Dao.java</filepattern>
                    <template>dao/daohome.ftl</template>
                </componentProperties>
            </configuration>
            </execution>
        </executions>
    
    </plugin>
    

    【讨论】:

      猜你喜欢
      • 2014-12-05
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多