瀏覽代碼

新增项目详细文档输出

赵冬冬 3 年之前
父節點
當前提交
5f86c271ca

+ 0 - 39
src/main/java/com/dwl/mindoc/database/BaseFactory.java

@@ -14,46 +14,7 @@ import org.springframework.stereotype.Component;
 @Component
 public class BaseFactory {
 
-    @Autowired
-    private MinConfig config;
 
-    private enum BaseType{
-
-        MySQL,Oracle,PostgreSQL,SQLite,SQLServer
-
-    }
-
-
-    /**
-     * 获取数据库信息
-     * @return
-     */
-    public Database getDataBase() throws Exception {
-        Database dataBase = null;
-        String baseType = config.getValue("baseType");
-        String baseName = config.getValue("baseName");
-        BaseType type =BaseType.valueOf(baseType);
-        switch (type) {
-            case MySQL:
-                dataBase = new MySQL(baseName);
-                break;
-            case Oracle:
-                dataBase = new Oracle(baseName);
-                break;
-            case PostgreSQL:
-                dataBase = new PostgreSQL();
-                break;
-            case SQLite:
-                dataBase = new SQLite();
-                break;
-            case SQLServer:
-                dataBase = new SQLServer(baseName);
-                break;
-            default:
-                throw new Exception("Not Support BaseType: "+baseType);
-        }
-        return dataBase;
-    }
 }
 
 

+ 51 - 0
src/main/java/com/dwl/mindoc/service/DBGenerDocService.java

@@ -0,0 +1,51 @@
+package com.dwl.mindoc.service;
+
+import com.dwl.mindoc.MinConfig;
+import com.dwl.mindoc.database.Database;
+import com.dwl.mindoc.database.impl.*;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+@Service
+public class DBGenerDocService {
+    @Autowired
+    private MinConfig config;
+
+    private enum BaseType{
+
+        MySQL,Oracle,PostgreSQL,SQLite,SQLServer
+
+    }
+
+
+    /**
+     * 获取数据库信息
+     * @return
+     */
+    public Database getDataBase() throws Exception {
+        Database dataBase = null;
+        String baseType = config.getValue("baseType");
+        String baseName = config.getValue("baseName");
+        BaseType type =BaseType.valueOf(baseType);
+        switch (type) {
+            case MySQL:
+                dataBase = new MySQL(baseName);
+                break;
+            case Oracle:
+                dataBase = new Oracle(baseName);
+                break;
+            case PostgreSQL:
+                dataBase = new PostgreSQL();
+                break;
+            case SQLite:
+                dataBase = new SQLite();
+                break;
+            case SQLServer:
+                dataBase = new SQLServer(baseName);
+                break;
+            default:
+                throw new Exception("Not Support BaseType: "+baseType);
+        }
+        return dataBase;
+    }
+}

+ 1 - 1
src/main/java/com/dwl/mindoc/service/GenerateService.java

@@ -48,7 +48,7 @@ public class GenerateService {
     private Logger logger = LoggerFactory.getLogger(getClass());
 
     @Autowired
-    private BaseFactory factory;
+    private DBGenerDocService factory;
 
     @Autowired
     private BaseDao dao;

+ 36 - 0
src/main/java/com/dwl/mindoc/service/ProjectModelDocService.java

@@ -0,0 +1,36 @@
+package com.dwl.mindoc.service;
+
+import com.alibaba.fastjson.JSON;
+import com.dwl.mindoc.wordutil.WordUtil;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.stereotype.Service;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
+
+@Service
+public class ProjectModelDocService {
+
+    public void generDoc() throws IOException {
+        ClassPathResource classPathResource = new ClassPathResource("json/mean.json");
+        InputStream config = classPathResource.getInputStream();
+        Map<String, Object> json = new HashMap();
+        if (config == null) {
+            throw new RuntimeException("读取文件失败");
+        } else {
+            json = JSON.parseObject(config, Map.class);
+            System.out.println(json);
+        }
+        //文件路径
+        String filePath = "D://doc";
+
+        //文件名称
+        String fileName = System.currentTimeMillis() + "mean.doc";
+
+        /** 生成word */
+
+        WordUtil.createWord(json, "softinfo.ftl", filePath, fileName);
+    }
+}

+ 42 - 0
src/main/java/com/dwl/mindoc/test/MainTest.java

@@ -0,0 +1,42 @@
+package com.dwl.mindoc.test;
+
+import com.alibaba.fastjson.JSON;
+import com.dwl.mindoc.service.DBGenerDocService;
+import com.dwl.mindoc.service.ProjectModelDocService;
+import com.dwl.mindoc.wordutil.WordUtil;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.text.ParseException;
+import java.util.HashMap;
+import java.util.Map;
+
+@RunWith(SpringRunner.class)
+@SpringBootTest
+public class MainTest {
+
+
+    @Autowired
+    private DBGenerDocService dbGenerDocService;
+
+    @Autowired
+    private ProjectModelDocService projectModelDocService;
+
+
+    @Test
+    public void ProjectModelDoc() throws ParseException, IOException {
+        projectModelDocService.generDoc();
+    }
+
+    @Test
+    public void DBInfoDoc() throws Exception {
+        dbGenerDocService.getDataBase();
+    }
+
+}

+ 124 - 0
src/main/java/com/dwl/mindoc/wordutil/WordUtil.java

@@ -0,0 +1,124 @@
+package com.dwl.mindoc.wordutil;
+
+import freemarker.cache.ClassTemplateLoader;
+import freemarker.template.Configuration;
+import freemarker.template.Template;
+import org.springframework.core.io.ClassPathResource;
+import sun.misc.BASE64Encoder;
+
+import java.io.*;
+import java.util.Map;
+
+public class WordUtil {
+
+    /**
+     * @param dataMap
+     *            word中需要展示的动态数据,用map集合来保存
+     * @param templateName
+     *            word模板名称,例如:teample.ftl
+     * @param filePath
+     *            文件生成的目标路径,例如:D:/
+     * @param fileName
+     *            生成的文件名称
+     */
+    @SuppressWarnings("unchecked")
+    public static void createWord(Map dataMap, String templateName,
+                                  String filePath, String fileName) {
+        try {
+            // 创建配置实例
+            Configuration configuration = new Configuration();
+
+            // 设置编码
+            configuration.setDefaultEncoding("UTF-8");
+
+            // ftl模板文件
+            configuration.setTemplateLoader(new ClassTemplateLoader(WordUtil.class,"/word/"));
+            // 获取模板
+            Template template = configuration.getTemplate(templateName);
+            // 输出文件
+            File outFile = new File(filePath + File.separator + fileName);
+
+            // 如果输出目标文件夹不存在,则创建
+            if (!outFile.getParentFile().exists()) {
+                outFile.getParentFile().mkdirs();
+            }
+
+            // 将模板和数据模型合并生成文件
+            Writer out = new BufferedWriter(new OutputStreamWriter(
+                    new FileOutputStream(outFile), "UTF-8"));
+
+            // 生成文件
+            template.process(dataMap, out);
+
+            // 关闭流
+            out.flush();
+            out.close();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+    }
+
+
+    /**
+     * 将图片转换为BASE64为字符串
+     * @param filename
+     * @return
+     * @throws IOException
+     */
+    public static String getImageStringByDir(String filename) throws IOException {
+        InputStream in = null;
+        byte[] data = null;
+        try {
+            in = new FileInputStream(filename);
+            data = new byte[in.available()];
+            in.read(data);
+            in.close();
+        } catch (IOException e) {
+            throw e;
+        } finally {
+            if(in != null) {
+                in.close();
+            }
+        }
+        BASE64Encoder encoder = new BASE64Encoder();
+        return data != null ? encoder.encode(data) : "";
+    }
+    /**
+     * 将图片转换为BASE64为字符串
+     * @param filename
+     * @return
+     * @throws IOException
+     */
+    public static String getImageStringByProject(String filename) throws IOException {
+        ClassPathResource classPathResource = new ClassPathResource(filename);
+        InputStream in = null;
+        byte[] data = null;
+        try {
+            in = classPathResource.getInputStream();
+            data = new byte[in.available()];
+            in.read(data);
+            in.close();
+        } catch (IOException e) {
+            throw e;
+        } finally {
+            if(in != null) {
+                in.close();
+            }
+        }
+        BASE64Encoder encoder = new BASE64Encoder();
+        return data != null ? encoder.encode(data) : "";
+    }
+
+    public static byte[] toByteArray(InputStream input) throws IOException {
+        ByteArrayOutputStream output = new ByteArrayOutputStream();
+        byte[] buffer = new byte[4096];
+        int n = 0;
+        while (-1 != (n = input.read(buffer))) {
+            output.write(buffer, 0, n);
+        }
+        return output.toByteArray();
+    }
+
+
+}

+ 3 - 3
src/main/resources/application.properties

@@ -1,7 +1,7 @@
 #\u6570\u636E\u5E93\u94FE\u63A5\u4FE1\u606F Mysql
-spring.datasource.url=jdbc:mysql://39.100.111.244:3306/carbon-platform?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT
+spring.datasource.url=jdbc:mysql://172.16.90.64:3306/ac_db?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT
 spring.datasource.username=root
-spring.datasource.password=Hywa@123
+spring.datasource.password=123456
 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
 #\u6570\u636E\u5E93\u94FE\u63A5\u4FE1\u606F MSSql
 #spring.datasource.url=jdbc:sqlserver://server_ip:port;DatabaseName=dbname
@@ -26,4 +26,4 @@ baseType=MySQL
 fileType=word
 
 #\u6570\u636E\u5E93\u5B9E\u4F8B\u540D
-baseName=carbon-platform
+baseName=ac_db

文件差異過大導致無法顯示
+ 814 - 0
src/main/resources/json/mean.json


文件差異過大導致無法顯示
+ 2452 - 0
src/main/resources/word/softinfo.ftl