Personally, I don’t think there is much difference between using Mybatis-Plus and not using it. I don’t think it can help us improve efficiency.

Mybatis also provides its own code generator, using the code generator to quickly generate some general and commonly used Sql can also replace Mybatis-Plus BaseMapper. Mybatis-Plus generates Mapper and XML files more succinctly.

With the classic four-tier architecture of DDD domain-driven design, other features provided by Mybatis-Plus are largely eliminated.

What I most want to make fun of is the code generator of Mybatis-Plus. It forced me to generate Service, ServiceImpl and Controller without asking me if I needed them. At the beginning, I thought it would not be generated without configuration, but it turned out that I was naive.

Among other things, is it really necessary to generate a Service for each Mapper? Since when do we get used to having a table for a Service?

The extra classes generated were unnecessary in a four-tier DDD project, so I rewrote the template generation engine myself to remove the annoying Service, ServiceImpl, and Controller.

easy-mybatis-plus-generator

Easy-mybatis -Plus-Generator is a mybatis -Plus code generator based on mybatis -Plus-Generator, which replaces AbstractTemplateEngine.

Easy-mybatis – PlUS-Generator is a sub-module of easyMulti-datasource – Spring-boot-starter.

Github link: github.com/wujiuye/eas…

The premise of using easy-Mybatis – plus-Generator

  • 1. You want to use it through the configuration file.
  • 2, do not want to generateService,ServiceImpl,ControllerClass;
  • 3. Accept some default configurationsxmlAutomatic file generationbaseResultMap, do not enable level 2 cache, uselombokAutomatic hump mapping of column names;

About version Number

  • 2.xVersion: Correspondingmybatis-plus3.xVersion, also corresponds tomybatis-plus-generator3.xVersion, latest version:2.0.1 - RELEASE;

use

  • Step 1: Add dependencies
<! -- easy-mybatis-plus-generator -->
<dependency>
    <groupId>com.github.wujiuye</groupId>
    <artifactId>easy-mybatis-plus-generator</artifactId>
    <version>2.0.1 - RELEASE</version>
</dependency>
Copy the code

Of course, you also need a JDBC driver package for your database, such as mysql

<! -- mysql -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.29</version>
    <scope>runtime</scope>
</dependency>
Copy the code
  • Step 2: Add the configuration

Add the mybatis-plus-generator.properties configuration file to the resources directory

Data source configuration
datasource.jdbc.url=
datasource.jdbc.driverName=com.mysql.jdbc.Driver
datasource.jdbc.username=
datasource.jdbc.password=
Global configuration
globalconfig.author=mybatis-plus
globalconfig.outputDir=easy-mybatis-plus-generator/test/src/main/java
globalconfig.entityName=%sPO
globalconfig.mapperName=%sMapper
globalconfig.xmlName=%sMapper
Copy the code
  • Step 3: Write code

Write the test class, or write a main method that calls the EasyMybatisGenerator#run method to generate code as follows.

public class EasyGeneratorTest {

    public static void main(String[] args) throws Exception {
        // Configure the package information
        PackageConfig config = new PackageConfig()
                .setParent("com.github.wujiuye.generator")
                .setEntity("repo.dao.po")
                .setMapper("repo.dao")
                .setXml("repo.dao");
        // Start generating code
        EasyMybatisGenerator.run(config,"pay_config_rec"); }}Copy the code

EasyMybatisGenerator#run

  • packageConfigGenerated:Entity,MappperThe package information is also generatedEntity,Mappper,XmlThe output path of the file;
  • tables: The name of the database table whose code needs to be generated automatically this time;