数据库结构文档导出

总体思路:
1.获得所有的表名
2.根据表名找出表结构
3.将找出的表结构进行导出

下面依次列出 实体类–>controller–>mapper.xm–>sql

a.数据库结构的实体类

public class DbFormatEntity {
    private String columnName;//列名
    private String columnType;//数据类型
    private String dateType;//字段类型
    private String length;//长度
    private String isnullable;//是否为空
    private String columnDefault;//默认值
    private String columnComment;//备注
    private String tableSchema;//数据库名
    private String tableName;//表名

    public String getColumnName() {
        return columnName;
    }

    public void setColumnName(String columnName) {
        this.columnName = columnName;
    }

    public String getColumnType() {
        return columnType;
    }

    public void setColumnType(String columnType) {
        this.columnType = columnType;
    }

    public String getDateType() {
        return dateType;
    }

    public void setDateType(String dateType) {
        this.dateType = dateType;
    }

    public String getLength() {
        return length;
    }

    public void setLength(String length) {
        this.length = length;
    }

    public String getIsnullable() {
        return isnullable;
    }

    public void setIsnullable(String isnullable) {
        this.isnullable = isnullable;
    }

    public String getColumnDefault() {
        return columnDefault;
    }

    public void setColumnDefault(String columnDefault) {
        this.columnDefault = columnDefault;
    }

    public String getColumnComment() {
        return columnComment;
    }

    public void setColumnComment(String columnComment) {
        this.columnComment = columnComment;
    }

    public String getTableSchema() {
        return tableSchema;
    }

    public void setTableSchema(String tableSchema) {
        this.tableSchema = tableSchema;
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }
}

b.controller

@Controller
@RequestMapping(value = "/dbExport")
public class DbExportController {
    @Autowired
    private DbExportService dbExportService;


    @RequestMapping("/export")
    public void export(HttpServletResponse response) throws IOException {

        String dbName = "db_name";//数据库名
        List<String> tablesnameList = dbExportService.getAllTablesName(dbName);//获取所有表名(这里的sql在下面)
        Map<String,List<DbFormatEntity>> map = new LinkedHashMap<>();//存放表名,表结构实体
        if (tablesnameList != null && tablesnameList.size() > 0) {
            for (String tablename : tablesnameList) {
                List<DbFormatEntity> dbFormatEntityList = dbExportService.getTableFormat(dbName, tablename);//获得表结构
                map.put(tablename,dbFormatEntityList);
            }
        }
        String fileName =dbName+"数据库文档.xls";
        // 创建一个webbook,对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        // 在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet(dbName + "数据库文档");

        // 设置单元格样式
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
//        style.setFillForegroundColor(HSSFColor.GREY_40_PERCENT.index);
        sheet.autoSizeColumn(0, true);
        sheet.autoSizeColumn(1, true);
        sheet.autoSizeColumn(2, true);
        sheet.autoSizeColumn(3, true);
        sheet.autoSizeColumn(4, true);
        sheet.autoSizeColumn(5, true);
        sheet.autoSizeColumn(6, true);

        //第一行统计表名
        HSSFRow headRow = sheet.createRow((int) 0);//创建第一行
        headRow.setHeightInPoints(20);//设置高度
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));//合并单元格
        Cell titleCell = headRow.createCell(0);//创建一个单元格
        titleCell.setCellStyle(style);//设置单元格样式
        titleCell.setCellValue(dbName + "数据库文档");//第一行单元格填写内容


        int line = 1;
        for(String key:map.keySet()) {
            List<DbFormatEntity> dbFormatList = (List<DbFormatEntity>) map.get(key);//获取一张表的详情
            HSSFRow tableNameRow = sheet.createRow((int) line);//创建第二行
            headRow.setHeightInPoints(20);//设置高度
            sheet.addMergedRegion(new CellRangeAddress(line, line, 0, 6));//合并单元格
            Cell tableNameRowCell = tableNameRow.createCell(0);//创建一个单元格
            tableNameRowCell.setCellStyle(style);//设置单元格样式
            tableNameRowCell.setCellValue(key);//第一行单元格填写内容
            line++;
            //第3行
            HSSFRow row = sheet.createRow(line);
            HSSFCell cell = null;
            cell = row.createCell(0);
            cell.setCellValue("列名");
            cell.setCellStyle(style);
            cell = row.createCell(1);
            cell.setCellValue("数据类型");
            cell.setCellStyle(style);
            cell = row.createCell(2);
            cell.setCellValue("字段类型");
            cell.setCellStyle(style);
            cell = row.createCell(3);
            cell.setCellValue("长度");
            cell.setCellStyle(style);
            cell = row.createCell(4);
            cell.setCellValue("是否为空");
            cell.setCellStyle(style);
            cell = row.createCell(5);
            cell.setCellValue("默认值");
            cell.setCellStyle(style);
            cell = row.createCell(6);
            cell.setCellValue("备注");
            cell.setCellStyle(style);
            line++;
            for (int j = 0; j < dbFormatList.size(); j++) {
                row = sheet.createRow(line);
                //列名
                cell = row.createCell(0);
                cell.setCellValue(dbFormatList.get(j).getColumnName());
                cell.setCellStyle(style);

                //数据类型
                cell = row.createCell(1);
                cell.setCellValue(dbFormatList.get(j).getColumnType());
                cell.setCellStyle(style);

                //字段类型
                cell = row.createCell(2);
                cell.setCellValue(dbFormatList.get(j).getDateType());
                cell.setCellStyle(style);

                //长度
                cell = row.createCell(3);
                cell.setCellValue(dbFormatList.get(j).getLength());
                cell.setCellStyle(style);

                //是否为空
                cell = row.createCell(4);
                cell.setCellValue(dbFormatList.get(j).getIsnullable());
                cell.setCellStyle(style);

                //默认值
                cell = row.createCell(5);
                cell.setCellValue(dbFormatList.get(j).getColumnDefault());
                cell.setCellStyle(style);

                //备注
                cell = row.createCell(6);
                cell.setCellValue(dbFormatList.get(j).getColumnComment());
                cell.setCellStyle(style);
                line++;

            }
//            line = line + dbFormatList.size() +1;
            line++;
        }

        //响应到客户端
        OutputStream output=response.getOutputStream();
        response.reset();
        response.setContentType("application/octet-stream;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename="
                + new String(fileName.getBytes(),"iso-8859-1"));
        response.setContentType("application/msexcel");
        wb.write(output);
        output.close();
    }
}

C.
(1).获得某库所有表名的sql
mapper.xml中 ,入参是库名

List<String> getAllTablesName(@Param("dbName") String dbName);

对应的sql:

<select id="getAllTablesName" resultType="java.lang.String">
        SELECT
        table_name
        FROM
            information_schema. TABLES
        WHERE
            TABLE_SCHEMA = #{dbName}

(2).根据表名查询表结构:
mapper.xml中,入参是库名,表名

List<DbFormatEntity> getTableFormat(@Param("dbName") String dbName, @Param("tableName") String tablename);

对应的sql:

<select id="getTableFormat" resultType="com.healthcloud.entity.DbFormatEntity">
        SELECT
        COLUMN_NAME columnName,
        COLUMN_TYPE columnType,
        DATA_TYPE dateType,
        CHARACTER_MAXIMUM_LENGTH length,
        IS_NULLABLE isnullable,
        COLUMN_DEFAULT columnDefault,
        COLUMN_COMMENT columnComment
        FROM
        INFORMATION_SCHEMA.COLUMNS
        where
        table_schema =#{dbName}
        AND
        table_name  = #{tableName}
    </select>

猜你喜欢

转载自blog.csdn.net/qq_32090861/article/details/80949243