用 Maven 运行 MyBatis Generator(Running MyBatis Generator W

Running MyBatis Generator With Maven

MBG(MyBatis Generator)包含一个 Maven 插件以集成到 Maven 构建当中。保持与 Maven 的常规配置一致,使得在 Maven 构建系统中包含 MBG 也变得简单多了。最小配置如下所示:

MyBatis Generator (MBG) includes a Maven plugin for integration into a maven build. In keeping with Maven’s configuration by convention strategy, including MBG in a Maven build can be very simple. The minimum configuration is shown below:

<project …>…<build>…<plugins>…<plugin><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-maven-plugin</artifactId><version>1.3.0</version></plugin>…</plugins>…</build>… </project>

当然,事情并不总是那么容易。

Of course, things are never that easy!

Maven 目标和执行Maven Goal and Execution

MBG Maven 插件包含一个目标:

The MBG Maven plugin includes one goal:

mybatis-generator:generate

该 目标不会被 Maven 自动执行。它可以通过两种方式执行。

The goal is not automatically executed by Maven. It can be executed in two ways.

目标可以从命令行使用命令执行:

The goal can be executed from the command line with the command:

mvn mybatis-generator:generate

使用标准的 Maven 命令行属性给目标传递参数。例如:

You can pass parameters to the goal with standard Maven command line properties.For example:

mvn -Dmybatis.generator.overwrite=true mybatis-generator:generate

这就会运行 MBG 并构建它来覆盖任何它能找到的的已存在的 Java 文件。

This will run MBG and instruct it to overwrite any existing Java files it may find.

在一个持续构建环境中,你可能想要自动执行 MBG,,使它作为 Maven 构建系统的一部分。这可能通过配置目标自动完成来实现。示例如下:

In a continuous build environment, you may want to automatically execute MBG as a part of a Maven build. This can be accomplished by configuring the goal to execute automatically. An example of this is shown below:

<project …>…<build>…<plugins>…<plugin><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-maven-plugin</artifactId><version>1.3.0</version><executions><execution><id>Generate MyBatis Artifacts</id><goals><goal>generate</goal></goals></execution></executions></plugin>…</plugins>…</build>… </project>

The MBG plugin is bound to the generate-sources phase of a Maven build, so it will execute before the complie step. Also note that MBG generates both Java source files and XML resources. The MBG goal will bind both generated Java files and XML resources to the build and they will both be included in any JAR generated by the build.

MyBatis Generator Configuration Properties

Any property specified in the POM will be passed into the configurationfile and may be used in the normal way. For example:

<project …>…<properties><dao.target.dir>src/main/java</dao.target.dir></properties>…<build>…<plugins>…<plugin><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-maven-plugin</artifactId><version>1.3.0</version><executions><execution><id>Generate MyBatis Artifacts</id><goals><goal>generate</goal></goals></execution></executions></plugin>…</plugins>…</build>… </project>

In this case, the property may be accessed in the configuration file with thesyntax ${dao.target.dir}.

Parameter Reference

All parameters are optional and most have suitable defaults.

Parameter Expression Type Comments

configurationFile ${mybatis.generator.configurationFile} java.io.File The location of the XML configuration file.

Default value:

${basedir}/src/main/resources/generatorConfig.xml

contexts ${mybatis.generator.contexts} java.lang.String A comma delimited list of contexts to use in the current run.Any id specified in the list must exactly match the value of the idattribute of an <context> configuration element.Only ids specified in this list will be active for this run.If this parameter is not specified, then all contexts will be active.

jdbcDriver ${mybatis.generator.jdbcDriver} java.lang.String If you specify a sqlScript, then this is thefully qualified JDBC driver class name to use when connecting to thedatabase.

jdbcPassword ${mybatis.generator.jdbcPassword} java.lang.String If you specify a sqlScript, then this is thepassword to use when connecting to the database.

jdbcURL ${mybatis.generator.jdbcURL} java.lang.String If you specify a sqlScript, then this is theJDBC URL to use when connecting to the database.

jdbcUserId ${mybatis.generator.jdbcUserId} java.lang.String If you specify a sqlScript, then this is theJDBC user ID to use when connecting to the database.

学习会使你永远立于不败之地。

用 Maven 运行 MyBatis Generator(Running MyBatis Generator W

相关文章:

你感兴趣的文章:

标签云: