用 Maven 运行 MyBatis Generator(Running MyBatis Generator With Maven)

用 Maven 运行 MyBatis Generator(Running MyBatis Generator With Maven)

太阳火神的美丽人生 (http://blog.csdn.net/opengl_es)

本文遵循“署名-非商业用途-保持一致”创作公用协议

转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究: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:

  • 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 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.

时间: 2024-11-01 06:16:12

用 Maven 运行 MyBatis Generator(Running MyBatis Generator With Maven)的相关文章

Java的MyBatis框架中MyBatis Generator代码生成器的用法_java

关于Mybatis GeneratorMyBatis Generator (MBG) 是一个Mybatis的代码生成器 MyBatis 和 iBATIS. 他可以生成Mybatis各个版本的代码,和iBATIS 2.2.0版本以后的代码. 他可以内省数据库的表(或多个表)然后生成可以用来访问(多个)表的基础对象. 这样和数据库表进行交互时不需要创建对象和配置文件. MBG的解决了对数据库操作有最大影响的一些简单的CRUD(插入,查询,更新,删除)操作. 您仍然需要对联合查询和存储过程手写SQL和

【MyBatis框架】mybatis逆向工程自动生成代码

逆向工程 1.什么是逆向工程 mybaits需要程序员自己编写sql语句,mybatis官方提供逆向工程 可以针对单表自动生成mybatis执行所需要的代码(mapper.java,mapper.xml.po..) 企业实际开发中,常用的逆向工程方式: 由于数据库的表生成java代码. 2.下载逆向工程mybatis-generator-core-1.3.2-bundle.zip 3.使用方法(会用) 3.1运行逆向工程  官方文档中提供的运行逆向工程的几种方法 Running MyBatis

MyBatis框架之mybatis逆向工程自动生成代码_java

Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成文件. 逆向工程 1.什么是逆向工程 mybaits需要程序员自己编写sql语句,mybatis官方提供逆向工程 可以针对单表自动生成mybatis执行所需要的代码(mapper.java,mapper.xml.po..) 企业实际开发中,常用的逆向工程方式: 由于数据库的表生成java代码. 2.下载逆向工程 my

mybatis实战教程(mybatis in action),mybatis入门到精通

 目录(?)[-] mybatis实战教程mybatis in action之一开发环境搭建 mybatis实战教程mybatis in action之二以接口的方式编程 mybatis实战教程mybatis in action之三实现数据的增删改查 mybatis实战教程mybatis in action之四实现关联数据的查询 mybatis实战教程mybatis in action之五与spring3集成附源码 mybatis实战教程mybatis in action之六与Spring

jetty maven运行时 出现503错误 打不开页面 但是后台显示jetty容器正常

问题描述 jetty maven运行时 出现503错误 打不开页面 但是后台显示jetty容器正常 jetty maven运行时 出现503错误 打不开页面 但是后台显示jetty容器正常

maven运行出错,求大神帮忙解决

问题描述 maven运行出错,求大神帮忙解决 java.lang.ArrayIndexOutOfBoundsException: 24444 at org.objectweb.asm.ClassReader.(Unknown Source) at org.objectweb.asm.ClassReader.(Unknown Source) at org.objectweb.asm.ClassReader.(Unknown Source) at org.eclipse.jetty.annotati

《maven官方文档》5分钟开始Maven

前提 你必须明白如何在电脑上安装软件.如果你不知道如何做,请向你学校.办公室里等的人请教下,或者付费给他人让他们解释给你.Maven邮件组不是寻求这个建议的最好地方. 安装 Maven是个Java工具,所以你必须安装了Java才能继续.首先,下载Maven,然后按照安装指南.完成后,在终端或命令行中键入如下指令: mvn --version 应该会打印已安装Maven的版本.例如: Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf96

[转]Maven如何手动添加jar包到本地Maven仓库

Apache Maven,是一个软件(特别是Java软件)项目管理及自动构建工具,由Apache软件基金会所提供.基于项目对象模型(缩写:POM)概念,Maven利用一个中央信息片断能管理一个项目的构建.报告和文档等步骤.曾是Jakarta项目的子项目,现为独立Apache项目. 大家肯定遇到过想在pom文件中加入自己开发的依赖包,这些包肯定是不是在Maven仓库(http://repo1.maven.org/maven2/)的.那我们怎么将那些不存在Maven仓库中的包加入到本地的Mave

我在Eclipse中导入maven项目报错(就配置了maven仓库)大神支招

问题描述 严重:Errorconfiguringapplicationlistenerofclassorg.springframework.web.context.ContextLoaderListenerjava.lang.ClassNotFoundException:org.springframework.web.context.ContextLoaderListeneratorg.apache.catalina.loader.WebappClassLoader.loadClass(Weba