標籤:android includes 美麗人生 文章 running
用 Maven 運行 MyBatis Generator(Running MyBatis Generator With Maven)
IT藍豹:http://itlanbao.com/codes.aspx#1,0
轉載請保留此句:太陽火神的美麗人生 - 本部落格專註於 敏捷開發及移動和物聯裝置研究:iOS、Android、Html5、Arduino、pcDuino,否則,出自本部落格的文章拒絕轉載或再轉載,謝謝合作。
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:
該 目標不會被 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:
使用標準的 Maven 命令列屬性給目標傳遞參數。例如:
You can pass parameters to the goal with standard Maven command line properties.For example:
這就會運行 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 id attribute 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 the fully qualified JDBC driver class name to use when connecting to the database. |
| jdbcPassword |
${mybatis.generator.jdbcPassword} |
java.lang.String |
If you specify a sqlScript, then this is the password to use when connecting to the database. |
| jdbcURL |
${mybatis.generator.jdbcURL} |
java.lang.String |
If you specify a sqlScript, then this is the JDBC URL to use when connecting to the database. |
| jdbcUserId |
${mybatis.generator.jdbcUserId} |
java.lang.String |
If you specify a sqlScript, then this is the JDBC user ID to use when connecting to the database. |
| outputDirectory |
${mybatis.generator.outputDirectory} |
java.io.File |
The directory where files generated by MBG will be placed. This directory is used whenever a targetProject in the configuration file is set to the special value "MAVEN" (case sensitive).
Default value: ${project.build.directory}/generated-sources/mybatis-generator |
| overwrite |
${mybatis.generator.overwrite} |
boolean |
If true, then existing Java files will be overwritten if an existing Java file if found with the same name as a generated file. If not specified, and a Java file already exists with the same name as a generated file, then MBG will write the newly generated Java file to the proper directory with a unique name (e.g. MyClass.java.1, MyClass.java.2, etc.). Important: MBG will always merge and overwrite XML files.
Default value: false |
| sqlScript |
${mybatis.generator.sqlScript} |
java.lang.String |
Location of a SQL script file to run before generating code. If null, then no script will be run. If not null, then jdbcDriver, jdbcURL must be supplied also. In addition, jdbcUserId and jdbcPassword may be supplied if the database requires authentication.
Value can be specified as a location in the file system or, if prefixed with "classpath:" a location on the build classpath. |
| tableNames |
${mybatis.generator.tableNames} |
java.lang.String |
If specified, then this is a comma delimited list of tables to use in the current run. Any table specified in the list must exactly match the fully qualified table name specified in a <table> configuration element. Only tables specified in this list will be active for this run. If this argument is not specified, then all tables will be active. Specify table names as:
table
schema.table
catalog..table
etc. |
| verbose |
${mybatis.generator.verbose} |
boolean |
If true, then MBG will write progress messages to the build log. |
Interpretation of targetProject
The targetProject attribute of the generator configurations is interpreteddifferently when running with Maven. If set to the special value "MAVEN" (casesensitive), then targetProject will be set to the plugin‘s output directory,and that directory will be created if it doesn‘t already exist. If not set to "MAVEN", thentargetProject will be interpreted as normal by MBG - it must be setto a directory that already exists.
轉載請證明出處:http://itlanbao.com/codes.aspx#1,0
用 Maven 運行 MyBatis Generator(Running MyBatis Generator With Maven)《IT藍豹》