java 达梦数据库结构 转为word文档


前言

达梦数据库结构转为word文档


一、spring boot 适配达梦

1. 手动引入达梦jdbc驱动包,复制jar包到resources的lib目录下

在这里插入图片描述

2. pom.xml中增加依赖,打包插件添加配置

 <dependency>
     <groupId>com.dm</groupId>
     <artifactId>Dm7JdbcDriver17</artifactId>
     <version>1.7</version>
     <scope>system</scope>
     <systemPath>${project.basedir}/src/main/resources/lib/Dm7JdbcDriver17.jar</systemPath>
 </dependency>
   <plugin>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-maven-plugin</artifactId>
       <configuration>
       	   <!-- 打包包含达梦jdbc驱动jar包 -->
           <includeSystemScope>true</includeSystemScope>
       </configuration>
   </plugin>

3. application.yml里添加达梦数据库的配置

spring:
  datasource:
    driver-class-name: dm.jdbc.driver.DmDriver
    url: jdbc:dm://192.168.1.33:5237/DMDEMO
    username: DMDEMO
    password: DMDEMO

二、添加mybatis-plus、druid

1.添加maven依赖

 <!--druid 连接池-->
 <dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>druid-spring-boot-starter</artifactId>
     <version>1.2.16</version>
 </dependency>

 <!-- mybatis-plus -->
 <dependency>
     <groupId>com.baomidou</groupId>
     <artifactId>mybatis-plus-boot-starter</artifactId>
     <version>3.5.3.1</version>
 </dependency>
 <dependency>
     <groupId>com.baomidou</groupId>
     <artifactId>mybatis-plus</artifactId>
     <version>3.5.3.1</version>
 </dependency>

2. application.yml配置mybatis-plus

#配置mapper xml文件的路径
mybatis-plus:
  mapper-locations: classpath:mapping/*.xml

三、编写生成word代码

1. entity

import lombok.Data;

@Data
public class DmTableInfo {
    private String tableName;
    private String tableComment;
}
import lombok.Data;

@Data
public class DmTableField {
    private String columnName;
    private String dataType;
    private String dataLength;
    private String nullable;
    private String comments;
}

2. Mapper

import com.example.dmword.table.entity.DmTableField;
import com.example.dmword.table.entity.DmTableInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface DmTableMapper {

    List<DmTableInfo> findAllTables(@Param("schemaName") String schemaName);

    List<DmTableField> findFieldsByTableName(@Param("schemaName") String schemaName, 
                                             @Param("tableName") String tableName);

}

3. xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.dmword.table.mapper.DmTableMapper">

    <select id="findAllTables" resultType="com.example.dmword.table.entity.DmTableInfo">
        select
            a.table_name as tableName,
            b.comments as tableComment
        from
            dba_tables a
        LEFT OUTER JOIN USER_TAB_COMMENTS b on a.table_name = b.table_name
        where
            a.owner = #{schemaName}
        order by
            a.table_name
    </select>

    <select id="findFieldsByTableName" resultType="com.example.dmword.table.entity.DmTableField">
        select
            a.column_name as columnName,
            a.data_type as dataType,
            a.data_length as dataLength,
            a.nullable as nullable,
            b.comments as comments
        from
            user_tab_columns a
        LEFT OUTER JOIN ALL_COL_COMMENTS b on (a.Table_Name = b.Table_Name and a.column_name = b.column_name)
        where
            a.Table_Name = #{tableName}
        and b.schema_name = #{schemaName}
        order by
            a.column_id
    </select>

</mapper>

4. service

import com.example.dmword.table.entity.DmTableField;
import com.example.dmword.table.entity.DmTableInfo;
import com.example.dmword.table.mapper.DmTableMapper;
import com.lowagie.text.Font;
import com.lowagie.text.*;
import com.lowagie.text.rtf.RtfWriter2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

@Service
@Slf4j
public class DmTableService {
    private final static String SCHEMA_NAME = "SCHEMA_NAME";
    private final static String ROOT_PATH = "E:\\src";
    private final static String FILE_NAME = "数据库设计文档.doc";

    private final DmTableMapper dmTableMapper;

    public DmTableService(DmTableMapper dmTableMapper) {
        this.dmTableMapper = dmTableMapper;
    }

    public void tableInfo2Word() {
        List<DmTableInfo> tables = dmTableMapper.findAllTables(SCHEMA_NAME);
        Document document = new Document(PageSize.A4);
        try {
            // 创建文件
            File file = createFile();

            // 写入文件信息
            RtfWriter2.getInstance(document, new FileOutputStream(file));
            document.open();

            // 文档标题
            setDocTitle(document);

            int i = 0;
            for (DmTableInfo tableInfo : tables) {
                // 标题
                setTableTitle(document, tableInfo, ++i);

                // 创建表格
                Table table = getTable();
                // 设置表格头部
                setTableHeader(table);
                // 表格的主体
                setTableBody(tableInfo.getTableName(), table);
                // 生成表格
                document.add(table);

                // 换行
                document.add(new Paragraph(""));
            }

            document.close();
        } catch (Exception e) {
            log.error("生成数据库设计文档失败!", e);
        }
    }

    private void setDocTitle(Document document) throws DocumentException {
        Font font = new Font(Font.NORMAL, 24, Font.NORMAL, new Color(0, 0, 0));
        Paragraph paragraph = new Paragraph("数据库设计文档", font);
        paragraph.setAlignment(1);
        document.add(paragraph);
    }

    private File createFile() throws IOException {
        File dir = new File(ROOT_PATH);
        boolean result = dir.mkdirs();
        log.info("创建根目录结果:{}", result);

        // 创建文件
        File file = new File(ROOT_PATH + File.separator + FILE_NAME);
        if (file.exists() && file.isFile()) {
            boolean deleteFlag = file.delete();
            log.info("删除旧doc文件结果:{}", deleteFlag);
        }
        result = file.createNewFile();
        log.info("创建文件结果:{}", result);
        return file;
    }

    private void setTableTitle(Document document, DmTableInfo tableInfo, int num) throws DocumentException {
        String tableComment = tableInfo.getTableComment();
        tableComment = tableComment == null ? "" : "(" + tableComment + ")";
        String tableTitle = num + ". 表名称: " + tableInfo.getTableName() + tableComment;
        document.add(new Paragraph(tableTitle));
    }

    private Table getTable() throws DocumentException {
        Table table = new Table(5);
        // 表格宽度占比
        table.setWidth(90.0f);

        // 每列宽度
        int[] widths = {5, 30, 20, 10, 30};
        table.setWidths(widths);

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

    private void setTableHeader(Table table) {
        //添加表头的元素,并设置表头背景的颜色
        Color color = new Color(176, 196, 222);

        Cell cell = new Cell("编号");
        addTableHeader(table, cell, color);
        cell = new Cell("字段名");
        addTableHeader(table, cell, color);
        cell = new Cell("类型");
        addTableHeader(table, cell, color);
        cell = new Cell("非空");
        addTableHeader(table, cell, color);
        cell = new Cell("注释");
        addTableHeader(table, cell, color);

        // 表头结束
        table.endHeaders();
    }

    private void setTableBody(String tableName, Table table) {
        List<DmTableField> fields = dmTableMapper.findFieldsByTableName(SCHEMA_NAME, tableName);
        for (int i = 0; i < fields.size(); i++) {
            addContent(table, (i + 1) + "");
            addContent(table, fields.get(i).getColumnName());

            String type = fields.get(i).getDataType();
            if (!"CLOB".equals(type)) {
                type += "(" + fields.get(i).getDataLength() + ")";
            }
            addContent(table, type);

            addContent(table, "Y".equals(fields.get(i).getNullable()) ? "否" : "是");
            addContent(table, fields.get(i).getComments());
        }
    }

    /**
     * 添加表头到表格
     */
    private void addTableHeader(Table table, Cell cell, Color color) {
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setBackgroundColor(color);
        table.addCell(cell);
    }

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

}


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值