0
点赞
收藏
分享

微信扫一扫

86 springboot导出数据库表信息生成Word文档

攻城狮Chova 2021-09-21 阅读 48

pom

    <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>2.2.0</version>
        </dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
    <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
<!-- ================== 将数据库表信息生成word文档信息所需 ====================== -->
        <!-- https://mvnrepository.com/artifact/com.lowagie/itext -->
        <dependency>
            <groupId>com.lowagie</groupId>
            <artifactId>itext</artifactId>
            <version>2.1.7</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.itextpdf/itext-asian -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>
<!-- https://mvnrepository.com/artifact/com.lowagie/itext-rtf -->
        <dependency>
            <groupId>com.lowagie</groupId>
            <artifactId>itext-rtf</artifactId>
            <version>2.1.7</version>
        </dependency>





2,yml

spring:
  application:
       name: service-zuul
  datasource:
     # type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false
        username: root
        password: root
server:
  port:  8009
logging:
  level:
    com.taotao.springbootword.mapper: debug



3,controller


import com.taotao.servicezuul.service.DataSourceDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController
import java.util.List;
import java.util.Map;

/**
 * 生成数据库表设计文档
 */
@RestController
@RequestMapping("/myTest")
public class DataSourceDetailController {
    @Autowired
    private DataSourceDetailService dataSourceDetailService;

   /**
     * 生成数据库表设计文档
     *
     * @param dbName
     * @return
     */
    @RequestMapping("/getDbDetail")
    public String getDbDetail(String dbName) {
        try {
            List<Map<String, Object>> list = this.dataSourceDetailService.getAllDataSourceName(dbName);
            this.dataSourceDetailService.toWord(list);
        }catch (Exception e){
            e.getCause();
            return  "生成数据库表设计文档失败";
        }
   return "生成数据库表设计文档成功";
    }

}





2,service

package com.taotao.servicezuul.service;

import java.util.List;
import java.util.Map;

public interface DataSourceDetailService {
    /**
     * 描述 根据表名称获取表的详细信息
     * @param tableName
     * @return
     */
    List<Map<String,Object>> getDataSourceDetail(String tableName);

    /**
     * 根据数据库名称获取表的详细信息
     * @param dbName
     * @return
     */
    List<Map<String,Object>> getAllDataSourceName(String dbName);

    /**
     * 讲数据写出到指定的world文档
     * @param list
     */
 void toWord(List<Map<String, Object>> list);
}


3.serviceImpl

package com.taotao.servicezuul.service.impl;

import com.lowagie.text.Font;
import com.lowagie.text.*;
import com.lowagie.text.rtf.RtfWriter2;
import com.taotao.servicezuul.mapper.DataSourceMapper;
import com.taotao.servicezuul.service.DataSourceDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;

@Service
public class DataSourceDetailServiceImpl implements DataSourceDetailService {
    @Autowired
    private DataSourceMapper dataSourceMapper;

 @Override
    public List<Map<String, Object>> getDataSourceDetail(String tableName) {
        return dataSourceMapper.getDataDetail(tableName);
    }

    @Override
    public List<Map<String, Object>> getAllDataSourceName(String dbName) {
        return dataSourceMapper.getAllDataSourceName(dbName);
    }

   @Override
    public void toWord(List<Map<String, Object>> tables) {
        Document document = new Document(PageSize.A4);
        try {
            // 创建文件
            String  fileName="D:/data/dbDetail.doc";
        File file = new File(fileName);
            if (file.exists() && file.isFile()) {
                file.delete();
            }

    file.createNewFile();

            // 写入文件信息
            RtfWriter2.getInstance(document, new FileOutputStream(fileName));
            document.open();
  Paragraph ph = new Paragraph();
            Font f = new Font();
            Paragraph p = new Paragraph("数据库表设计文档", new Font(Font.NORMAL, 24, Font.BOLDITALIC, new Color(0, 0, 0)));
            p.setAlignment(1);
            document.add(p);
         ph.setFont(f);
            for (int i = 0; i < tables.size(); i++) {
                String table_name = (String) tables.get(i).get("name");
                // 表说明
                String table_comment = (String) tables.get(i).get("comment");
       //获取某张表的所有字段说明
                List<Map<String, Object>> list = this.getDataSourceDetail(table_name);
                String all = "" + (i + 1) + " 表名称:" + table_name + "(" + table_comment + ")";
                Table table = new Table(6);
     document.add(new Paragraph(""));

                table.setBorderWidth(1);
                table.setPadding(0);
                table.setSpacing(0);

   //添加表头的元素,并设置表头背景的颜色
                Color chade = new Color(176, 196, 222);

                Cell cell = new Cell("编号");
                addCell(table, cell, chade);
                cell = new Cell("字段名");
               addCell(table, cell, chade);
                cell = new Cell("类型");
                addCell(table, cell, chade);
                cell = new Cell("是否非空");
                addCell(table, cell, chade);
     cell = new Cell("是否主键");
                addCell(table, cell, chade);
                cell = new Cell("注释");
                addCell(table, cell, chade);

     table.endHeaders();

                // 表格的主体
                for (int k = 0; k < list.size(); k++) {
                    addContent(table, cell, (k + 1) + "");
                    addContent(table, cell,(String) list.get(k).get("Field"));
      addContent(table, cell,(String) list.get(k).get("Type"));
                    addContent(table, cell,  list.get(k).get("Null").equals("YES") ? "否" : "是");
                    addContent(table, cell,  list.get(k).get("Key") != "" ? "是" : "否");
         addContent(table, cell, (String) list.get(k).get("Comment"));
                }
                Paragraph pheae = new Paragraph(all);
                //写入表说明
                document.add(pheae);
                //生成表格
              document.add(table);
            }
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

  /**
     * 添加表头到表格
     *
     * @param table
     * @param cell
     * @param chade
     */
    private void addCell(Table table, Cell cell, Color chade) {
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setBackgroundColor(chade);
        table.addCell(cell);
    }
   /**
     * 添加内容到表格
     *
     * @param table
     * @param content
     */
    private void addContent(Table table, Cell cell, String content) {
        cell = new Cell(content);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);
    }

}

mapper

package com.taotao.servicezuul.mapper;




import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.*;
@Mapper
public interface DataSourceMapper {
    /**
     * 根据表名获取详细信息
     * @param tableName
     * @return
     */
 @Select("SHOW FULL FIELDS FROM ${tableName}")
    List<Map<String, Object>> getDataDetail(@Param("tableName") String tableName);
   /**
     * 根据数据库名称获取数据库中表的名称和注释
     * @param dbName
     * @return
     */

    @Select("select table_name as name,table_comment as comment from information_schema.tables where table_schema =#{dbName} order by table_name")
   List<Map<String,Object>> getAllDataSourceName(@Param("dbName")String dbName);
}

访问 http://localhost:8009/myTest/getDbDetail?dbName=test
这里test 是数据库的库名称

代码 : https://github.com/wangjin123456/2020/tree/master/springboot/springbootword

举报

相关推荐

0 条评论