guohongjin
2024-05-10 19d4dd3d5ab4563b06b13f2cfefd752920ffb9a1
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
package com.mindant.generator.action;
 
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.simple.SimpleDataSource;
import com.mindant.generator.action.config.WebGeneratorConfig;
import com.mindant.generator.action.model.GenQo;
 
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * 代码生成器,可以生成实体,dao,service,controller,html,js
 *
 * @author stylefeng
 * @Date 2017/5/21 12:38
 */
public class GunsCodeBatchGenerator {
 
    public static void main(String[] args) throws Exception {
        // 数据库名
        String dbName = "xlzx";
        // 数据库连接
        String url = "jdbc:mysql://127.0.0.1:3306/"
                + dbName
                + "?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT&nullCatalogMeansCurrent=true";
        // 用户名
        String userName = "root";
        // 密码
        String password = "123456";
 
        // 查询数据库表名、注释
        String tableSql = "SELECT TABLE_NAME AS tableName,TABLE_COMMENT AS tableComment FROM information_schema.`TABLES` WHERE TABLE_SCHEMA = '" + dbName + "'" +
                " AND TABLE_NAME NOT LIKE 'sys_%'" +
                " ORDER BY TABLE_NAME ASC";
 
        // 创建数据源
        SimpleDataSource ds = new SimpleDataSource(url, userName, password);
 
        // 使用数据源连接数据库
        Db db = Db.use(ds);
 
        List<Entity> list = db.query(tableSql);
 
        // 查询表名、注释
        LinkedHashMap<String, String> tableCommentMap = list.stream().collect(Collectors.toMap(e -> e.getStr("tableName"), e -> e.getStr("tableComment"), (e1, e2) -> e1, LinkedHashMap::new));
 
        // 执行SQL查询
        for (String k : tableCommentMap.keySet()) {
            // 打印数据表名
            System.out.println("\"" + k + "\",");
//            System.out.println(k + ":" + tableCommentMap.get(k));
        }
 
        String[] tableNameArray = new String[]{
//                "hr_org_approver",
//                "hr_organization",
//                "hr_position",
                "t_version"
 
        };
 
        for (String tableName : tableNameArray) {
            genTableModel(dbName, url, userName, password, tableName, tableCommentMap);
        }
    }
 
    public static void genTableModel(String dbName, String url, String userName, String password, String tableName, Map<String, String> tableCommentMap) {
        /*
         * guns的生成器:
         * guns的代码生成器可以生成controller,html页面,页面对应的js
         */
//        GunsGeneratorConfig gunsGeneratorConfig = new GunsGeneratorConfig();
//        gunsGeneratorConfig.doMpGeneration();
//        gunsGeneratorConfig.doGunsGeneration();
 
        // 代码生成的查询参数
        GenQo genQo = new GenQo();
        //数据库url
        genQo.setUrl(url);
        // 数据库用户名
        genQo.setUserName(userName);
        // 数据库密码
        genQo.setPassword(password);
        // 表名
        genQo.setTableName(tableName);
        // 作者
        genQo.setAuthor("luodangjia");
        // 数据库名
        genQo.setDbName(dbName);
        // 项目包
        genQo.setProjectPackage("cn.stylefeng.guns");
        genQo.setModuleName("business");
        // 代码生成位置
        genQo.setProjectPath("D:\\study\\PsychologicalCounseling\\common-buiness");
        // 用户信息
        genQo.setBizName(tableCommentMap.get(tableName));
        genQo.setControllerSwitch(true);
        genQo.setEntitySwitch(true);
        genQo.setServiceSwitch(true);
        genQo.setDaoSwitch(true);
        genQo.setIgnoreTabelPrefix("t_");
 
        WebGeneratorConfig webGeneratorConfig = new WebGeneratorConfig(genQo);
        webGeneratorConfig.doMpGeneration();
        webGeneratorConfig.doGunsGeneration();
    }
 
}