springboot+mybatis plus下代码生成器

一、导包(数据库的包以及框架自带的就不说了)

 
        
            com.baomidou
            mybatis-plus
            2.3
        

        
        
            org.apache.velocity
            velocity
            1.7
        

        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.1.1
        

二、相关配置

package czy.com.dldl.utils;

import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
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.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.apache.commons.lang.StringUtils;

import java.util.Scanner;

/**
 * @author Czy--feeling
 * @create 2019-03-25 5:30 PM
 **/
public class Generator {

    public static final String OUT_PUT_DIR = "/Users/macbook/intellij-workspace/DLDL/src/main/java";

    public static final String AUTHOR = "feeling";

    public static final String URL = "jdbc:mysql://localhost:3306/dldl?useSSL=false&&characterEncoding=utf-8&&jdbcCompliantTruncation=false&&zeroDateTimeBehavior=convertToNull";

    public static final String DRIVER_NAME = "com.mysql.cj.jdbc.Driver";

    public static final String USERNAME = "root";

    public static final String PASSWORD = "root";

    public static final String MODULENAME = "";

    public static final String PARENT = "czy.com.dldl";

    public static final String CONTROLLER = "controller";

    public static void main(String[] args) {
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(OUT_PUT_DIR);
        gc.setFileOverride(true);
        gc.setActiveRecord(true);// 开启 activeRecord 模式
        gc.setEnableCache(false);// XML 二级缓存
        gc.setBaseResultMap(true);// XML ResultMap
        gc.setBaseColumnList(false);// XML columList
        gc.setAuthor(AUTHOR);
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        dsc.setUrl(URL);
        dsc.setDriverName(DRIVER_NAME);
        dsc.setUsername(USERNAME);
        dsc.setPassword(PASSWORD);
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName(MODULENAME);
        pc.setParent(PARENT);// 自定义包路径
        pc.setController(CONTROLLER);// 这里是控制器包名,默认 web
        mpg.setPackageInfo(pc);

        //策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        strategy.setInclude(scanner("表名"));
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix(pc.getModuleName() + "_");
        strategy.setEntityColumnConstant(true);
        mpg.setStrategy(strategy);
        mpg.execute();// 执行生成
    }

    private static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        System.out.println(("请输入" + tip + ":"));
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotBlank(ipt)) return ipt;
        }
        throw new MybatisPlusException("请输入正确的" + tip + "!");
    }
}

 

你可能感兴趣的:(mybatis,plus)