簡體   English   中英

自定義休眠工具導出器

[英]Custom hibernate tool exporter

我使用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”后綴

您是否知道是否可以通過某種方式提供自定義的Exporter實現或在插件中進行配置以實現此目的?

謝謝

最后,我使用JD-gui來反編譯並讀取插件的代碼,並且設法使用目標模板hbmtemplate每次運行2次插件,每次使用不同的模板進行操作:一個用於實體,一個用於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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM