关于java:MybatisPlus的自动代码生成器

31次阅读

共计 3909 个字符,预计需要花费 10 分钟才能阅读完成。

为了满足日常的应用,故将生成的主动配置总结为如下,后续能够应用,须要的逻辑删除、乐观锁等,能够进行配置. 具体配置数据能够参考 MP 官网

1、pom.xml

罕用的依赖如下。


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.1.tmp</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>


        <!-- swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.2</version>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.3.1.tmp</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

2、YAML

对应的 yml/properties 文件须要本人去创立生成。yml 配置如下:

server:
  port: 80

#数据库配置
spring:
  thymeleaf:
    cache: false
  datasource:
    username: root
    password: 1234
    url: jdbc:mysql://localhost:3306/mybatisplus?useSSL=false&useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.jdbc.Driver

#配置日志
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true
  mapper-locations: classpath:/mapper/*.xml
  type-aliases-package: com.lgz.Project.entity

3、GeneratorCode

package com.lgz;

import com.baomidou.mybatisplus.annotation.DbType;
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.NamingStrategy;

/**
 * @Author: lgz
 * @Date: 2020/5/15 13:53
 */
public class CodeGenerator {public static void main(String[] args) {
        // 创立代码生成器实例
        AutoGenerator mpg = new AutoGenerator();
        // 配置生成策略

        //1. 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");// 获取以后零碎的目录
        gc.setOutputDir(projectPath+"/src/main/java"); // 生成代码的门路
        gc.setAuthor("lgz");
        gc.setOpen(false);// 生成后是否关上文件
        gc.setFileOverride(false);// 是否笼罩
        gc.setServiceImplName("%sServiceImpl");// 去 service 的 I 前缀
        gc.setServiceName("%sService");
        gc.setMapperName("%sDao");
        gc.setSwagger2(true);
        mpg.setGlobalConfig(gc); // 全局配置

        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/mybatisplus?useSSL=false&useUnicode=true&characterEncoding=utf-8");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("1234");
        dsc.setDbType(DbType.MYSQL);  // 数据库类型
        mpg.setDataSource(dsc); // 配置数据源

        // 配置包
        PackageConfig pc = new PackageConfig();// 设置包治理
        pc.setModuleName("Project");
        pc.setParent("com.lgz");
        pc.setEntity("entity");
        pc.setService("service");
        pc.setController("controller");
        pc.setMapper("dao");
        pc.setXml("mapper");
        mpg.setPackageInfo(pc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        mpg.setStrategy(strategy);
        mpg.execute();}

}

正文完
 0