Kaynağa Gözat

新增代码生成器JdbcGenUtils

ZhouChenglin 7 yıl önce
ebeveyn
işleme
6027619f96

+ 125 - 0
src/main/java/net/chenlin/dp/common/utils/JdbcUtils.java

@@ -0,0 +1,125 @@
+package net.chenlin.dp.common.utils;
+
+import java.sql.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * JDBC工具类
+ * @author ZhouChenglin
+ * @date 2018/1/7
+ */
+public class JdbcUtils {
+
+    /**
+     * 数据库链接
+     */
+    private Connection conn;
+
+    /**
+     * sql语句的执行对象
+     */
+    private PreparedStatement pstmt;
+
+    /**
+     * 查询返回的结果集合
+     */
+    private ResultSet rs;
+
+    /**
+     * 初始化
+     * @param driver
+     * @param url
+     * @param username
+     * @param password
+     */
+    public JdbcUtils(String driver, String url, String username, String password) {
+        try {
+            Class.forName(driver);
+            conn = DriverManager.getConnection(url, username, password);
+            System.out.println("数据库连接成功");
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * 更新数据
+     * @param sql
+     * @param params
+     * @return
+     * @throws SQLException
+     */
+    public boolean updateByParams(String sql, List params) throws SQLException {
+        // 影响行数
+        int result = -1;
+        pstmt = conn.prepareStatement(sql);
+        int index = 1;
+        // 填充sql语句中的占位符
+        if (null != params && !params.isEmpty()) {
+            for (int i = 0; i < params.size(); i ++) {
+                pstmt.setObject(index ++, params.get(i));
+            }
+        }
+        result = pstmt.executeUpdate();
+        return result > 0 ? true : false;
+    }
+
+    /**
+     * 查询多条记录
+     * @param sql
+     * @param params
+     * @return
+     * @throws SQLException
+     */
+    public List<Map> selectByParams(String sql, List params) throws SQLException {
+        List<Map> list = new ArrayList<>();
+        int index = 1;
+        pstmt = conn.prepareStatement(sql);
+        if (null != params && !params.isEmpty()) {
+            for (int i = 0; i < params.size(); i ++) {
+                pstmt.setObject(index++, params.get(i));
+            }
+        }
+        rs = pstmt.executeQuery();
+        ResultSetMetaData metaData = rs.getMetaData();
+        int cols_len = metaData.getColumnCount();
+        while (rs.next()) {
+            Map map = new HashMap();
+            for (int i = 0; i < cols_len; i ++) {
+                String cols_name = metaData.getColumnName(i + 1);
+                Object cols_value = rs.getObject(cols_name);
+                if (null == cols_value) {
+                    cols_value = "";
+                }
+                map.put(cols_name, cols_value);
+            }
+            list.add(map);
+        }
+        return list;
+    }
+
+    /**
+     * 关闭数据库资源
+     */
+    public void release() {
+        try {
+            if (null != rs) {
+                rs.close();
+            }
+            if (null != pstmt) {
+                pstmt.close();
+            }
+            if (null != conn) {
+                conn.close();
+            }
+        } catch (SQLException e) {
+            e.printStackTrace();
+        }
+        System.out.println("释放数据库连接");
+    }
+
+
+}

+ 8 - 0
src/main/java/net/chenlin/dp/modules/generator/entity/TableEntity.java

@@ -2,6 +2,7 @@ package net.chenlin.dp.modules.generator.entity;
 
 import java.io.Serializable;
 import java.sql.Timestamp;
+import java.util.ArrayList;
 import java.util.List;
 
 /**
@@ -110,5 +111,12 @@ public class TableEntity implements Serializable {
 	public void setCreateTime(Timestamp createTime) {
 		this.createTime = createTime;
 	}
+
+	public void addColumn(ColumnEntity columnEntity) {
+		if (this.columns == null) {
+			columns = new ArrayList<>();
+		}
+		columns.add(columnEntity);
+	}
 	
 }

+ 222 - 0
src/main/java/net/chenlin/dp/modules/generator/utils/JdbcGenUtils.java

@@ -0,0 +1,222 @@
+package net.chenlin.dp.modules.generator.utils;
+
+import net.chenlin.dp.common.utils.DateUtils;
+import net.chenlin.dp.common.utils.JdbcUtils;
+import net.chenlin.dp.common.utils.PropertiesUtils;
+import net.chenlin.dp.modules.generator.constant.GenConstant;
+import net.chenlin.dp.modules.generator.entity.ColumnEntity;
+import net.chenlin.dp.modules.generator.entity.TableEntity;
+import org.apache.commons.lang.StringUtils;
+import org.apache.velocity.VelocityContext;
+
+import java.io.File;
+import java.util.*;
+
+/**
+ * 代码生成工具类(使用jdbc生成本地代码)
+ * @author ZhouChenglin
+ * @date 2018/1/7
+ */
+public class JdbcGenUtils {
+
+    public static void main(String[] args) throws Exception {
+
+        String jdbcDriver = "com.mysql.jdbc.Driver";
+        String jdbcUrl = "jdbc:mysql://192.168.180.130:3306/dp-lte-boot?useUnicode=true&characterEncoding=utf-8";
+        String jdbcUsername = "root";
+        String jdbcPassword = "root";
+
+        String tablePrefix = "gen_";
+
+        String javaModule = "test";
+        String webModule = "test";
+
+        generatorCode(jdbcDriver, jdbcUrl, jdbcUsername, jdbcPassword, tablePrefix, javaModule, webModule);
+
+    }
+
+    public static void generatorCode(String jdbcDriver,
+                                     String jdbcUrl,
+                                     String jdbcUsername,
+                                     String jdbcPassword,
+                                     String tablePrefix,
+                                     String javaModule,
+                                     String webModule) throws Exception {
+        String tableSql = "SELECT table_name, table_comment FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = (SELECT DATABASE()) AND table_name LIKE '" + tablePrefix + "_%';";
+
+        JdbcUtils jdbcUtils = new JdbcUtils(jdbcDriver, jdbcUrl, jdbcUsername, jdbcPassword);
+        List<Map> tableList = jdbcUtils.selectByParams(tableSql, null);
+
+        TableEntity table = null;
+        List<TableEntity> tables = new ArrayList<>();
+
+        Iterator<Map> tableIterator = tableList.iterator();
+        while(tableIterator.hasNext()) {
+            Map<String, String> currTable = tableIterator.next();
+            table = new TableEntity();
+            String tableName = currTable.get("TABLE_NAME");
+            String className = GenUtils.tableToJava(tableName, "");
+            table.setTableName(tableName);
+            table.setClassName(className);
+            table.setObjName(StringUtils.uncapitalize(className));
+            table.setTableComment(currTable.get("TABLE_COMMENT"));
+
+            String columnSql = "SELECT column_name,data_type,column_comment,column_key,extra FROM information_schema.columns WHERE TABLE_NAME = '"+ tableName + "' AND table_schema = (SELECT DATABASE()) ORDER BY ordinal_position";
+            ColumnEntity columnEntity = null;
+            List<Map> columnList = jdbcUtils.selectByParams(columnSql,null);
+            Iterator<Map> columnIterator = columnList.iterator();
+            while(columnIterator.hasNext()){
+                Map<String, String> currColumn = columnIterator.next();
+                columnEntity = new ColumnEntity();
+                columnEntity.setExtra(currColumn.get("EXTRA"));
+
+                String columnName = currColumn.get("COLUMN_NAME");
+                String methodName = GenUtils.columnToJava(columnName);
+                columnEntity.setColumnName(columnName);
+                columnEntity.setFieldName(StringUtils.uncapitalize(methodName));
+                columnEntity.setMethodName(methodName);
+
+                columnEntity.setColumnKey(currColumn.get("COLUMN_KEY"));
+                columnEntity.setDataType(currColumn.get("DATA_TYPE"));
+                columnEntity.setColumnComment(currColumn.get("COLUMN_COMMENT"));
+
+                // 属性类型
+                columnEntity.setFieldType(PropertiesUtils.getInstance("velocity/generator").get(columnEntity.getDataType()));
+
+                // 主键判断
+                if ("PRI".equals(columnEntity.getColumnKey()) && table.getPk() == null) {
+                    table.setPk(columnEntity);
+                }
+
+                table.addColumn(columnEntity);
+            }
+            tables.add(table);
+        }
+
+        // 没主键,则第一个字段为主键
+        if (table.getPk() == null) {
+            table.setPk(table.getColumns().get(0));
+        }
+
+        String projectPath = getProjectPath();
+        System.out.println("===>>>java generation path:" + projectPath +"/src/main/java");
+        System.out.println("===>>>web generation path:" + projectPath + "/src/main/resources/templates\n");
+        Map<String, Object> map = null;
+        for (TableEntity tableEntity : tables) {
+            // 封装模板数据
+            map = new HashMap<>(16);
+            map.put("tableName", tableEntity.getTableName());
+            map.put("comments", tableEntity.getTableComment());
+            map.put("pk", tableEntity.getPk());
+            map.put("className", tableEntity.getClassName());
+            map.put("objName", tableEntity.getObjName());
+            map.put("functionCode", webModule);
+            map.put("requestMapping", tableEntity.getTableName().replace("_","/"));
+            map.put("viewPath", webModule + "/" + tableEntity.getClassName().toLowerCase());
+            map.put("authKey", GenUtils.urlToAuthKey(tableEntity.getTableName().replace("_","/")));
+            map.put("columns", tableEntity.getColumns());
+            map.put("package", PropertiesUtils.getInstance("velocity/generator").get("package"));
+            map.put("module", javaModule);
+            map.put("author", PropertiesUtils.getInstance("velocity/generator").get("author"));
+            map.put("email", PropertiesUtils.getInstance("velocity/generator").get("email"));
+            map.put("url", PropertiesUtils.getInstance("velocity/generator").get("url"));
+            map.put("datetime", DateUtils.format(new Date(), DateUtils.DATE_PATTERN_SLASH));
+            VelocityContext context = new VelocityContext(map);
+
+            System.out.println("============ start table: " + tableEntity.getTableName() + " ================");
+
+            for (String template : GenUtils.getTemplates()) {
+                String filePath = getFileName(template, javaModule, webModule, table.getClassName());
+                String templatePath = JdbcUtils.class.getResource("/"+template).getPath().replaceFirst("/", "");
+                File dstDir = new File(VelocityUtils.getPath(filePath));
+                //文件夹不存在创建文件夹
+                if(!dstDir.exists()){
+                    dstDir.mkdirs();
+                }
+                File dstFile = new File(filePath);
+                //文件不存在则创建
+                if(!dstFile.exists()){
+                    VelocityUtils.generate(templatePath, filePath, context);
+                    System.out.println(filePath + "===>>>创建成功!");
+                } else {
+                    System.out.println(filePath + "===>>>文件已存在,未重新生成!");
+                }
+            }
+            System.out.println("============ finish table: " + tableEntity.getTableName() + " ================\n");
+        }
+    }
+
+
+    public static String getFileName(String template, String javaModule, String webModule, String className) {
+        String packagePath = getProjectPath() + "/src/main/java/" + PropertiesUtils.getInstance("velocity/generator").get("package").replace(".","/") + "/modules/" + javaModule + "/";
+        String resourcePath = getProjectPath() + "/src/main/resources/";
+        String webPath = resourcePath + "templates/";
+        if (template.contains(GenConstant.JAVA_ENTITY)) {
+            return packagePath + "entity/" + className + "Entity.java";
+        }
+
+        if (template.contains(GenConstant.JAVA_MAPPER)) {
+            return packagePath + "dao/" + className + "Mapper.java";
+        }
+
+        if (template.contains(GenConstant.XML_MAPPER)) {
+            return resourcePath + "mapper/" + javaModule + "/" + className + "Mapper.xml";
+        }
+
+        if (template.contains(GenConstant.JAVA_MANAGER)) {
+            return packagePath + "manager/" + className + "Manager.java";
+        }
+
+        if (template.contains(GenConstant.JAVA_MANAGER_IMPL)) {
+            return packagePath + "manager/impl/" + className + "ManagerImpl.java";
+        }
+
+        if (template.contains(GenConstant.JAVA_SERVICE)) {
+            return packagePath + "service/" + className + "Service.java";
+        }
+
+        if (template.contains(GenConstant.JAVA_SERVICE_IMPL)) {
+            return packagePath + "service/impl/" + className + "ServiceImpl.java";
+        }
+
+        if (template.contains(GenConstant.JAVA_CONTROLLER)) {
+            return packagePath + "controller/" + className + "Controller.java";
+        }
+
+        if (template.contains(GenConstant.HTML_LIST)) {
+            return webPath + webModule + "/" + className.toLowerCase() + "/list.html";
+        }
+
+        if (template.contains(GenConstant.HTML_ADD)) {
+            return webPath + webModule + "/" + className.toLowerCase() + "/add.html";
+        }
+
+        if (template.contains(GenConstant.HTML_EDIT)) {
+            return webPath + webModule + "/" + className.toLowerCase() + "/edit.html";
+        }
+
+        if (template.contains(GenConstant.JS_LIST)) {
+            return resourcePath + "static/js/" + webModule + "/" + className.toLowerCase() + "/list.js";
+        }
+
+        if (template.contains(GenConstant.JS_ADD)) {
+            return resourcePath + "static/js/" + webModule + "/" + className.toLowerCase()  + "/add.js";
+        }
+
+        if (template.contains(GenConstant.JS_EDIT)) {
+            return resourcePath + "static/js/" + webModule + "/" + className.toLowerCase()  + "/edit.js";
+        }
+
+        if (template.contains(GenConstant.SQL_MENU)) {
+            return resourcePath + "sqls/" + className.toLowerCase() + "_menu.sql";
+        }
+
+        return null;
+    }
+
+    public static String getProjectPath() {
+        String basePath = JdbcUtils.class.getResource("/").getPath().replace("/target/classes/", "").replaceFirst("/", "");
+        return basePath;
+    }
+
+}