SpringBoot实现数据库表结构导出PDF

 实体类层

@Data
public class ColumnMeta {
    private String columnName;      // 字段名
    private String dataType;        // 数据类型
    private String columnComment;   // 字段说明
}




service层
 

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@Service
public class TableMetaService {

    @Autowired
    private DataSource dataSource;

    // 获取字段信息
    public List<ColumnMeta> getTableColumns(String tableName) {
        List<ColumnMeta> columnList = new ArrayList<>();
        String sql = "SELECT COLUMN_NAME, DATA_TYPE, COLUMN_COMMENT " +
                "FROM information_schema.COLUMNS " +
                "WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?";

        try (Connection conn = dataSource.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            String dbName = conn.getCatalog(); // 当前数据库名
            stmt.setString(1, dbName);
            stmt.setString(2, tableName);

            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                ColumnMeta meta = new ColumnMeta();
                meta.setColumnName(rs.getString("COLUMN_NAME"));
                meta.setDataType(rs.getString("DATA_TYPE"));
                meta.setColumnComment(rs.getString("COLUMN_COMMENT"));
                columnList.add(meta);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return columnList;
    }

    // 获取表注释
    public String getTableComment(String tableName) {
        String sql = "SELECT TABLE_COMMENT FROM information_schema.TABLES " +
                "WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?";
        try (Connection conn = dataSource.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            String dbName = conn.getCatalog();
            stmt.setString(1, dbName);
            stmt.setString(2, tableName);

            ResultSet rs = stmt.executeQuery();
            if (rs.next()) {
                return rs.getString("TABLE_COMMENT");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return "";
    }
}



Controller层
 

import com.ruoyi.api.ColumnMeta;
import com.ruoyi.api.PdfExportUtil;
import com.ruoyi.api.service.TableMetaService;
import com.ruoyi.common.annotation.Anonymous;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;
@Api(tags = "数据库表结构导出")
@RestController
@RequestMapping("/tool/meta")
public class TablePdfController {

    @Autowired
    private TableMetaService tableMetaService;

    @ApiOperation("导出指定表结构为 PDF")
    @GetMapping("/pdf/{tableName}")
    @Anonymous
    public void exportTablePdf(@PathVariable String tableName, HttpServletResponse response) {
        try {
            List<ColumnMeta> columns = tableMetaService.getTableColumns(tableName);
            String tableComment = tableMetaService.getTableComment(tableName);

            response.setContentType("application/pdf");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + URLEncoder.encode(tableName + "_结构文档.pdf", "UTF-8"));

            PdfExportUtil.exportTableMetadata(response.getOutputStream(), tableName, tableComment, columns);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 测试方式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值