CodeAutoGenerator.java 4.54 KB
Newer Older
张辰's avatar
张辰 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
package com.tanpu.community.dao;


import com.baomidou.mybatisplus.annotation.DbType;
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 java.util.ArrayList;
import java.util.List;


public class CodeAutoGenerator {

    public static void main(String[] args) {
        String currentPath = System.getProperty("user.dir");
        String codeBaseHome = "/community-service/src/main/java";
        String author = "xudong";
刘基明's avatar
刘基明 committed
22 23 24 25
        String mysqlUserName = "tamp_admin";
        String mysqlPassword = "@imeng123";
        String jdbcUrl = "jdbc:mysql://rm-uf6r22t3d798q4kmkao.mysql.rds.aliyuncs.com:3306/tamp_community";
        // String[] tables = new String[]{"theme"};
刘基明's avatar
刘基明 committed
26
       String[] tables = new String[]{"file_record"};
张辰's avatar
张辰 committed
27
        String basePackage = "com.tanpu.community";
刘基明's avatar
刘基明 committed
28 29 30
        String mapperPackage = "dao.mapper.community";
        String entityPackage = "dao.entity.community";
        String xmlPath = "D:\\IdeaProjects\\tanpu-community\\community-service\\src\\main\\resources\\mapper\\community";
张辰's avatar
张辰 committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
        AutoGenerator autoGenerator = new AutoGenerator();
        //全局配置
        GlobalConfig gc = new GlobalConfig();
        String path = currentPath + codeBaseHome;
        gc.setOutputDir(path);   //生成文件输出根目录
        gc.setOpen(false);//生成完成后不弹出文件框
        gc.setAuthor(author);
        gc.setFileOverride(true);  //文件覆盖
        gc.setActiveRecord(false);// 不需要ActiveRecord特性的请改为false
        gc.setEnableCache(false);// XML 二级缓存
        gc.setBaseResultMap(true);// XML ResultMap
        gc.setBaseColumnList(false);// XML columList
        gc.setSwagger2(true);

        // 自定义文件命名,注意 %s 会自动填充表实体属性!
        gc.setMapperName("%sMapper");
        gc.setXmlName("%sMapper");
        gc.setEntityName("%sEntity");
        autoGenerator.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);   //设置数据库类型,我是postgresql
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername(mysqlUserName);
        dsc.setPassword(mysqlPassword);
        dsc.setUrl(jdbcUrl);  //指定数据库
        autoGenerator.setDataSource(dsc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);      // 表名生成策略
        strategy.setInclude(tables);     // 需要生成的表
//        strategy.setSuperEntityClass(BaseEntity.class);
//        strategy.setSuperEntityColumns("create_by", "update_by", "create_time", "update_time", "delete_tag");
//        strategy.setSuperServiceClass(null);
//        strategy.setSuperServiceImplClass(null);
//        strategy.setSuperMapperClass(null);
        autoGenerator.setStrategy(strategy);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(basePackage);
//        pc.setController(controllerPackage);
//        pc.setService(servicePackage);
        pc.setMapper(mapperPackage);
        pc.setEntity(entityPackage);
        autoGenerator.setPackageInfo(pc);

        // 模板配置
        TemplateConfig tc = new TemplateConfig();
        tc.setController(null);
        tc.setService(null);
        tc.setServiceImpl(null);
        tc.setXml(null);
        autoGenerator.setTemplate(tc);

        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };

        // 如果模板引擎是 freemarker
//        String templatePath = "/templates/mapper.xml.ftl";
//         如果模板引擎是 velocity
        String templatePath = "/templates/mapper.xml.vm";

        // 自定义输出配置
        List<FileOutConfig> focList = new ArrayList<>();
        // 自定义配置会被优先输出
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return xmlPath + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        autoGenerator.setCfg(cfg);

        // 执行生成
        autoGenerator.execute();
    }
}