mybatis-plus之代码生成器

当我们需要对数据库操作时,常规的做法是建立dao,service,controller,mapper.xml文件,一系列繁琐的操作让我们总在做无用功,以前可以用逆向工程生成,现在mybatis-plus也给我们提供了模板,而且功能更加的灵活和强大,我们只要配置好基本的参数,就可以自动生成了,下面正式开始奇迹之旅吧!

1、pom文件


    org.springframework.boot
    spring-boot-starter-parent
    2.0.3.RELEASE




    
    
        org.springframework.boot
        spring-boot-starter-web
    

    
        org.springframework.boot
        spring-boot-starter
    
    
        org.springframework.boot
        spring-boot-starter-test
        test
    


    
    
        com.baomidou
        mybatis-plus-boot-starter
        3.0.1
    

    
    
        com.alibaba
        druid
        1.0.29
    

    
    
        mysql
        mysql-connector-java
        5.1.38
    

    
        org.projectlombok
        lombok
        1.16.16
    

2、启动类

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;

import java.net.URL;
import java.util.Objects;

/**
 * @Description:
 * @Auther: chenmingjian
 * @Date: 18-10-25 18:14
 */

public class CodeGenerator {

    //代码生成器
    private static AutoGenerator mpg = new AutoGenerator();

    //全局配置
    private static  GlobalConfig gc = new GlobalConfig();

    //作者、包名、去除表前缀
    private static final String author = "chenmingjian";
    private static final String package_name = "mybatisplus";
    private static final String TABLE_PREFIX = "tb_";

    //数据库
    private static final String url = "jdbc:mysql://192.168.3.172:3306/test?useUnicode=true&useSSL=false&characterEncoding=utf8";
    private static final String driverName = "com.mysql.jdbc.Driver";
    private static final String userName = "devuser";
    private static final String password = "dev123";
    private static final String table_name = "tb_user";


    public static void main(String[] args){

        // 数据源配置
        setDataSource();

        // 全局配置
        setGlobalConfig();

        // 策略配置
        setStrategy();

        //执行
        mpg.execute();
    }

    private static void setStrategy() {
        StrategyConfig strategy = new StrategyConfig();

        // 类名:Tb_userController -> TbUserController
        strategy.setNaming(NamingStrategy.underline_to_camel);
        // 属性名:start_time -> startTime
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        // lombok 代替 setter/getter等方法
        strategy.setEntityLombokModel(true);
        // 设置Controller为RestController
        strategy.setRestControllerStyle(true);
        //由数据库该表生成
        strategy.setInclude(table_name);
        //去除表前缀
        strategy.setTablePrefix(TABLE_PREFIX);
        mpg.setStrategy(strategy);
    }

    private static void setGlobalConfig() {
        URL urlPath = Thread.currentThread().getContextClassLoader().getResource("");
        String projectPath = Objects.requireNonNull(urlPath).getPath().replace("target/classes", "src/main/java");
        gc.setOutputDir(projectPath);//代码生成位置
        gc.setFileOverride(true);//覆盖已有文件
        gc.setAuthor(author);
        gc.setSwagger2(true);
        gc.setIdType(IdType.AUTO);//主键ID类型
        gc.setDateType(DateType.ONLY_DATE);//设置时间类型为Date
        mpg.setGlobalConfig(gc);
        PackageConfig pc = new PackageConfig();// 包配置
        pc.setParent(package_name);
        mpg.setPackageInfo(pc);
    }

    private static void setDataSource() {
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(url);
        dsc.setDriverName(driverName);
        dsc.setUsername(userName);
        dsc.setPassword(password);
        mpg.setDataSource(dsc);
    }

}

 

3、生成结果展示

mybatis-plus之代码生成器_第1张图片

 

注意事项:

1、若不想使用vm模板生成,则只要将模板删除即可

2、若想自定义类名则
 

//        gc.setEntityName(entityName);
//        gc.setMapperName(name_prefix + "Mapper");
//        gc.setControllerName(name_prefix + "Controller");
//        gc.setServiceName(name_prefix + "Service");
//        gc.setServiceImplName(name_prefix + "ServiceImpl");

github: https://github.com/jxcmj92/mybatis-plus-generator

mybatis-plus官网:http://mp.baomidou.com/guide/generator.html

你可能感兴趣的:(Mybatis-plus)