`

获取指定表的表结构

 
阅读更多

最近公司项目中需要做一个使用excel员工导入的需求,在导入过程中,需要对数据核验,就需要获取相应的表结构的字段类型,及其类型精度。以下为代码:

 

    private Map<String, TableColumnInfo> getColumnInfo() {
        Map<String, String> condition = new HashMap<String, String>();
	// 参数值只能是小写
        condition.put("tableSchema", "public");
        condition.put("tableName", "***");
        List<Map<String, Object>> schemaList = sqlSession.selectList("com.*.*.*.getTableSchemaByCondition", condition);
        
        Map<String, TableColumnInfo> columnMap = new HashMap<String, TableColumnInfo>();
        for (Map<String, Object> map : schemaList) {
            TableColumnInfo column = new TableColumnInfo();
            column.setTableName(String.valueOf(map.get("table_name")));
            column.setColName(String.valueOf(map.get("column_name")));
            column.setColType(String.valueOf(map.get("data_type")));
            column.setNullable(String.valueOf(map.get("is_nullable")));
            if (map.get("character_maximum_length") == null) {
                column.setColLegth(0);
            } else {
                column.setColLegth(Integer.valueOf(String.valueOf(map.get("character_maximum_length"))));
            }
            if (map.get("numeric_precision") == null) {
                column.setNumericPrecision(0);
            } else {
                column.setNumericPrecision(Integer.valueOf(String.valueOf(map.get("numeric_precision"))));
            }
            if (map.get("numeric_scale") == null) {
                column.setNumericScale(0);
            } else {
                column.setNumericScale(Integer.valueOf(String.valueOf(map.get("numeric_scale"))));
            }
            
            columnMap.put(column.getColName(), column);
        }
        return columnMap;
    }

 

public class TableColumnInfo {
    private String  tableName;
    private String  colName;
    private String  colType;
    private int     colLegth;
    private boolean nullable;
    private int     numericPrecision;
    private int     numericScale;

    public String toString() {
        return String.format("%s %s(%s)", new Object[] { this.colName, this.colType, Integer.valueOf(this.colLegth) });
    }

    public String getColName() {
        return this.colName;
    }

    public void setColName(String colName) {
        this.colName = colName;
    }

    public String getColType() {
        return this.colType;
    }

    public void setColType(String colType) {
        this.colType = colType;
    }

    public int getColLegth() {
        return this.colLegth;
    }

    public void setColLegth(int colLegth) {
        this.colLegth = colLegth;
    }

    public String getTableName() {
        return this.tableName;
    }

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

    public boolean isNullable() {
        return this.nullable;
    }

    public boolean getNullable() {
        return this.nullable;
    }

    public void setNullable(String nullable) {
        if ("YES".equals(nullable))
            this.nullable = true;
        else
            this.nullable = false;
    }

    public int getNumericPrecision() {
        return numericPrecision;
    }

    public void setNumericPrecision(int numericPrecision) {
        this.numericPrecision = numericPrecision;
    }

    public int getNumericScale() {
        return numericScale;
    }

    public void setNumericScale(int numericScale) {
        this.numericScale = numericScale;
    }
}

 

<?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.*.*.*">

	<select id="getTableSchemaByCondition" parameterType="map" resultType="map">
		SELECT
		    col.table_schema ,
		    col.table_name ,
		    col.ordinal_position,
		    col.column_name ,
		    col.data_type ,
		    col.character_maximum_length,
		    col.numeric_precision,
		    col.numeric_scale,
		    col.is_nullable,
		    col.column_default ,
		    des.description
		FROM information_schema.columns col 
		LEFT JOIN pg_description des ON col.table_name::regclass = des.objoid AND col.ordinal_position = des.objsubid
		WHERE table_schema = #{tableSchema} AND table_name = #{tableName}
		ORDER BY ordinal_position
	</select>
</mapper>

 针对不同的数据库,语法不尽相同,请注意。

分享到:
评论

相关推荐

    数据结构顺序表和链表的相关操作

    3.根据指定条件能获取元素;4.实现在指定位置插入和删除元素的功能 主菜单设计要求: 程序运行后创建顺序表和链表,之后给出9个菜单项的内容和输入提示: 1.顺序表的插入 2.顺序表的删除 3.顺序表的长度 4.顺序表的...

    数据结构源码之二叉树的二叉链表

    数据结构源码C语言描述续,...//获取二叉树某一个指定结点父节点 int GetBTreeParent(BTNode *root, BTNode *btNode, BTNode *parentNode); //销毁二叉树 void DestroyBTree(BTNode *BT); 读者可以直接下载并运行应用

    【数据结构实现】C语言实现顺序表

    内容概要:线性表是一种相当灵活的数据结构,其长度可以根据需要增长或缩短,对线性表的数据元素不仅可以进行访问,还可以进行插入和删除等操作。本次试验用C语言实现顺序表,实现 Init 初始化线性表 Destory 销毁...

    用数组实现数据结构顺序表的几种功能

    用数组实现数据结构顺序表的几种功能。包括插入,判断顺序表是否为空,遍历顺序表,获取指定位置元素,查找元素,获取元素的前驱、后继,删除、清空、销毁顺序表。

    纯C语言实现顺序表附加源码

    顺序表是一种线性表的存储结构,可以使用数组来实现。...6. 获取指定位置的元素:根据位置获取顺序表中对应的元素。 这些是顺序表的基本操作,通过这些操作可以实现对顺序表的插入、删除、查找和获取等操作。

    CodeGenerator:C#代码生成器,用于从数据库读取表结构并生成相应的代码

    代码生成器 C#代码生成器,用于从数据库读取表结构并生成相应的代码

    用指针实现数据结构顺序表的几种功能

    用指针实现数据结构顺序表的几种功能。包括插入,判断顺序表是否为空,遍历顺序表,获取指定位置元素,查找元素,获取元素的前驱、后继,删除、清空、销毁顺序表。

    数据结构源码之二叉树的三叉链表

    数据结构源码C语言描述续,本篇描述了二叉树三叉链表结构及其操作,以及测试程序: //创建二叉树结点 TriTreeNode *CreateTriTreeNode(char data); //给二叉树添加结点,用于创建二叉树 int AddTriTreeNode(char ...

    MySQL多层级结构-树搜索介绍

    发现一个表结构硬是不明白是怎么回事。具体表结构如下: CREATE TABLE acos ( id INTEGER(10) UNSIGNED NOT NULL AUTO_INCREMENT, parent_id INTEGER(10) DEFAULT NULL, model VARCHAR(255) DEFAULT '', ...

    java自动生成节假日对照表

    中国的节假日不是按照阳历就能够计算,在没有调接口的情况下,最好的办法就是在数据库里面维护相对应的对照表.这里就提供了一个工具类自动生成指定年份的节假日对照表

    Oralce 自定义数据库同步脚本

    此脚本利用JOB定时把指定表中的数据生成SQL(利用模版去生成),模版不需要自己去写,只要做好相关配置就行,至于怎么配置,表结构中都有描述,更新方式有:1.先删除满足条件的数据再插入;2.整表删除再插入)。生成...

    静态数据结构与动态数据结构.pdf

    数据结构通常基于计算机在内存中的任何位置获取和存储数据的能⼒,由指针指定的⼀个位 串,表⽰⼀个内存地址,它可以存储在内存中,并由程序操作。因此,数组和记录数据结构基 于计算数据项的地址和算术运算;⽽链接...

    API之网络函数---整理网络函数及功能

    GetPrivateProfileSection 获取指定小节(在.ini文件中)所有项名和值的一个列表 GetPrivateProfileString 为初始化文件中指定的条目取得字串 GetProfileInt 取得win.ini初始化文件中指定条目的一个整数值 ...

    Excel VBA实用技巧大全 附书源码

    04085获取指定列号单元格的列标字母(之一) 04086获取指定列号单元格的列标字母(之二) 04087获取单元格区域的行号范围 04088获取单元格区域的列号范围 04089获取单元格区域的列标字母范围 04090获取数据区域的...

    数据库与数据表设计.pptx

    说明 在创建数据表前,首先要根据项目实际要求制定相关的表结构,然后在数据库中创建相应的数据表。 数据库与数据表设计全文共28页,当前为第12页。 tb_UserPope(用户权限表):用于保存每个操作员使用程序的相关...

    从SQL_server导出数据到EXCEL

    实例使用_RecordsetPtr指针类的GetCollect方法获取指定字段的数据,在使用_RecordsetPtr指针类对象时需要先通过CreateInstance方法初始化,初始化以后调用Open方法打开记录集,然后就可以调用GetCollect方法获取数据...

    VC 读取SQL Server数据库结构.rar

    VC 读取SQL Server数据库结构,读取指定数据库指定数据表的数据结构,演示中仅是读取出字段名、字段类型和字段类型取值大小,只是演示一种获取字段数据的方法:  m_grid.SetExtendedStyle(LVS_EX_FLATSB   |LVS_EX...

    Excel VBA与数据库整合应用范例精讲

    实例2-5 获取数据库中所有表的名称和类型(ADO) 实例2-6 获取数据库中所有表的名称和类型(ADOX) 实例2-7 获取数据库中所有的表名称(DAO) 实例2-8 获取数据库中所有数据表名称(ADO) 实例2-9 获取数据库中...

    Excel VBA与数据库整合应用范例精讲书及源代码

    实例2-5 获取数据库中所有表的名称和类型(ADO) 实例2-6 获取数据库中所有表的名称和类型(ADOX) 实例2-7 获取数据库中所有的表名称(DAO) 实例2-8 获取数据库中所有数据表名称(ADO) 实例2-9 获取数据库中...

    C/C++实现单向链表

    分别用C和C++实现了单向链表(创建链表,插入数据、获取指定位置的数据、删除指定位置的数据...),如果在使用中觉得api不够用可以进行扩展;其中包含测试。

Global site tag (gtag.js) - Google Analytics