step 1 : 配置maven的策略

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>${exec.maven.plugin.version}</version>
    <executions>
        <execution>
            <phase>prepare-package</phase>
            <goals>
                <goal>java</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <mainClass>com.test.SchemaGenerator</mainClass>
        <arguments>
            <argument>com.test.db.entity</argument>
            <argument>${project.basedir}/sql/</argument>
            <argument>test.sql</argument>
        </arguments>
    </configuration>
</plugin>

step 2 :实现策略操作:

Configuration cfg;
cfg = new Configuration();
cfg.setProperty("hibernate.hbm2ddl.auto", "create");// 从包中得到所有相关class
File directory = null;
ClassLoader cld = Thread.currentThread().getContextClassLoader();
URL resource = getResource(packageName, cld);
directory = new File(resource.getFile());
Class.forName(className)
for clazz in classes
cfg.addAnnotatedClass(clazz);
cfg.setProperty("hibernate.dialect","org.hibernate.dialect.MySQLDialect");
SchemaExport export = new SchemaExport(cfg);
export.setDelimiter(":");
export.setOutputFile(directory + fileName);
export.setFormat(true);
export.execute(true, false, false, false);

posted on 2018-04-19 02:34  byso  阅读(379)  评论(0编辑  收藏  举报