puzhibing
2023-10-08 22199bbdda579861736420fe26c2873ab0f5d21c
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
140
141
142
143
144
145
146
147
148
149
150
151
package com.sinata.generator;
 
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.Test;
 
import java.util.HashMap;
import java.util.Map;
 
/**
 * 实体生成
 */
public class EntityGeneratorGoku {
 
    // 代码生成输出目录
    public static final String outputDir = "D:\\work\\IdeaProjects\\project\\meiya-parent\\meiya-rest\\src\\main\\java";
    // 数据库配置
    public static final String dbUrl = "jdbc:mysql://140.210.218.57:8306/meiyadb-dev?characterEncoding=utf8&serverTimezone=GMT%2B8";
    public static final String dbUsername = "root";
    public static final String dbPassword = "Root2018!";
    // 代码类引用包位置
    private static final String basePackage = "com.sinata.rest.modular.mall";
    // Java实体自定义模板(在/src/main/resources目录下)
    private static final String templateFilePath = "/template/entity.java.vm";
    // 表前缀(生成时会去掉前缀)
    public static final String[] tablePrefix = new String[]{"t_"};
    // 需要生成代码的表
//    public static final String[] tableNameArray = new String[]{"t_app_set"};
    public static final String[] tableNameArray = new String[]{
//            "mall_tag",
//            "mall_classify_one",
//            "mall_classify_two",
//            "mall_goods",
//            "mall_goods_cart",
//            "mall_goods_detail",
//            "mall_goods_sku",
//            "mall_goods_spec",
//            "mall_goods_spec_value",
//            "mall_order",
//            "mall_order_main",
//            "mall_order_detail",
//            "mall_vip_equity",
//            "mall_commission_settlement",
//            "mall_commission_settlement_month",
//            "mall_goods_set",
//            "mem_user_login",
//            "system_notice",
            "mall_group_spec",
    };
 
    /***************   com.sinata.modular.code.controller.CodeController   获取全部表数组   ***************************
     List<Map<String, Object>> allTables = tableService.getAllTables();
     StringBuffer sb = new StringBuffer("{");
     for (int i = 0; i < allTables.size(); i++) {
     sb.append("\"" + allTables.get(i).get("tableName") + "\",");
     }
     System.out.println("所有表:" + sb.substring(0, sb.length()-1) + "}");
     *******************************************************************************************************************/
 
    @Test
    public void entityGenerator() {
        AutoGenerator mpg = new AutoGenerator();
 
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(outputDir);//这里写你自己的java目录
        gc.setFileOverride(true);//是否覆盖
        gc.setActiveRecord(true);
        gc.setEnableCache(false);// XML 二级缓存
        gc.setBaseResultMap(true);// XML ResultMap
        gc.setBaseColumnList(false);// XML columList
        gc.setAuthor("goku");
        mpg.setGlobalConfig(gc);
 
        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
//        dsc.setTypeConvert(new MySqlTypeConvert() {
//            // 自定义数据库表字段类型转换【可选】
//            @Override
//            public DbColumnType processTypeConvert(String fieldType) {
//                return super.processTypeConvert(fieldType);
//            }
//        });
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername(dbUsername);
        dsc.setPassword(dbPassword);
        dsc.setUrl(dbUrl);
        mpg.setDataSource(dsc);
 
//        // 获取数据库所有表名【自定义】
//        List<Map<String, Object>> allTables = tableService.getAllTables();
//        String[] tableNameArray = new String[allTables.size()];
//        for (int i = 0; i < allTables.size(); i++) {
//            tableNameArray[i] = allTables.get(i).get("tableName").toString();
//        }
 
        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setTablePrefix(tablePrefix);// 此处可以修改为您的表前缀
        strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
        strategy.setInclude(tableNameArray);// 需要生成代码的数据表
        // 是否使用Lombok简化代码
        strategy.setEntityLombokModel(true);
//        strategy.setExclude(new String[]{"test"}); // 排除生成的表
        mpg.setStrategy(strategy);
 
        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(null);
 
        pc.setEntity(basePackage + ".model");
        pc.setXml(basePackage + ".dao.mapping");
        pc.setMapper(basePackage + ".dao");
        pc.setService(basePackage + ".service");
        pc.setServiceImpl(basePackage + ".service.impl");
        pc.setController(basePackage + ".controller");
 
//        pc.setEntity("TTT");    //本项目没用,生成之后删掉
//        pc.setXml("TTT");    //本项目没用,生成之后删掉
//        pc.setMapper("TTT");    //本项目没用,生成之后删掉
//        pc.setService("TTT");    //本项目没用,生成之后删掉
//        pc.setServiceImpl("TTT");    //本项目没用,生成之后删掉
//        pc.setController("TTT");    //本项目没用,生成之后删掉
 
        mpg.setPackageInfo(pc);
 
        // 自定义模板配置
        TemplateConfig tc = new TemplateConfig();
        tc.setEntity(templateFilePath);
        // 如上任何一个模块如果设置 空 OR Null 将不生成该模块。
        mpg.setTemplate(tc);
 
        // 注入自定义配置,可以在 VM 中使用 cfg.abc 设置的值
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                Map<String, Object> map = new HashMap<>();
                map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
                this.setMap(map);
            }
        };
        mpg.setCfg(cfg);
 
        // 执行生成
        mpg.execute();
    }
}