MyBatis Generator

本文介绍MyBatis Generator的配置方法与多种运行方式,包括通过命令行、Maven插件、Java代码及Eclipse插件等。并提供了一个详细的generatorConfig.xml配置示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一 概述

官方文档

二 XML配置文件generatorConfig.xml

官方文档

generatorConfig.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
  <!--数据库驱动-->
  <classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" />

  <context id="DB2Tables" targetRuntime="MyBatis3">
    <!--数据库链接地址账号密码-->
    <jdbcConnection driverClass="COM.ibm.db2.jdbc.app.DB2Driver"
        connectionURL="jdbc:db2:TEST"
        userId="db2admin"
        password="db2admin">
    </jdbcConnection>

    <javaTypeResolver >
      <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>

    <!--生成Model类存放位置-->
    <javaModelGenerator targetPackage="test.model" targetProject="\MBGTestProject\src">
      <property name="enableSubPackages" value="true" />
      <property name="trimStrings" value="true" />
    </javaModelGenerator>

    <!--生成映射文件存放位置-->
    <sqlMapGenerator targetPackage="test.xml"  targetProject="\MBGTestProject\src">
      <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>

    <!--生成Dao类存放位置-->
    <javaClientGenerator type="XMLMAPPER" targetPackage="test.dao"  targetProject="\MBGTestProject\src">
      <property name="enableSubPackages" value="true" />
    </javaClientGenerator>

    <!--生成对应表及类名-->
    <table schema="DB2ADMIN" tableName="ALLTYPES" domainObjectName="Customer" >
      <property name="useActualColumnNames" value="true"/>
      <generatedKey column="ID" sqlStatement="DB2" identity="true" />
      <columnOverride column="DATE_FIELD" property="startDate" />
      <ignoreColumn column="FRED" />
      <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
    </table>

  </context>
</generatorConfiguration>

三 生成代码的不同方式

A. From the Command Line

java -jar mybatis-generator-core-x.x.x.jar -configfile \temp\generatorConfig.xml -overwrite

B. Running MyBatis Generator With Maven

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

Maven Goal and Execution

The MBG Maven plugin includes one goal:

mybatis-generator:generate

goal 不是由Maven自动执行。它可以通过两种方式来执行. The goal can be executed from the command line with the command:

mvn mybatis-generator:generate
mvn -Dmybatis.generator.overwrite=true mybatis-generator:generate
<plugin>
  <groupId>org.mybatis.generator</groupId>
  <artifactId>mybatis-generator-maven-plugin</artifactId>
  <version>1.3.7</version>
  <executions>
    <execution>
      <id>Generate MyBatis Artifacts</id>
      <goals>
        <goal>generate</goal>
      </goals>
    </execution>
  </executions>
</plugin>

C. Running MyBatis Generator With Java

from Java with an XML Configuration File

List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);

DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);

from Java with a Java Based Configuration

List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
Configuration config = new Configuration();

//   ... fill out the config object as appropriate...

DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);

D. Running MyBatis Generator with Eclipse

Eclipse Plugin

拖放到运行的 Eclipse 中. *Requires Eclipse Marketplace Client

 

org.mybatis.generator.eclipse.site-1.3.7.201807042148.zip

Using the Eclipse Feature

To get up and running quickly with MyBatis Generator in the eclipse environment, follow these steps:

  1. Create a configuration file:
    1. File>New>Other...
    2. Select "MyBatis Generator Configuration File" from the "MyBatis" category, then press "Next"
    3. Specify the location of the file, and the name of the file (defaults to generatorConfig.xml)
    4. Finish the wizard
  2. Fill out the configuration file appropriately. At a minimum, you must specify:
    1. jdbcConnection information to specify how to connect to the target database
    2. A target package, and target project for the javaModelGenerator
    3. A target package, and target project for the sqlMapGenerator
    4. A target package, target project, and type for the javaClientGenerator (or you can remove the javaClientGenerator element if you don't wish to generate Java Clients)
    5. At least one database table
  3. Save the file
  4. Right click on the configuration file in Eclipse's navigator, or package explorer, view and take the menu option "Run As>Run MyBatis Generator"
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
  <classPathEntry location="D:\apache-maven-3.5.4\repo\com\oracle\ojdbc\6\ojdbc-6.jar"/>
  <context id="context1">
    <jdbcConnection connectionURL="jdbc:oracle:thin:@localhost:1521:orcl"
    	driverClass="oracle.jdbc.OracleDriver" password="dev" userId="dev" />
    <javaModelGenerator targetPackage="com.neusoft.demo1.sys.model" targetProject="sys\src\main\java" />

    <sqlMapGenerator targetPackage="com.neusoft.demo1.sys.mapper" targetProject="sys\src\main\resources" />
    <javaClientGenerator targetPackage="com.neusoft.demo1.sys.mapper" targetProject="sys\src\main\java" type="XMLMAPPER" />
    <!-- enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false" -->
    <table schema="dev" tableName="T_USER">
      <property name="useActualColumnNames" value="false"/>
      <domainObjectRenamingRule searchString="^T" replaceString=""/>
      <columnOverride column="USER_ID" property="id" />
    </table>
  </context>
</generatorConfiguration>

参考文档:| Eclipse Plugins, Bundles and Products - Eclipse Marketplace

MyBatis Generator Core – MyBatis Generator XML Configuration File Reference

MyBatis Generator Core – Introduction to MyBatis Generator

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值