AdaKing88
2023-08-21 23258c585a626b15d770459870a8b24763cf540c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package com.tairui.app.cim.util;
 
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.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
 
 
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
 
/**
 * @program: spring-boot-parent
 * @description: 代码生成工具类
 * @author:
 * @create: 2020-12-14 19:23
 */
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.isNotBlank(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "!");
    }
 
 
    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");
        gc.setAuthor("hyy");//设置作者
        gc.setOpen(false);
        gc.setFileOverride(false);
        gc.setIdType(IdType.ASSIGN_ID);
        gc.setDateType(DateType.ONLY_DATE);
        gc.setSwagger2(true); //实体属性 Swagger2 注解
        gc.setServiceName("%sService");    //去掉Service接口的首字母I
        gc.setBaseResultMap(true);
        gc.setBaseColumnList(true);
        mpg.setGlobalConfig(gc);
 
        TemplateConfig templateConfig = new TemplateConfig()
                .setController(ConstVal.TEMPLATE_CONTROLLER).setXml(null)
                .setServiceImpl(ConstVal.TEMPLATE_SERVICE_IMPL).setXml(null)
                .setMapper(ConstVal.TEMPLATE_MAPPER).setXml(null)
                .setService(ConstVal.TEMPLATE_SERVICE).setXml(null);
 
        mpg.setTemplate(templateConfig);
        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:postgresql://192.168.22.198:5432/fssh?characterEncoding=utf8&ssl=false");
        // dsc.setSchemaName("public");
        dsc.setDriverName("org.postgresql.Driver");
        dsc.setUsername("postgres");
        dsc.setPassword("postgres");
//        dsc.setUrl("jdbc:mysql://localhost:3306/cyioc?charset=utf8mb4&useSSL=false&characterEncoding=utf-8&serverTimezone=Asia/Shanghai");
//        // dsc.setSchemaName("public");
//        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
//        dsc.setUsername("root");
//        dsc.setPassword("123456");
//        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);
 
 
        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.tairui.app.cim");
        pc.setModuleName(scanner("模块名"));
        pc.setEntity("domain");
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setController("controller");
        mpg.setPackageInfo(pc);
 
 
        // 如果模板引擎是 velocity
        // String templatePath = "/templates/mapper.xml.vm";
 
 
        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        //公共父类
        //strategy.setSuperControllerClass("BaseEntity");
//        strategy.setSuperEntityClass(BaseEntity.class);
        // 写于父类中的公共字段
//        strategy.setSuperEntityColumns("id", "create_time", "update_time");
        strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix(pc.getModuleName() + "_");
        strategy.setVersionFieldName("version");
 
 
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
            }
        };
        List<FileOutConfig> configList = new ArrayList<>();
        configList.add(new FileOutConfig(ConstVal.TEMPLATE_XML+ ".vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return projectPath+"/src/main/resources/"+pc.getParent().replace(".","/")
                        +"/mapper/"+tableInfo.getXmlName()+ StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(configList);
        mpg.setCfg(cfg);
        mpg.setStrategy(strategy);
        mpg.execute();
    }
}