用 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-08-09 12:24:57

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

用 Maven 运行 MyBatis Generator(Running MyBatis Generator With Maven)《IT蓝豹》

用 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

Java框架-MyBatis三剑客之MyBatis Generator(mybatis-generator MBG插件)详解

生成器设计思路: 连接数据库 -> 获取表结构 -> 生成文件 1 下载与安装 官网文档入口 最方便的 maven 插件使用方式 贴至pom 文件 2 新建配置文件 填充配置信息(官网示例) 项目实例 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Genera

【Java MyBatis Generator】使用generator自动生成Dao,Mapping和实体文件

具体请参照: http://blog.csdn.net/fengshizty/article/details/43086833 按照上面博客地址,下载Generator的依赖包: 如下是我的配置文件: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Co

mybatis自定义代码生成器(Generator)——自动生成model&amp;dao代码

花了两天的时间研究了下mybatis的generator大体了解了其生成原理以及实现过程.感觉generator做的非常不错,给开发者也留足了空间.看完之后在generator的基础上实现了自定义的生成器.代码start..... 建立了一个maven工程(common)项目结构: ----------------------------------------------------------------pom.xml-------------------------------------

Mybatis分页-利用Mybatis Generator插件生成基于数据库方言的分页语句,统计记录总数 (转)

众所周知,Mybatis本身没有提供基于数据库方言的分页功能,而是基于JDBC的游标分页,很容易出现性能问题.网上有很多分页的解决方案,不外乎是基于Mybatis本机的插件机制,通过拦截Sql做分页.但是在像Oracle这样的数据库上,拦截器生成的Sql语句没有变量绑定,而且每次语句的都要去拦截,感觉有点浪费性能. Mybatis Generator是Mybatis的代码生成工具,可以生成大部分的查询语句. 本文提供的分页解决方案是新增Mybatis Generator插件,在用Mybatis

mybatis学习笔记(六)使用generator生成mybatis基础配置代码和目录结构

原文:http://blog.csdn.net/oh_mourinho/article/details/51463413 创建maven项目 [java] view plain copy print? <span style="font-size:14px;"><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSc

SpringBoot学习--04SpringBoot整合Mybatis(上)(配置mybatis generator,PageHelper)

陆陆续续又忙了几天,继续写. 本篇仿照着优秀的文章的书写,加上自己的理解和踩过的坑,原文地址:https://www.jianshu.com/p/5cd772c07041?utm_campaign=haruki&utm_content=note&utm_medium=reader_share&utm_source=weixin 环境/版本一览: 开发工具:eclipse springboot: 2.0.1.RELEASE jdk:1.8.0_40 maven:3.3.9 额外功能:

Mybatis的逆向工程(generator)

Tips:Mybatis generator官网 http://www.mybatis.org/generator/configreference/commentGenerator.html Mybatis的逆向工程的作用是可由表帮我们生成bean,dao,xml映射文件 1. 引入Mybatis generator的jar包 获取jar包 将此jar包放到lib文件夹下,并将其build path 2.创建并获取generator配置文件 接下来,修改generator.xml配置文件,具体内

MyBatis之七:使用generator工具

可以将mybatis理解成一种半自动化orm框架,通过注解或者配置xml映射文件来手写相关sql语句,不能像我之前介绍orm的文章那样全对象化操作数据库增删改查.其实你会发现,手写配置xml映射文件是件很痛苦的事情,正因为如此,mybatis提供出了一个generator工具,只需要配置数据库连接字符串.对应数据库的驱动包,再执行固定格式的命令行语句即可轻松完事.generator的下载地址在:http://code.google.com/p/mybatis/wiki/Downloads?tm=