Run MyBatis Generator using maven (Running MyBatis Generator with maven)
The beautiful Life of the Sun Vulcan (http://blog.csdn.net/opengl_es)
This article follows "Attribution-non-commercial use-consistent" authoring public agreement
Reprint Please keep this sentence: Sun Vulcan's Beautiful Life-this blog focuses on Agile development and mobile and IoT device research: IOS, Android, HTML5, Arduino, Pcduino , Otherwise, the article from this blog refused to reprint or re-reproduced, thank you for your cooperation.
Running MyBatis Generator with Maven
MBG (MyBatis Generator) contains a MAVEN plugin to integrate into the MAVEN build. Maintaining consistency with MAVEN's general configuration makes it much easier to include MBG in a Maven build system. The minimum configuration is as follows:
MyBatis Generator (MBG) includes a MAVEN plugin for integration into a MAVEN build. In keeping with Maven's configuration by convention strategy, the including MBG in a Maven build can is 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 aren't always that easy.
Of course, things is never that easy!
Maven goals and execution
Maven Goal and execution
The MBG Maven plugin contains a target:
The MBG Maven plugin includes one goal:
- Mybatis-generator:generate
The target will not be automatically executed by Maven. It can be executed in two ways.
The goal isn't automatically executed by Maven. It can be executed in the ways.
The target can be executed from the command line using the command:
The goal can executed from the command line with the command:
- MVN mybatis-generator:generate
Pass parameters to the target using the standard Maven command-line properties. For example:
You can pass parameters to the goal with standard Maven command line properties. For example:
- Mvn-dmybatis.generator.overwrite=true mybatis-generator:generate
This will run MBG and build it to overwrite any existing Java files it can find.
This would run MBG and instruct it to overwrite any existing the Java files it may find.
In a continuous build environment, you might want to automate MBG to make it part of the Maven build system. This can be achieved by configuring the target auto-completion. Examples are as follows:
In a continuous build environment, the want to automatically execute MBG as a part of a Maven build. This can is accomplished by configuring the goal to execute automatically. An example of 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'll execute before the Complie Ste P. Also Note that MBG generates both Java source files and XML resources. The MBG goal would bind both generated Java files and XML resources to the build and they would both be included in any JAR Generated by the build.
MyBatis Generator Configuration Properties
The specified in the POM would be passed into the configurationfile and is used in the normal. 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 is accessed in the configuration file with Thesyntax ${dao.target.dir}.
Parameter Reference
All parameters is optional and most of them 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 the current run. Any ID specified in the list must exactly match the value of the the id attribute of a <context> configuration element. Only the IDs specified in this list would be active for this run. If This parameter was not specified and then all contexts would be active. |
Jdbcdriver |
${mybatis.generator.jdbcdriver} |
Java.lang.String |
If you specify a sqlscriptand 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 sqlscriptand 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 the the-is the JDBC URL of the connecting to the database. |
Jdbcuserid |
${mybatis.generator.jdbcuserid} |
Java.lang.String |
If you are specify a sqlscript, then this is the JDBC user ID of the when connecting to the database. |
outputdirectory |
${mybatis.generator.outputdirectory} | TD valign= "Top" >java.io.file
the directory where files generated by MBG would be placed. This directory was used whenever a targetproject in the configuration file was set to the special value "MAVEN" (ca Se sensitive). Default value: ${project.build.directory}/generated-sources/mybatis-generator |
overwrite |
${mybatis.generator.overwrite} |
boolean |
if true, then existing Java files would be overwritten if a existing Java file if Fou nd 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 would write the newly gen erated Java file to the proper directory with a unique name (e.g. myclass.java.1, myclass.java.2, etc.). IMPORTANT:MBG would 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 would be run. If NOT NULL, then jdbcdriver , Jdbcurl must is supplied also. In addition, Jdbcuserid and Jdbcpassword is supplied if the database requires authentication. Value can be specified as a at the file system or, if prefixed with "classpath:" A location on the build class Path. |
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> Configurati on element. Only tables specified in the This list would be the active for this run. If This argument was not specified and then all tables would be active. Specify table names as: Table Schema.table Catalog.. Table etc. |
Verbose |
${mybatis.generator.verbose} |
Boolean |
If true, then MBG would 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) and then Targetproject 'll be set to the plugin ' s output direct Ory,and that directory would be created if it doesn ' t already exist. If not set to "MAVEN", thentargetproject 'll be interpreted as normal by mbg-it must is setto a directory that already exists.
Run MyBatis Generator using maven (Running MyBatis Generator with maven)