MyBatis Plus Plug in for generating Entiry Mapper.xml Mapper.class from database tables

  • 2021-06-28 12:49:43
  • OfStack

Create an maven project and modify the pom.xml file as follows:


<?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>
  <parent>
    <groupId>com.xxxx</groupId>
    <artifactId>parent-pom</artifactId>
    <version>1.0.0-SNAPSHOT</version>
  </parent>
  <groupId>com.xxxx</groupId>
  <artifactId>mapper-creator</artifactId>
  <version>1.0-SNAPSHOT</version>
  <properties>
    <configuration.outputDir>d:\demo-mapper-folder</configuration.outputDir>
    <dataSource.url>jdbc:mysql://192.168.18.140:8066/TESTDB?useUnicode=true&amp;characterEncoding=UTF-8</dataSource.url>
    <dataSource.username>root</dataSource.username>
    <dataSource.password>123456</dataSource.password>
    <packageInfo.parent>com.xxxx.demotwo</packageInfo.parent>
  </properties>
  <build>
    <plugins>
      <plugin>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatisplus-maven-plugin</artifactId>
        <version>1.0</version>
        <configuration>
          <!-- 输出目录(默认java.io.tmpdir) -->
          <outputDir>${configuration.outputDir}</outputDir>
          <!-- 是否覆盖同名文件(默认false) -->
          <fileOverride>true</fileOverride>
          <!-- mapper.xml 中添加2级缓存配置(默认true) -->
          <enableCache>true</enableCache>
          <!-- 开发者名称 -->
          <author>ZuoQuan Tu</author>
          <!-- 是否开启 ActiveRecord 模式(默认true) -->
          <activeRecord>false</activeRecord>
          <!-- 数据源配置,( **必配** ) -->
          <dataSource>
            <driverName>com.mysql.jdbc.Driver</driverName>
            <url>${dataSource.url}</url>
            <username>${dataSource.username}</username>
            <password>${dataSource.password}</password>
          </dataSource>
          <strategy>
            <!-- 字段生成策略,4种类型,从名称就能看出来含义:
              nochange(默认),
              underline_to_camel,(下划线转驼峰)
              remove_prefix,(去除第1个下划线的前部分,后面保持不变)
              remove_prefix_and_camel(去除第1个下划线的前部分,后面转驼峰) -->
            <naming>underline_to_camel</naming>
            <!-- 表前缀 -->
            <!--<tablePrefix>bmd_</tablePrefix>-->
            <!--Entity中的ID生成策略(默认 id_worker)-->
            <idGenType>uuid</idGenType>
            <!--自定义超类-->
            <!--<superServiceClass>com.baomidou.base.BaseService</superServiceClass>-->
            <!-- 要包含的表 与exclude 2选1配置-->
            <!--<include>-->
            <!--<property>sec_user</property>-->
            <!--<property>table1</property>-->
            <!--</include>-->
            <!-- 要排除的表 -->
            <!--<exclude>-->
            <!--<property>schema_version</property>-->
            <!--</exclude>-->
          </strategy>
          <packageInfo>
            <!-- 父级包名称,如果不写,下面的service等就需要写全包名(默认com.baomidou) -->
            <parent>${packageInfo.parent}</parent>
            <!--service包名(默认service)-->
            <service>service</service>
            <!--serviceImpl包名(默认service.impl)-->
            <serviceImpl>service.impl</serviceImpl>
            <!--entity包名(默认entity)-->
            <entity>entity</entity>
            <!--mapper包名(默认mapper)-->
            <mapper>mapper</mapper>
            <!--xml包名(默认mapper.xml)-->
            <xml>mapper</xml>
          </packageInfo>
          <template>
            <!-- 定义controller模板的路径 -->
            <!--<controller>/template/controller1.java.vm</controller>-->
          </template>
        </configuration>
        <dependencies>
          <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>
</project>

Project Run Steps

A, modify the values of each properties parameter in pom.xml to fit the configuration in your project

B, added in setting.xml of maven:


<pluginGroups>
  <pluginGroup>com.baomidou</pluginGroup>
</pluginGroups>

C, execute the following maven command:

mvn mp:code

After execution, you will see a folder pop-up containing the tables to be generated, such as Entity, mapper, mapper.xml, etc.

summary


Related articles: