Introduction to the

  • mybatis-plusMybatis is a framework based on MyBatis, which strengthens the advantages of MyBatis and makes up for the original inconvenient functions of MyBatis. If you are using MyBatis, you like its flexibility, but sometimes you have to write XML files in some simple queries, and the built-in Example mode is tedious. Mybatis – Plus will be perfect for you.
  • Mybatis – Plus itself and Mybatis are not conflicting, can be used together.
  • As a framework written by people, Mybatis – Plus is in line with people’s development habits, mybatis- Plus in addition to its strengths query, but also integrated such as paging, automatic filling, logical deletion, SQL injection, tenant plug-ins and other functions, can be described as very rich.
  • The author hopes to combine with their own use, in addition to the framework itself tutorial to add their own understanding of ordinary work, to help everyone use Mybatis – Plus.
  • Mybatis – Plus official website: baomidou.com/guide/, many basic use can also be found in the official website.

The environment

Here the author uses:

  • Springboot2.3 + In fact, springBoot2.0 and above can be used
  • Mybatis plus3.4 +
  • Mybatisplus can only be used in the lower version of JDk7, and many features are not available

Home page to build the most basic Springboot project, here will not be described, after the introduction of database connection dependency, here is used mysql:

         <dependency>
               <groupId>mysql</groupId>
               <artifactId>mysql-connector-java</artifactId>
         </dependency>
Copy the code

Then introduce the three mybatis-plus related packages we will use:

        <! Mybatis Plus core dependencies -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>Rule 3.4.3</version>
        </dependency>
        <! -- Mybatis Plus code generator dependencies -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.4.0</version>
        </dependency>
        <! -- Code generator template -->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.3</version>
        </dependency>
Copy the code

Here, if the code is run purely, only the first dependency is needed. The second and third dependencies are those related to the automatic generation of model, Mapper, DAO, and other code.

Code generator

The CodeGenerator code is available on the official website, and the author’s file is posted here:

package com.wochanye; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.config.DataSourceConfig; import com.baomidou.mybatisplus.generator.config.GlobalConfig; import com.baomidou.mybatisplus.generator.config.PackageConfig; import com.baomidou.mybatisplus.generator.config.StrategyConfig; import com.baomidou.mybatisplus.generator.config.rules.DateType; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; /** * @Author Chen yongyu * @Date 2019/04/01 * @Description */ public class CodeGenerator { public static void main(String[] args) { entityGenerator(); } public static void entityGenerator() { String[] tables = new String[]{ "sys_user", "sys_group", "sys_user_group" }; AutoGenerator mpg = new AutoGenerator(); // GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir"); gc.setOutputDir(projectPath + "/src/main/java"); gc.setFileOverride(true); // Whether to override gc.setActiverecord (true); gc.setEnableCache(false); // XML level cache gc.setBaseresultmap (true); // XML ResultMap gc.setBaseColumnList(false); // XML columList gc.setSwagger2(true); gc.setAuthor("Chen Yongyu"); gc.setOpen(false); gc.setDateType(DateType.TIME_PACK); gc.setIdType(IdType.AUTO); mpg.setGlobalConfig(gc); // DataSourceConfig DSC = new DataSourceConfig(); dsc.setDriverName("com.mysql.cj.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("Uni@123"); DSC. SetUrl (" JDBC: mysql: / / 172.22.12.23:3306 / dtube_cyy? characterEncoding=UTF-8&zeroDateTimeBehavior" + "=convertToNull&useSSL=true&verifyServerCertificate=false"); mpg.setDataSource(dsc); // StrategyConfig strategy = new StrategyConfig(); //strategy.setTablePrefix(new String[]{"_"}); // This can be changed to your table prefix strategy.setnaming (namingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setEntityLombokModel(true); / / / / logic delete field / / strategy setLogicDeleteFieldName (" does "); // // autofill fields // List<TableFill> tableFillList = new ArrayList<>(); // tableFillList.add(new TableFill("create_time", FieldFill.INSERT)); // tableFillList.add(new TableFill("create_user", FieldFill.INSERT)); // tableFillList.add(new TableFill("update_time", FieldFill.INSERT_UPDATE)); // tableFillList.add(new TableFill("update_user", FieldFill.INSERT_UPDATE)); // tableFillList.add(new TableFill("organization_id", FieldFill.INSERT)); // strategy.setTableFillList(tableFillList); strategy.setInclude(tables); mpg.setStrategy(strategy); PackageConfig PC = new PackageConfig(); pc.setParent(null); pc.setEntity("com.wochanye.model"); pc.setMapper("com.wochanye.mapper"); pc.setXml("com.wochanye.mapping"); pc.setService("com.wochanye.service"); pc.setServiceImpl("com.wochanye.service.impl"); pc.setController("com.wochanye.controller"); mpg.setPackageInfo(pc); // generate mpg.execute(); System.out.println("mybatis-plus generator over."); }}Copy the code

Some of the code here has been commented out for functionality (logical deletion, autofill, etc.) that will be used later.

Modify the database connection information and tables to be generated in this article, you can see the related files after running.Commonly agreed:Automatically generated files are not modified, but can be inherited and extendedBecause these files need to be regenerated if the database changes.

If you don’t want to use one of the layers provided by MyBatisPlus, such as the Controller and Service layers, the author usually changes the generated path to a unified package and removes it after it is generated.

The configuration file

Basic use to configure the database connection information and Mybatis -plus package information can be. Configure in application.yml:

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: JDBC: mysql: / / 172.22.12.23:3306 / dtube_cyy? useUnicode=true&useSSL=false&characterEncoding=utf-8
    username: root
    password: Uni@123


mybatis-plus:
  mapper-locations:
    - classpath*:com/wochanye/mapping/**/*.xml
  type-aliases-package: com.wochanye.model
Copy the code

Add a comment to the startup class:

@MapperScan(basePackages = {"com.wochanye.mapper"})
Copy the code

A test run

Inject mapper and run the most basic count method to get the result.

In the next chapter we will explain the basic use of Mybatis – Plus.