mybatis_plus Plug-in: Generator

Keywords: MySQL Mybatis Spring Database xml

Recently, learning the mybatis framework has simplified some Dao code, but still want to go up to the next level? Are you no longer bothered by the basic duplicate code in the basic pojo layer, controller layer, service layer and Dao layer? Here, let's learn about the mybatis plus generator and realize mybatis reverse engineering, preferably with spring boot.

1. Import maven dependencies, pay attention to versions, and write generator main classes

<!--springboot Use mybatis-plus,Two Dependencies Needed jar package-->
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus</artifactId>
      <version>2.1.9</version>
    </dependency>
    <dependency>
      <!--mybatis Automatic assembly must-->
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-autoconfigure</artifactId>
      <version>1.3.2</version>
    </dependency>
 1 package com.ljg.generator;
 2 
 3 
 4 import com.baomidou.mybatisplus.enums.IdType;
 5 import com.baomidou.mybatisplus.generator.AutoGenerator;
 6 import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
 7 import com.baomidou.mybatisplus.generator.config.GlobalConfig;
 8 import com.baomidou.mybatisplus.generator.config.PackageConfig;
 9 import com.baomidou.mybatisplus.generator.config.StrategyConfig;
10 import com.baomidou.mybatisplus.generator.config.rules.DbType;
11 import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
12 
13 import java.sql.SQLException;
14 
15 public class MyBatisPlusGenerator {
16 
17     public static void main(String[] args) throws SQLException {
18         String tableName="user";//Table nameRemember to create database tables with numbers
19         AutoGenerator mpg = new AutoGenerator();
20         // Choice freemarker Engine, default Veloctiy
21         //1. Global configuration
22         GlobalConfig config = new GlobalConfig();
23         config.setActiveRecord(true) // Whether to support AR Pattern
24                 .setAuthor("Ljg") // author
25                 .setEnableCache(false)// XML Two level cache
26                 .setOutputDir("D:\\IdeaProject\\mybatis_plus_demo\\src\\main\\java") // Generation path
27                 .setFileOverride(true)  // File coverage
28                 .setIdType(IdType.AUTO) // Primary key policy
29                 .setBaseResultMap(true)//Generate basic resultMap
30                 .setBaseColumnList(true)//Generate basic SQL fragment
31         /* Customize file naming. Note that% s automatically fills in table entity attributes! */
32                  .setMapperName("I%sDao")
33                  .setXmlName("I%sDao")
34                  .setServiceName("I%sService")
35                  .setServiceImplName("%sServiceImpl")
36                  .setControllerName("%sController");
37                  mpg.setGlobalConfig(config);
38 
39         //2. Data source configuration
40         DataSourceConfig dsConfig = new DataSourceConfig();
41         dsConfig.setDbType(DbType.MYSQL)  // Setting up the database type
42                 .setDriverName("com.mysql.jdbc.Driver")
43                 .setUrl("jdbc:mysql://172.1.10.24/zz?useUnicode=true&characterEncoding=utf-8")
44                 .setUsername("root")
45                 .setPassword("123456");
46                 mpg.setDataSource(dsConfig);
47 
48         //3. Policy configuration globalConfiguration in
49         StrategyConfig stConfig = new StrategyConfig();
50         stConfig.setCapitalMode(true) //Global capitalization naming
51                 .setDbColumnUnderline(true)  // Specifies whether the table name field name is underlined
52                 .setNaming(NamingStrategy.underline_to_camel) // Naming strategy for mapping database tables to entities
53                 //.setTablePrefix("tbl_")//table prefix
54                 .setInclude(tableName);  // Tables to be generated
55 
56         //4. Package Name Policy Configuration
57         PackageConfig pkConfig = new PackageConfig();
58         pkConfig.setParent("com.ljg")
59                 .setMapper("dao")//dao
60                 .setService("service")//servcie
61                 .setController("controller")//controller
62                 .setEntity("po")
63                  .setXml("mappper");//configuration file mapper/xml
64 
65         //5. Integrated configuration
66         AutoGenerator ag = new AutoGenerator();
67         ag.setGlobalConfig(config)
68                 .setDataSource(dsConfig)
69                 .setStrategy(stConfig)
70                 .setPackageInfo(pkConfig);
71 
72         //6. implement
73         ag.execute();
74     }
75 }

2. Implementation results:

One key constitutes a three-tier architecture: controller layer, service layer, dao layer, and mybatis.xml file.

Posted by bpops on Tue, 01 Oct 2019 12:27:52 -0700