zoukankan      html  css  js  c++  java
  • MyBatis-Plus代码自动生成器

    package watt.gasleakage;
    
    import com.baomidou.mybatisplus.annotation.DbType;
    import com.baomidou.mybatisplus.annotation.FieldFill;
    import com.baomidou.mybatisplus.annotation.IdType;
    import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
    import com.baomidou.mybatisplus.core.toolkit.StringPool;
    import com.baomidou.mybatisplus.core.toolkit.StringUtils;
    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.TableFill;
    import com.baomidou.mybatisplus.generator.config.po.TableInfo;
    import com.baomidou.mybatisplus.generator.config.rules.DateType;
    import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
    import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    /**
     * @Author watt
     * @Description 代码生成器
     * @Date 2020/12/17 15:38
     */
    public class CodeGenerator {
    
        /**
         * <p>
         * 读取控制台内容
         * </p>
         */
        public static String scanner(String tip) {
            Scanner scanner = new Scanner(System.in);
            StringBuilder help = new StringBuilder();
            help.append("请输入" + tip + ":");
            System.out.println(help.toString());
            if (scanner.hasNext()) {
                String ipt = scanner.next();
                if (StringUtils.isNotEmpty(ipt)) {
                    return ipt;
                }
            }
            throw new MybatisPlusException("请输入正确的" + tip + "!");
        }
    
        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.setFileOverride(true); // 默认false,是否覆盖已生成文件
            gc.setAuthor("watt"); // 作者
            gc.setOpen(false); //默认true,是否打开输出目录
            gc.setSwagger2(true); //实体属性 Swagger2 注解
            // 自定义文件名,%s会自动填充表实体属性
            gc.setServiceName("%sService"); // Service接口的名字,去除Service的I前缀
            gc.setIdType(IdType.AUTO); // 主键类型
            gc.setDateType(DateType.ONLY_DATE); // 日期类型
            mpg.setGlobalConfig(gc);
    
            // 2、数据库连接配置
            DataSourceConfig dsc = new DataSourceConfig();
            dsc.setUrl("jdbc:mysql://localhost:3306/gasleakage?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=UTC");
            dsc.setDriverName("com.mysql.cj.jdbc.Driver");
            dsc.setUsername("root");
            dsc.setPassword("123456");
            dsc.setDbType(DbType.MYSQL); // 数据库类型
            mpg.setDataSource(dsc);
    
            // 3、包配置
            PackageConfig pc = new PackageConfig();
            pc.setModuleName(null); // 所属模块的名称
            pc.setParent("watt.gasleakage"); // 代码生成到哪个包下面
            pc.setEntity("entity"); // 生成包的名称
            pc.setMapper("mapper");
            pc.setService("service");
            pc.setController("controller");
            mpg.setPackageInfo(pc);
    
            // 4、策略配置
            StrategyConfig strategy = new StrategyConfig();
            strategy.setNaming(NamingStrategy.underline_to_camel); // 表映射 驼峰命名
            strategy.setColumnNaming(NamingStrategy.underline_to_camel); // 字段映射
            strategy.setEntityLombokModel(true); // 默认false,是否使用lombok
            strategy.setRestControllerStyle(true); // Restful
            strategy.setInclude(scanner("表名,多个英文逗号分割").split(",")); // 设置要映射的表名
            strategy.setControllerMappingHyphenStyle(true); // url中支持下划线,localhost:8080/hello_id_2
            strategy.setLogicDeleteFieldName("deleted"); // 逻辑删除的名字
            // strategy.setTablePrefix("m_"); // 表的前缀
            // 自动填充配置
            TableFill createTime = new TableFill("create_time", FieldFill.INSERT);
            TableFill updateTime = new TableFill("update_time",FieldFill.INSERT_UPDATE);
            ArrayList<TableFill> tableFills = new ArrayList<>();
            tableFills.add(createTime);
            tableFills.add(updateTime);
            strategy.setTableFillList(tableFills);
            strategy.setVersionFieldName("version"); // 乐观锁
            mpg.setStrategy(strategy);
    
            // 5、模板配置
            TemplateConfig templateConfig = new TemplateConfig();
            templateConfig.setXml(null);
            mpg.setTemplate(templateConfig);
    
            // 6、模板引擎配置
            mpg.setTemplateEngine(new FreemarkerTemplateEngine());
    
            // 7、自定义配置
            InjectionConfig cfg = new InjectionConfig() {
                @Override
                public void initMap() {
                    // to do nothing
                }
            };
    
            String templatePath = "/templates/mapper.xml.ftl"; // 如果模板引擎是 freemarker
            // String templatePath = "/templates/mapper.xml.vm"; // 如果模板引擎是 velocity
    
            List<FileOutConfig> focList = new ArrayList<>(); // 自定义输出配置
            focList.add(new FileOutConfig(templatePath) { // 自定义配置会被优先输出
                @Override
                public String outputFile(TableInfo tableInfo) {
                    // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
                    return projectPath + "/src/main/resources/mapper/"
                            + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
                }
            });
    
            cfg.setFileOutConfigList(focList);
            mpg.setCfg(cfg);
    
            // 执行配置
            mpg.execute();
        }
    }
    
  • 相关阅读:
    《深入理解 Java 虚拟机》读书笔记:线程安全与锁优化
    《深入理解 Java 虚拟机》读书笔记:Java 内存模型与线程
    《深入理解 Java 虚拟机》读书笔记:晚期(运行期)优化
    《深入理解 Java 虚拟机》读书笔记:早期(编译期)优化
    《深入理解 Java 虚拟机》读书笔记:虚拟机字节码执行引擎
    《深入理解 Java 虚拟机》读书笔记:虚拟机类加载机制
    Java学习书籍推荐
    IntelliJ IDEA之新建项目后之前的项目不见了
    剑指Offer之左旋转字符串
    剑指Offer之和为S的两个数字
  • 原文地址:https://www.cnblogs.com/wattmelon/p/14306971.html
Copyright © 2011-2022 走看看