mybatis-plus之代码生成器

当我们需要对数据库操作时,常规的做法是建立dao,service,controller,mapper.xml文件,一系列繁琐的操作让我们总在做无用功,以前可以用逆向工程生成,现在mybatis-plus也给我们提供了模板,而且功能更加的灵活和强大,我们只要配置好基本的参数,就可以自动生成了,废话不多说,直接上代码。
1、pom.xml


    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、启动类

package com.wocloud.miaosha;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.List;

/**
 * @author mazhen
 */
public class CodeGenerator {

    //作者
    private static final String author = "mazhen";

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

    //表对应的基类名
    private static final String entityName = "User";

    //如StudentController,StudentService
    private static final String name_prex = "User";

    //生成在该目录下
    private static final String package_name = "com.wocloud.miaosha";


    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        //不覆盖已有,则为false
        gc.setFileOverride(true);
        // 不需要ActiveRecord特性的请改为false
        gc.setActiveRecord(false);
        // XML 二级缓存
        gc.setEnableCache(false);
        // XML ResultMap
        gc.setBaseResultMap(true);
        // XML columList
        gc.setBaseColumnList(true);
        gc.setIdType(IdType.AUTO);
        gc.setAuthor(author);
        gc.setOpen(false);
        gc.setEntityName(entityName);
        gc.setMapperName(name_prex + "Mapper");
        gc.setControllerName(name_prex + "Controller");
        gc.setServiceName(name_prex + "Service");
        gc.setServiceImplName(name_prex + "ServiceImpl");
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(url);
        // dsc.setSchemaName("public");
        dsc.setDriverName(driverName);
        dsc.setUsername(userName);
        dsc.setPassword(password);
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        //pc.setModuleName(scanner("模块名"));
        pc.setParent(package_name);
        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };
        List focList = new ArrayList<>();
        focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return projectPath + "/src/main/resources/mapper/" /*+ pc.getModuleName()*/
                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
        mpg.setTemplate(new TemplateConfig().setXml(null));

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
//        strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity");
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
//        strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");
        strategy.setInclude(table_name);
        //strategy.setSuperEntityColumns("id");
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix(pc.getModuleName() + "_");
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }

}

3、生成结果展示
mybatis-plus之代码生成器_第1张图片
4、mybatis-plus官方文档:https://baomidou.gitee.io/mybatis-plus-doc/#/generate-code

你可能感兴趣的:(mybatis)