Mybatis Generator使用教程

一.MyBatis Generator简介
MyBatis Generator(MBG)是MyBatis MyBatis 和iBATIS的代码生成器。它将为所有版本的MyBatis以及版本2.2.0之后的iBATIS版本生成代码。它将内省数据库表(或许多表),并将生成可用于访问表的工件。这减少了设置对象和配置文件以与数据库表交互的初始麻烦。MBG寻求对简单CRUD(创建,检索,更新,删除)的大部分数据库操作产生重大影响。您仍然需要为连接查询或存储过程手动编写SQL和对象代码。

MyBatis Generator将生成:

与表结构匹配的Java POJO。这可能包括:

  • 一个匹配表的主键的类(如果有主键)
  • 一个匹配表的非主键字段的类(BLOB字段除外)
  • 包含表的BLOB字段的类(如果表具有BLOB字段)
  • 用于启用动态选择,更新和删除的类

这些类之间存在适当的继承关系。请注意,生成器可以配置为生成不同类型的POJO层次结构 - 例如,如果您愿意,可以选择为每个表生成单个域对象。

MyBatis / iBATIS兼容的SQL Map XML文件。MBG在配置中的每个表上为简单的CRUD函数生成SQL。生成的SQL语句包括:

  • 插入
  • 按主键更新
  • 通过示例更新(使用动态where子句)
  • 按主键删除
  • 按示例删除(使用动态where子句)
  • 按主键选择
  • 按示例选择(使用动态where子句)
  • 以身作则

根据表的结构,这些语句有不同的变体(例如,如果表没有主键,则MBG不会通过主键功能生成更新)。

适当使用上述对象的Java客户端类。Java客户端类的生成是可选的。MBG将为MyBatis 3.x生成以下类型的Java客户端:

  • 适用于MyBatis 3.x映射器基础结构的映射器接口
  • MBG将为iBATIS 2.x生成以下类型的Java客户端:
  • 符合Spring框架的DAO
  • 仅使用iBATIS SQL映射API的DAO。这些DAO可以生成两种:通过构造函数或setter注入提供SqlMapClient。
  • 符合iBATIS DAO框架的DAO(iBATIS的可选部分,现在不推荐使用此框架,我们建议您使用Spring框架)

MyBatis生成器设计为在迭代开发环境中运行良好,并且可以作为Ant任务或Maven插件包含在连续构建环境中。迭代运行MBG时需要注意的重要事项包括:

  1. 如果存在与新生成的XML文件同名的现有文件,MBG将自动合并XML文件。MBG不会覆盖您对其生成的XML文件所做的任何自定义更改。您可以反复运行它,而不必担心会丢失对XML的自定义更改。MBG将替换先前运行中生成的任何XML元素。
  2. MBG 不会合并Java文件,它可以覆盖现有文件或使用不同的唯一名称保存新生成的文件。如果对生成的Java文件进行更改并以迭代方式运行MBG,则必须手动合并更改。当作为Eclipse 插件运行时 ,MBG可以自动合并Java文件。

二.MyBatis Generator下载

     1.源码地址:https://github.com/mybatis/generator/releases

     2.官方文档:http://www.mybatis.org/generator/index.html

三.Mybatis Generator配置和使用

      1.创建maven项目

     

      2.配置Mybatis Generator的依赖和插件 maven的 pom.xml文件配置

      

<?xml version="1.0" encoding="UTF-8"?>
 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
 
  <groupId>com.mybatis.generator</groupId>
  <artifactId>mybatis-generator</artifactId>
  <version>1.0-SNAPSHOT</version>
 
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
 
  <dependencies>
 
  </dependencies>
 
  <build>
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
 
        <plugin>
          <groupId>org.mybatis.generator</groupId>
          <artifactId>mybatis-generator-maven-plugin</artifactId>
          <version>1.3.7</version>
          <configuration>
            <configurationFile>src/main/resources/mybatis-generator/generatorConfig.xml</configurationFile>
            <verbose>true</verbose>
            <overwrite>true</overwrite>
          </configuration>
          <executions>
            <execution>
              <id>Generate MyBatis Artifacts</id>
              <goals>
                <goal>generate</goal>
              </goals>
            </execution>
          </executions>
          <dependencies>
            <dependency>
              <groupId>org.mybatis.generator</groupId>
              <artifactId>mybatis-generator-core</artifactId>
              <version>1.3.7</version>
            </dependency>
          </dependencies>
        </plugin>
      </plugins>
  </build>
</project>


3.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="E:/GARBAGE_TEMP_SPACE/mysql-connector-java-5.1.46.jar"/>
    <!---Mybatis上下文-->
    <context id="MySqlContext" targetRuntime="MyBatis3">
        <!--配置数据库的链接信息-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://192.168.2.40:3306/test?useSSL=true" userId="root"
                        password="ggg123"/>
        <!--数据库BigDecimals字段在java中定义-->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!--实体类生成信息-->
        <javaModelGenerator targetPackage="com.lpinfo.frame.domain" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--mybatis 的xml文件地址-->
        <sqlMapGenerator targetPackage="com.lpinfo.frame.mybatis" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!--mybatis的mapper接口-->
        <javaClientGenerator targetPackage="com.lpinfo.frame.mapper" targetProject="src/main/java" type="XMLMAPPER">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!--要生成的表结构-->
        <table tableName="lpinfo_user" domainObjectName="User"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
 
    </context>
 
</generatorConfiguration>


4.执行mybatis-generator插件


发布了47 篇原创文章 · 获赞 223 · 访问量 37万+

猜你喜欢

转载自blog.csdn.net/ccblogger/article/details/104308813