cy
2022-06-21 129904537f66509f97b285e7eb4f42b3dc349dd0
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package cn.ksource.tools.db2java;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.apache.commons.lang.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
 
import cn.ksource.core.dao.BaseDao;
import cn.ksource.core.dao.SqlParameter;
import cn.ksource.core.util.DateUtil;
import cn.ksource.core.util.FreeMarkerUtil;
import cn.ksource.core.util.JdbcUtil;
import freemarker.ext.beans.BeansWrapper;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateHashModel;
 
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/applicationContext_core.xml"})
@Transactional
public class Db2Java4Mysql {
    
    @Autowired
    private BaseDao baseDao;
    
    public final static String TABLE_SCHEMA = JdbcUtil.getDbName();
    public final static String FILE_PATH = "d:\\db2java\\";
    public final static String PACKAGE_PATH = "";
    public final static String CLASS_FILE_PATH = FILE_PATH+"classes";
    private List<Map> freeMakerList;
    
    
    /**
     * @return
     */
    private List<Map> getFreeMakerList(){
        if (freeMakerList == null || freeMakerList.size() == 0) {
            String sql = "select TABLE_NAME,COLUMN_NAME,COLUMN_TYPE as TYPE,COLUMN_COMMENT from information_schema.columns where TABLE_SCHEMA = '"+ Db2Java4Mysql.TABLE_SCHEMA +"'";
            List<Map> list = baseDao.queryForList(sql,new SqlParameter());
            for (Map map : list) {
                String column_type = map.get("TYPE").toString().toLowerCase();
                if (StringUtils.startsWith(column_type, "char") || StringUtils.startsWith(column_type, "varchar") || StringUtils.startsWith(column_type, "text") || StringUtils.startsWith(column_type, "longtext")) {
                    map.put("COLUMN_TYPE", "String");
                } else if (StringUtils.startsWith(column_type, "int") || StringUtils.startsWith(column_type, "tinyint") || StringUtils.startsWith(column_type, "smallint")) {
                    map.put("COLUMN_TYPE", "Integer");
                } else if (StringUtils.startsWith(column_type, "bigint")) {
                    map.put("COLUMN_TYPE", "Long");
                } else if (StringUtils.startsWith(column_type, "float") || StringUtils.startsWith(column_type, "double") || StringUtils.startsWith(column_type, "decimal")) {
                    map.put("COLUMN_TYPE", "Double");
                }
                map.put("COLUMN_LENGTH", !StringUtils.startsWith(column_type,"text") ?
                                column_type.substring(column_type.indexOf("(")+1, column_type.length()-1)
                                :new Integer(65535));
            }
            freeMakerList = list;
        }
        return this.freeMakerList;
    }
    
    private List<Map> getTableInfoList(){
        List<Map> resultList = new ArrayList<Map>();
        Map bwlMap = new HashMap();
        List<Map> list = getFreeMakerList();
        for (Map map : list) {
            if (!bwlMap.containsKey(map.get("TABLE_NAME"))) {
                resultList.add(map);
                bwlMap.put(map.get("TABLE_NAME"), map);
                List<Map> tempList = new ArrayList<Map>();
                map.put("columns", tempList);
            } else {
                List<Map> tempList = (List<Map>)(((Map)bwlMap.get(map.get("TABLE_NAME"))).get("columns"));
                tempList.add(map);
            }
        }
        return resultList;
    }
    
    /**
     * ���
     * @param root
     * ���ߣ��
     */
    private Map addPK(){
        
        Map root = new HashMap();
        
        String getPKSQL = "SELECT\n" +
        "    TABLE_NAME,\n" +
        "  COLUMN_NAME,\n" +
        "  REFERENCED_TABLE_NAME as R_TABLE_NAME,\n" +
        "  REFERENCED_COLUMN_NAME as R_COLUMN_NAME\n" +
        " FROM\n" +
        "    information_schema.KEY_COLUMN_USAGE\n" +
        " WHERE\n" +
        //"     TABLE_NAME = 'ac_role_qx'\n" +
        " POSITION_IN_UNIQUE_CONSTRAINT = 1\n" +
        " and TABLE_SCHEMA = '"+ Db2Java4Mysql.TABLE_SCHEMA +"'";
        
        root.put("MYFK", baseDao.queryForList(getPKSQL,new SqlParameter()));
        return root;
    }
    
    /**
     * ��ע��
     * @return
     * ���ߣ��
     */
    public Map getTableComments(){
        String sql = "SELECT\n" +
        "    TABLE_NAME,\n" +
        "    TABLE_COMMENT\n" +
        "FROM\n" +
        "    information_schema.`TABLES`\n" +
        "WHERE\n" +
        "    TABLE_SCHEMA = '"+ Db2Java4Mysql.TABLE_SCHEMA +"'";
        List<Map> list = baseDao.queryForList(sql,new SqlParameter());
        Map rootMap = new HashMap();
        for (Map map : list) {
            rootMap.put(map.get("TABLE_NAME").toString().toUpperCase(), map.get("TABLE_COMMENT"));
        }
        return rootMap;
    }
    
    private void table2Java(Configuration cfg) throws Exception {
        List<Map> list = this.getTableInfoList();
 
        Map fkMap = addPK();
        Map tableComments = getTableComments();
        
        for (Map map : list) {
            Template template = cfg.getTemplate("bean.ftl");
            
            Map rootMap = new HashMap();
            rootMap.put("DATE", DateUtil.format("yyyy-mm-dd hh:MM", DateUtil.getCurrnetDate12()));
            rootMap.put("TABLENAME", map.get("TABLE_NAME").toString().toUpperCase());
            rootMap.put("columns", map.get("columns"));
            rootMap.put("MYFK", fkMap.get("MYFK"));
            rootMap.put("COMMENTS", tableComments);
            
            File targetFile = new File(FILE_PATH+Db2Java4Mysql.PACKAGE_PATH+map.get("TABLE_NAME").toString().toUpperCase()+".java");
            if (!targetFile.exists()) {
                targetFile.getParentFile().mkdirs();
            }
            Writer writer = new java.io.BufferedWriter(new java.io.OutputStreamWriter(new FileOutputStream(targetFile), "UTF-8"));
            template.process(rootMap, writer);
            
            writer.flush();
            writer.close();
        }
    }
    
    private static void loadCommonStaticClass(Configuration cfg) {
        try {
            BeansWrapper wrapper = BeansWrapper.getDefaultInstance();
            TemplateHashModel staticModels = wrapper.getStaticModels();
//            cfg.setSharedVariable("Constants", (TemplateHashModel) staticModels.get("com.zzsb.Constants"));
            cfg.setSharedVariable("ConvertUtil", (TemplateHashModel) staticModels.get("cn.ksource.core.util.ConvertUtil"));
            cfg.setSharedVariable("FreeMarkerUtil", (TemplateHashModel) staticModels.get("cn.ksource.core.util.FreeMarkerUtil"));
            // root.put("FileUtil", (TemplateHashModel)
            // staticModels.get("com.djwl.core.utils.FileUtil"));
            cfg.setSharedVariable("DateUtil", (TemplateHashModel) staticModels.get("cn.ksource.core.util.DateUtil"));
            cfg.setSharedVariable("StringUtil", (TemplateHashModel) staticModels.get("cn.ksource.core.util.StringUtil"));
            cfg.setSharedVariable("StringUtils", (TemplateHashModel) staticModels.get("org.apache.commons.lang.StringUtils"));
            cfg.setSharedVariable("BwlUtil", (TemplateHashModel) staticModels.get("cn.ksource.core.util.BwlUtil"));
            cfg.setSharedVariable("Math", (TemplateHashModel) staticModels.get("java.lang.Math"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    @Test
    public  void create() throws Exception {
        
        System.out.println("db2java ��ʼ��....");
        
        
        Configuration cfg = FreeMarkerUtil.getConfiguration(this.getClass(), "template");
        loadCommonStaticClass(cfg);
        this.table2Java(cfg);
        
        //�������ļ���
        String path = FILE_PATH.replaceAll("/", "\\\\");
        String cmd[]={"explorer.exe",path};   
        Process p = Runtime.getRuntime().exec(cmd);
        //�������ļ���
//        String cmd[]={"cmd /c cd /d " + FILE_PATH,"jar cf bean.jar " + PACKAGE_PATH};   
//        String cmd = "cmd /c cd /d " + FILE_PATH + 
//        " && " +
//        "javac -classpath "+ System.getProperty("java.class.path") +" "+ FILE_PATH+PACKAGE_PATH + "*.java " + 
//        " && " +
//        "jar cf bean-"+TABLE_SCHEMA+"-"+ DateUtil.getCurrentDate14() +".jar " + PACKAGE_PATH+"*.class" +
//        " && " +
//        "jar cf bean-source-"+TABLE_SCHEMA+"-"+ DateUtil.getCurrentDate14() +".jar " + PACKAGE_PATH+"*.java" +
//        " && " +
//        "jar cf bean-all-"+TABLE_SCHEMA+"-"+ DateUtil.getCurrentDate14() +".jar " + PACKAGE_PATH+"*.*" 
//        ;
//        System.out.println(cmd);
//        Process p = Runtime.getRuntime().exec(cmd);
//        
//        System.out.println("db2java ִ�гɹ�");
    }
}