xiangpei
2025-04-18 ccadf9480d4e6a9dcc227a2a0b1f9ae0612e36fd
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
152
153
154
//package com.monkeylessey.gen.utils;
//
//import com.baomidou.mybatisplus.core.mapper.BaseMapper;
//import com.baomidou.mybatisplus.generator.FastAutoGenerator;
//import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
//import com.baomidou.mybatisplus.generator.config.OutputFile;
//import com.baomidou.mybatisplus.generator.config.TemplateConfig;
//import com.monkeylessey.sys.domain.base.AbsEntity;
//import com.monkeylessey.gen.enums.GenFileTypeEnum;
//import com.monkeylessey.gen.gen.MyVelocityTemplateEngine;
//import org.apache.commons.lang3.StringUtils;
//
//import java.util.*;
//import java.util.function.Consumer;
//
///**
// * 该类用于后端人员直接使用
// *
// * @author 29443
// * @date 2022/4/7
// */
//public class GenUtil {
//
//    public static void main(String[] args) {
//        // 要生成代码的表
//        List<String> tableNames = Arrays.asList(
//                "sys_role"
//        );
//        // 为哪个模块生成代码
//        String forWhichModule = "gen-demo";
//        plusGener(tableNames, forWhichModule);
//    }
//
//    /**
//     * 代码生成方法
//     *
//     * @param genTables      要生成代码的表
//     * @param forWhichModule 为哪个模块生成代码
//     */
//    public static void plusGener(List<String> genTables, String forWhichModule) {
//        String outDir = "";
//        String mapperDir = "";
//        if (StringUtils.isNotBlank(forWhichModule)) {
//            // 多模块应用
//            outDir = System.getProperty("user.dir") + "\\" + forWhichModule + "\\src\\main\\java";
//            mapperDir = System.getProperty("user.dir") + "\\" + forWhichModule + "\\src\\main\\resources\\mapper";
//        } else {
//            // 单模块应用
//            outDir = System.getProperty("user.dir") + "\\src\\main\\java";
//            mapperDir = System.getProperty("user.dir") + "\\src\\main\\resources\\mapper";
//        }
//        System.out.println(System.getProperty("user.dir"));
//        String finalOutDir = outDir;
//        String finalMapperDir = mapperDir;
//
//        // 自定义的模板路径
//        Map<String, String> customFileMap = new HashMap();
//        customFileMap.put(GenFileTypeEnum.FORM.getValue(), "/templates/form.java.vm");
//        customFileMap.put(GenFileTypeEnum.VO.getValue(), "/templates/vo.java.vm");
//        customFileMap.put(GenFileTypeEnum.QUERY.getValue(), "/templates/query.java.vm");
//        customFileMap.put(GenFileTypeEnum.VIEW.getValue(), "/templates/vueView.vue.vm");
//        customFileMap.put(GenFileTypeEnum.DIALOG.getValue(), "/templates/vueDialog.vue.vm");
//        customFileMap.put(GenFileTypeEnum.TABLE.getValue(), "/templates/vueTable.vue.vm");
//        customFileMap.put(GenFileTypeEnum.STORE.getValue(), "/templates/vueStore.js.vm");
//        customFileMap.put(GenFileTypeEnum.API.getValue(), "/templates/vueApi.js.vm");
//
//        // 自定义数据
//        Map<String, Object> customDataMap = new HashMap();
//        // 数据校验分组
//        customDataMap.put("addGroup", "com.monkeylessey.group.AddGroup");
//        customDataMap.put("updateGroup", "com.monkeylessey.group.UpdateGroup");
//        // 统一响应类
//        customDataMap.put("responseDataName", "Result");
//        customDataMap.put("responseDataPath", "com.monkeylessey.response.Result");
////            // 是否存在创建者字段
////        customDataMap.put("hasCreateBy", true);
////            // 是否存在修改者字段
////        customDataMap.put("hasUpdateBy", true);
////            // 是否是父子嵌套表
////        customDataMap.put("isNest", true);
//        // 指向父的字段
////        customDataMap.put("parentPoint", "ParentId");
////            // 分页类名
////        customDataMap.put("pageClassName", "PageData");
////            // 分页类所在路径
////        customDataMap.put("pagePath", "com.monkeylessey.sys.domain");
//
//        FastAutoGenerator.create("jdbc:mysql://39.108.83.106:3306/start", "root", "Xp@199866")
//                .globalConfig(builder -> {
//                    builder.author("向培")               //作者
//                            .outputDir(finalOutDir)    //输出路径(写到java目录)
//                            .enableSwagger()           //开启swagger
//                            .commentDate("yyyy-MM-dd");
//                    //.fileOverride();            //开启覆盖之前生成的文件
//
//                })
//                .packageConfig(builder -> {
//                    builder.parent("com.monkeylessey")
//                            //.moduleName("onceCode")
//                            .entity("domain.entity")
//                            .service("service")
//                            .serviceImpl("service.impl")
//                            .controller("controller")
//                            .mapper("mapper")
//                            .other("domain")
//                            .xml("mapper")
//                            .pathInfo(Collections.singletonMap(OutputFile.other, finalOutDir + "\\domain"))
//                            .pathInfo(Collections.singletonMap(OutputFile.xml, finalMapperDir));
//                })
//                .strategyConfig(builder -> {
//                    builder.addInclude(genTables)
//                            //.addTablePrefix("sys_")
//                            .serviceBuilder()
//                            .formatServiceFileName("%sService")
//                            .formatServiceImplFileName("%sServiceImpl")
//                            .entityBuilder()
//                            // 需要说明的是,父类是需要自己创建的,下面的父类字段也是你自己创建才行。plus不会说发现父类并没有id而创建id字段
//                            .superClass(AbsEntity.class)
//                            // 指定哪些字段在父表中,实体类将不会生成这些字段了
//                            .addSuperEntityColumns("id", "gmt_create", "gmt_update", "deleted")
//                            .enableLombok()
//                            // 开启链式调用
//                            .enableChainModel()
//                            //.logicDeleteColumnName("deleted")
//                            .enableTableFieldAnnotation()
//                            .controllerBuilder()
//                            // 映射路径使用连字符格式,而不是驼峰
//                            .enableHyphenStyle()
//                            .formatFileName("%sController")
//                            .enableRestStyle()
//                            .mapperBuilder()
//                            //生成通用的resultMap
//                            .enableBaseResultMap()
//                            .superClass(BaseMapper.class)
//                            .formatMapperFileName("%sMapper")
//                            .enableMapperAnnotation()
//                            .formatXmlFileName("%sMapper");
//                })
//                .injectionConfig(builder -> {
//                    builder.customMap(customDataMap)
//                            .customFile(customFileMap);
//                })
//                .templateConfig(new Consumer<TemplateConfig.Builder>() {
//                    // 使用自定义entity模板
//                    @Override
//                    public void accept(TemplateConfig.Builder builder) {
//                        builder.entity("templates/entity.java");
//                    }
//                })
//                // 默认的是Velocity引擎模板
//                .templateEngine(new MyVelocityTemplateEngine())
//                .execute();
//    }
//}