获取数据库中字段的数据作为下拉框选项

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
简介: 获取数据库中字段的数据作为下拉框选项

 如题:当我们想把数据库中字段的数据作为对应的下拉框选项,并且数据库字段很多的时候。应该在后端获取这个表里有多少字段,遍历这个表,拿到所有的字段并放到一个map集合里,我们看看代码。

image.gif 编辑

image.gif 编辑

这个表里有大概四五十个字段,我们不能每一个字段都写一个对应获取的方法,所以我们在controller层直接使用这个方法。

@RequestMapping("/getDropdownItems")
    public ResultData getDropdownItems() {
        Map<String, List<String>> dropdownItems = new HashMap<>();
        String dbUrl = "jdbc:mysql://192.168.1.60:3306/qcfx_db?characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true";
        String user = "root";
        String password = "ME#EDC2WSx!qaZ";
        try (Connection conn = DriverManager.getConnection(dbUrl, user, password);
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery("SELECT * FROM report_changjingku_list WHERE 1=2")) { // 只查询字段,不查询数据
            ResultSetMetaData metaData = rs.getMetaData();
            int columnCount = metaData.getColumnCount();
            for (int i = 1; i <= columnCount; i++) {
                String columnName = metaData.getColumnName(i);
                String query = "SELECT DISTINCT " + columnName + " FROM report_changjingku_list";
                try (ResultSet columnResultSet = stmt.executeQuery(query)) {
                    List<String> items = new ArrayList<>();
                    while (columnResultSet.next()) {
                        items.add(columnResultSet.getString(1));
                    }
                    dropdownItems.put(columnName, items);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ResultData.success("ok",dropdownItems);
    }

image.gif

连接到数据库后,遍历字段个数并且插入到map集合里。

前端:

页面展示:

image.gif 编辑

代码:

js:

export function getDropdownItems() {
    return request({
      url: '/mixTableAdd/getDropdownItems',
      method: 'post'
    })
  }

image.gif

选择框:

<template>
  <el-form
    ref="formRef"
    :model="reqUser"
    label-width="180px"
    v-loading="formLoading"
  >
  <el-row :gutter="24">
    <el-col :span="12">
    <el-form-item label="竖维度" prop="verticalDimension">
      <el-select v-model="reqUser.verticalDimension" class="filter-item" placeholder="请选择竖维度" clearable style="width: 230%;">
        <el-option
          v-for="item in statusList"
          :key="item.name"
          :label="item.comment"
          :value="item.name"
        />
      </el-select>
    </el-form-item>
  </el-col>
  <el-col :span="12">
    <el-form-item label="横维度" prop="horizontalDimension">
      <el-select v-model="reqUser.horizontalDimension" class="filter-item" placeholder="请选择横维度" clearable style="width: 230%;">
        <el-option
          v-for="item in statusList"
          :key="item.name"
          :label="item.comment"
          :value="item.name"
        />
      </el-select>
    </el-form-item>
  </el-col>
</el-row>
<el-row :gutter="24">
  <el-col :span="12">
    <el-form-item label="值" prop="price">
      <el-select v-model="reqUser.price" class="filter-item" placeholder="请选择值" clearable style="width: 230%;">
        <el-option
          v-for="item in statusList"
          :key="item.name"
          :label="item.comment"
          :value="item.name"
        />
      </el-select>
    </el-form-item>
  </el-col>
  <el-col :span="12">
    <el-form-item label="年龄" prop="age">
      <el-select v-model="reqUser.age" class="filter-item" placeholder="请选择年龄" clearable style="width: 230%;">
        <el-option
          v-for="item in selectSource.r_nl"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </el-form-item>
  </el-col>
</el-row>
<el-row :gutter="24">
  <el-col :span="12">
    <el-form-item label="行业" prop="industry">
      <el-select v-model="reqUser.industry" class="filter-item" placeholder="请选择行业" clearable style="width: 230%;">
        <el-option
          v-for="item in selectSource.r_hy"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </el-form-item>
  </el-col>
  <el-col :span="12">
    <el-form-item label="性别" prop="sex">
      <el-select v-model="reqUser.sex" class="filter-item" placeholder="请选择性别" clearable style="width: 230%;">
        <el-option
          v-for="item in selectSource.r_xb"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </el-form-item>
  </el-col>
</el-row>
<el-row :gutter="24">
  <el-col :span="12">
    <el-form-item label="婚姻状况" prop="maritalStatus">
      <el-select v-model="reqUser.maritalStatus" class="filter-item" placeholder="请选择婚姻状况" clearable style="width: 230%;">
        <el-option
          v-for="item in selectSource.r_hyzk"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </el-form-item>
  </el-col>
  <el-col :span="12">
    <el-form-item label="小孩数量" prop="childNum">
      <el-select v-model="reqUser.childNum" class="filter-item" placeholder="请选择小孩数量" clearable style="width: 230%;">
        <el-option
          v-for="item in selectSource.r_xhsl"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </el-form-item>
  </el-col>
</el-row>
<el-row :gutter="24">
  <el-col :span="12">
    <el-form-item label="小孩年龄段" prop="childAgeScope">
      <el-select v-model="reqUser.childAgeScope" class="filter-item" placeholder="请选择小孩年龄段" clearable style="width: 230%;">
        <el-option
          v-for="item in selectSource.r_xhnld"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </el-form-item>
  </el-col>
  <el-col :span="12">
    <el-form-item label="职业属性" prop="occupationAttribute">
      <el-select v-model="reqUser.occupationAttribute" class="filter-item" placeholder="请选择职业属性" clearable style="width: 230%;">
        <el-option
          v-for="item in selectSource.r_zysx"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </el-form-item>
  </el-col>
</el-row>
<el-row :gutter="24">
  <el-col :span="12">
    <el-form-item label="家庭年收入情况(万)" prop="homeIncome">
      <el-select v-model="reqUser.homeIncome" class="filter-item" placeholder="请选择家庭年收入情况(万)" clearable style="width: 230%;">
        <el-option
          v-for="item in selectSource.r_jtnsrqk"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </el-form-item>
  </el-col>
  <el-col :span="12">
    <el-form-item label="学历情况" prop="educationalBack">
      <el-select v-model="reqUser.educationalBack" class="filter-item" placeholder="请选择学历情况" clearable style="width: 230%;">
        <el-option
          v-for="item in selectSource.r_xlqk"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </el-form-item>
  </el-col>
</el-row>
<el-row :gutter="24">
  <el-col :span="12">
    <el-form-item label="车系" prop="carSeries">
      <el-select v-model="reqUser.carSeries" class="filter-item" placeholder="请选择车系" clearable style="width: 230%;">
        <el-option
          v-for="item in selectSource.r_cx"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </el-form-item>
  </el-col>
  <el-col :span="12">
    <el-form-item label="级别" prop="carLevel">
      <el-select v-model="reqUser.carLevel" class="filter-item" placeholder="请选择级别" clearable style="width: 230%;">
        <el-option
          v-for="item in selectSource.r_jb"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </el-form-item>
  </el-col>
</el-row>
<el-row :gutter="24">
  <el-col :span="12">
    <el-form-item label="车身类别" prop="carBody">
      <el-select v-model="reqUser.carBody" class="filter-item" placeholder="请选择车身类别" clearable style="width: 230%;">
        <el-option
          v-for="item in selectSource.r_cslb"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </el-form-item>
  </el-col>
  <el-col :span="12">
    <el-form-item label="TP价格段(万)" prop="tpPrice">
      <el-select v-model="reqUser.tpPrice" class="filter-item" placeholder="请选择TP价格段(万)" clearable style="width: 230%;">
        <el-option
          v-for="item in selectSource.r_tpjgd"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </el-form-item>
  </el-col>
</el-row>
<el-row :gutter="24">
  <el-col :span="12">
    <el-form-item label="能源类型" prop="energyType">
      <el-select v-model="reqUser.energyType" class="filter-item" placeholder="请选择能源类型" clearable style="width: 230%;">
        <el-option
          v-for="item in selectSource.r_nylx"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </el-form-item>
  </el-col>
  <el-col :span="12">
    <el-form-item label="一级场景" prop="oneScene">
      <el-select v-model="reqUser.oneScene" class="filter-item" placeholder="请选择一级场景" clearable style="width: 230%;">
        <el-option
          v-for="item in selectSource.r_yjcj"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </el-form-item>
  </el-col>
</el-row>
<el-row :gutter="24">
  <el-col :span="12">
    <el-form-item label="二级场景描述" prop="twoSceneDescription">
      <el-select v-model="reqUser.twoSceneDescription" class="filter-item" placeholder="请选择二级场景描述" clearable style="width: 230%;">
        <el-option
          v-for="item in selectSource.r_ejcjms"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </el-form-item>
  </el-col>
  <el-col :span="12">
    <el-form-item label="出行阶段" prop="travelStage">
      <el-select v-model="reqUser.travelStage" class="filter-item" placeholder="请选择出行阶段" clearable style="width: 230%;">
        <el-option
          v-for="item in selectSource.r_cxjd"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </el-form-item>
  </el-col>
</el-row>
<el-row :gutter="24">
  <el-col :span="12">
    <el-form-item label="出行人数" prop="travelNum">
      <el-select v-model="reqUser.travelNum" class="filter-item" placeholder="请选择出行人数" clearable style="width: 230%;">
        <el-option
          v-for="item in selectSource.r_cxrs"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </el-form-item>
  </el-col>
  <el-col :span="12">
    <el-form-item label="用户角色" prop="userRole">
      <el-select v-model="reqUser.userRole" class="filter-item" placeholder="请选择用户角色" clearable style="width: 230%;">
        <el-option
          v-for="item in selectSource.r_yhjs"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </el-form-item>
  </el-col>
</el-row>
<el-row :gutter="24">
  <el-col :span="12">
    <el-form-item label="用户特征" prop="userFeature">
      <el-select v-model="reqUser.userFeature" class="filter-item" placeholder="请选择用户特征" clearable style="width: 230%;">
        <el-option
          v-for="item in selectSource.r_yhtz"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </el-form-item>
  </el-col>
  <el-col :span="12">
    <el-form-item label="驾驶状态" prop="driveStatus">
      <el-select v-model="reqUser.driveStatus" class="filter-item" placeholder="请选择驾驶状态" clearable style="width: 230%;">
        <el-option
          v-for="item in selectSource.r_jszt"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </el-form-item>
  </el-col>
</el-row>
<el-row :gutter="24">
  <el-col :span="12">
    <el-form-item label="驾驶道路" prop="driveRoad">
      <el-select v-model="reqUser.driveRoad" class="filter-item" placeholder="请选择驾驶道路" clearable style="width: 230%;">
        <el-option
          v-for="item in selectSource.r_jsdl"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </el-form-item>
  </el-col>
  <el-col :span="12">
    <el-form-item label="驾驶区域" prop="driveArea">
      <el-select v-model="reqUser.driveArea" class="filter-item" placeholder="请选择驾驶区域" clearable style="width: 230%;">
        <el-option
          v-for="item in selectSource.r_jsqy"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </el-form-item>
  </el-col>
</el-row>
<el-row :gutter="24">
  <el-col :span="12">
  </el-col>
  <el-col :span="12">
    <el-form-item label="天气环境" prop="weather">
      <el-select v-model="reqUser.weather" class="filter-item" placeholder="请选择天气环境" clearable style="width: 230%;">
        <el-option
          v-for="item in selectSource.r_tqhj"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </el-form-item>
  </el-col>
  <el-col :span="12">
    <el-form-item label="时间" prop="time">
      <el-date-picker
      v-model.trim="reqUser.time"
       type="datetime"
      style="width: 230%;"
      placeholder="选择时间"/>
    </el-form-item>
  </el-col>
</el-row>
<el-row :gutter="24">
  <el-col :span="12">
    <el-form-item label="季节" prop="season">
      <el-select v-model="reqUser.season" class="filter-item" placeholder="请选择季节" clearable style="width: 230%;">
        <el-option
          v-for="item in selectSource.r_jj"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </el-form-item>
  </el-col>
  <el-col :span="12">
    <el-form-item label="数据采集地点" prop="dataLocality">
      <el-select v-model="reqUser.dataLocality" class="filter-item" placeholder="请选择数据采集地点" clearable style="width: 230%;">
        <el-option
          v-for="item in selectSource.r_sjcjdd"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </el-form-item>
  </el-col>
</el-row>
<el-row :gutter="24">
  <el-col :span="12">
    <el-form-item label="城市等级" prop="cityLevel">
      <el-select v-model="reqUser.cityLevel" class="filter-item" placeholder="请选择城市等级" clearable style="width: 230%;">
        <el-option
          v-for="item in selectSource.r_csdj"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </el-form-item>
  </el-col>
  <el-col :span="12">
    <el-form-item label="隶属省份" prop="province">
      <el-select v-model="reqUser.province" class="filter-item" placeholder="请选择隶属省份" clearable style="width: 230%;">
        <el-option
          v-for="item in selectSource.r_lssf"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </el-form-item>
  </el-col>
</el-row>
<el-row :gutter="24">
  <el-col :span="12">
    <el-form-item label="一级指标" prop="oneIndex">
      <el-select v-model="reqUser.oneIndex" class="filter-item" placeholder="请选择一级指标" clearable style="width: 230%;">
        <el-option
          v-for="item in selectSource.r_yjzb"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </el-form-item>
  </el-col>
  <el-col :span="12">
    <el-form-item label="二级指标" prop="twoIndex">
      <el-select v-model="reqUser.twoIndex" class="filter-item" placeholder="请选择二级指标" clearable style="width: 230%;">
        <el-option
          v-for="item in selectSource.r_ejzb"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </el-form-item>
  </el-col>
</el-row>
<el-row :gutter="24">
  <el-col :span="12">
    <el-form-item label="三级指标" prop="threeIndex">
      <el-select v-model="reqUser.threeIndex" class="filter-item" placeholder="请选择三级指标" clearable style="width: 230%;">
        <el-option
          v-for="item in selectSource.r_sjzb"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </el-form-item>
  </el-col>
  <el-col :span="12">
    <el-form-item label="感知价值" prop="feelPrice">
      <el-select v-model="reqUser.feelPrice" class="filter-item" placeholder="请选择感知价值" clearable style="width: 230%;">
        <el-option
          v-for="item in selectSource.r_gzjz"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </el-form-item>
  </el-col>
</el-row>
<el-row :gutter="24">
  <el-col :span="12">
    <el-form-item label="感知时刻" prop="feelTime">
      <el-date-picker
      v-model.trim="reqUser.feelTime"
       type="datetime"
      style="width: 230%;"
      placeholder="选择感知时刻"/>
    </el-form-item>
  </el-col>
  <el-col :span="12">
    <el-form-item label="当前解决方案" prop="workOutScheme">
      <el-select v-model="reqUser.workOutScheme" class="filter-item" placeholder="请选择当前解决方案" clearable style="width: 230%;">
        <el-option
          v-for="item in selectSource.r_dqjjfa"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </el-form-item>
  </el-col>
</el-row>
<el-row :gutter="24">
  <el-col :span="12">
    <el-form-item label="数据来源" prop="dataResource">
      <el-select v-model="reqUser.dataResource" class="filter-item" placeholder="请选择数据来源" clearable style="width: 230%;">
        <el-option
          v-for="item in selectSource.r_sjly"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </el-form-item>
  </el-col>
</el-row>
<el-button type="primary" @click="save" style="margin-left: 47%;margin-top: 3%">确定</el-button>
  </el-form>
</template>

image.gif

获取方法:

getDropdownItems(){
      getDropdownItems().then(response => {
        console.log(response.r_nl,'asdsad')
        this.selectSource = response.data
      })
    }

image.gif

同时要定义一个selectSource数据来接受返回的字段。

返回的字段在postman中的样子:

image.gif 编辑

非常好用


目录
相关文章
|
6天前
|
调度 云计算 芯片
云超算技术跃进,阿里云牵头制定我国首个云超算国家标准
近日,由阿里云联合中国电子技术标准化研究院主导制定的首个云超算国家标准已完成报批,不久后将正式批准发布。标准规定了云超算服务涉及的云计算基础资源、资源管理、运行和调度等方面的技术要求,为云超算服务产品的设计、实现、应用和选型提供指导,为云超算在HPC应用和用户的大范围采用奠定了基础。
179577 18
|
13天前
|
存储 运维 安全
云上金融量化策略回测方案与最佳实践
2024年11月29日,阿里云在上海举办金融量化策略回测Workshop,汇聚多位行业专家,围绕量化投资的最佳实践、数据隐私安全、量化策略回测方案等议题进行深入探讨。活动特别设计了动手实践环节,帮助参会者亲身体验阿里云产品功能,涵盖EHPC量化回测和Argo Workflows量化回测两大主题,旨在提升量化投研效率与安全性。
云上金融量化策略回测方案与最佳实践
|
15天前
|
人工智能 自然语言处理 前端开发
从0开始打造一款APP:前端+搭建本机服务,定制暖冬卫衣先到先得
通义灵码携手科技博主@玺哥超carry 打造全网第一个完整的、面向普通人的自然语言编程教程。完全使用 AI,再配合简单易懂的方法,只要你会打字,就能真正做出一个完整的应用。
9291 23
|
19天前
|
Cloud Native Apache 流计算
资料合集|Flink Forward Asia 2024 上海站
Apache Flink 年度技术盛会聚焦“回顾过去,展望未来”,涵盖流式湖仓、流批一体、Data+AI 等八大核心议题,近百家厂商参与,深入探讨前沿技术发展。小松鼠为大家整理了 FFA 2024 演讲 PPT ,可在线阅读和下载。
4958 12
资料合集|Flink Forward Asia 2024 上海站
|
19天前
|
自然语言处理 数据可视化 API
Qwen系列模型+GraphRAG/LightRAG/Kotaemon从0开始构建中医方剂大模型知识图谱问答
本文详细记录了作者在短时间内尝试构建中医药知识图谱的过程,涵盖了GraphRAG、LightRAG和Kotaemon三种图RAG架构的对比与应用。通过实际操作,作者不仅展示了如何利用这些工具构建知识图谱,还指出了每种工具的优势和局限性。尽管初步构建的知识图谱在数据处理、实体识别和关系抽取等方面存在不足,但为后续的优化和改进提供了宝贵的经验和方向。此外,文章强调了知识图谱构建不仅仅是技术问题,还需要深入整合领域知识和满足用户需求,体现了跨学科合作的重要性。
|
27天前
|
人工智能 自动驾驶 大数据
预告 | 阿里云邀您参加2024中国生成式AI大会上海站,马上报名
大会以“智能跃进 创造无限”为主题,设置主会场峰会、分会场研讨会及展览区,聚焦大模型、AI Infra等热点议题。阿里云智算集群产品解决方案负责人丛培岩将出席并发表《高性能智算集群设计思考与实践》主题演讲。观众报名现已开放。
|
15天前
|
人工智能 容器
三句话开发一个刮刮乐小游戏!暖ta一整个冬天!
本文介绍了如何利用千问开发一款情侣刮刮乐小游戏,通过三步简单指令实现从单个功能到整体框架,再到多端优化的过程,旨在为生活增添乐趣,促进情感交流。在线体验地址已提供,鼓励读者动手尝试,探索编程与AI结合的无限可能。
三句话开发一个刮刮乐小游戏!暖ta一整个冬天!
|
14天前
|
消息中间件 人工智能 运维
12月更文特别场——寻找用云高手,分享云&AI实践
我们寻找你,用云高手,欢迎分享你的真知灼见!
1125 71