IDEA中使用Mybatis Generator快速生成Mybatis代码

本文演示如何在IDEA中使用"Mapper 专用代码生成器"生成实体类,Mapper接口以及相应的XML文件。

本文讲解如何在IDEA中使用Mapper 专用代码生成器生成实体类,Mapper接口以及相应的XML文件。

该插件基于Mybatis Generator(MBG)

MBG详解:

https://blog.csdn.net/isea533/article/details/42102297

本文使用Java编码方式运行MBG

1. 在IDEA的Project新建module

新建moudle
新建moudle

2. 配置pom.xml中的依赖

pom.xml中加入如下依赖:

maven依赖库:https://mvnrepository.com/

<?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.damocles</groupId>
  <artifactId>flash-mybatis</artifactId>
  <version>1.0-SNAPSHOT</version>

  <dependencies>
    <!--mysql-connector-java-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.15</version>
    </dependency>

    <!--mybatis-generator-core-->
    <dependency>
      <groupId>org.mybatis.generator</groupId>
      <artifactId>mybatis-generator-core</artifactId>
      <version>1.3.7</version>
    </dependency>

    <!--mapper generator整合在其中了-->
    <dependency>
      <groupId>tk.mybatis</groupId>
      <artifactId>mapper</artifactId>
      <version>4.1.5</version>
    </dependency>
  </dependencies>

</project>

3. 配置generatorConfig.xml

在moudle下创建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>
  <context id="MysqlContext" targetRuntime="MyBatis3Simple" defaultModelType="flat">
    <property name="beginningDelimiter" value="`"/>
    <property name="endingDelimiter" value="`"/>
    <!--指定生成的xxxMapper文件要继承的MyMapper类的路径-->
    <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
      <!--指定MyMapper接口的位置-->
      <property name="mappers" value="com.damocles.utils.MyMapper"/>
    </plugin>
    <!--连接数据库的URL,用户名和密码-->
    <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
      connectionURL="jdbc:mysql://localhost:8889/flash-dev"
      userId="root"
      password="root">
    </jdbcConnection>

    <!-- 对应生成的pojo -->
    <javaModelGenerator targetPackage="com.damocles.pojo"
      targetProject="flash-mybatis/src/main/java"/>

    <!-- 对应生成的mapper.xml所在目录 -->
    <sqlMapGenerator targetPackage="mapper"
      targetProject="flash-mybatis/src/main/resources"/>

    <!-- 配置mapper对应的java映射 -->
    <javaClientGenerator targetPackage="com.damocles.mapper"
      targetProject="flash-mybatis/src/main/java"
      type="XMLMAPPER"/>

    <!--对应的数据库表-->
    <table tableName="chat_msg">
    </table>
    <table tableName="users">
    </table>
    <table tableName="my_friends">
    </table>
    <table tableName="friends_request">
    </table>


  </context>
</generatorConfiguration>

4. 添加MyMapper类

这里最关键的就是MyMapper类,配置后生成的java映射都会继承该接口

package com.damocles.utils;

import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;

public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> {
  // TODO
}

5. 添加执行的java类

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


public class GeneratorDisplay {

  public void generator() throws Exception {

    List<String> warnings = new ArrayList<String>();
    boolean overwrite = true;
    //指向前面定义的配置文件generatorConfig.xml路径
    File configFile = new File("flash-mybatis/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);

  }
  public static void main(String[] args) throws Exception {
    try {
      GeneratorDisplay generatorSqlmap = new GeneratorDisplay();
      generatorSqlmap.generator();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

6. 结果

执行上一步的java类后,自动生成代码成功如下:

生成结果
生成结果

7. 可能出现的问题

7.1 mysql-connector-java 8.0驱动生成同名表问题

在使用mapper generator生成pojo等文件的时候,我发现表users生成出现错误,生成了mysql下的users表….

解决方案如下:

generatorConfig.xmljdbcConnection标签内加入:

<property name="nullCatalogMeansCurrent" value="true"/>

然后为每个数据表添加schema字段指定数据库:

<table schema="specified_schema" tableName="your_table_name"></table>

参考资料:

[1] https://juejin.im/post/5c5d8855e51d452a961143a4

[2] Mapper插件的GitHub

添加新评论