Java研学-数据字典(一)

news2024/9/23 12:47:31

一 需求分析

1 分析

  在项目中会有很多的下拉框,这些下拉框的特点,就是以键值对的形式存在,其中 value(如 id:1,2… ),key(展示给用户的内容),数据库中可通过 value 寻找对应的 key,对于固定的内容可使用数据字典形式进行填写,防止出错。
  实现方案:
  1.直接硬编码写在html代码中(升级复杂,维护困难)
  2.为每个下拉框都设计一个表,然后提供CRUD功能(表单中下拉框很多,内容相似,没必要每个都配,只特殊情况下使用)
  3.使用数据字典的方式(维护项目中所有的下拉列表)

二 表设计

1 概述

  sys_dict_type 中主要存储项目中有哪些下拉框,sys_dict_data 中主要存储项目中下拉框对应的option的具体内容,两张表通过 dict_type 进行关联,查询时可通过下拉框名称“性别”于 sys_dict_type 表中搜索 dict_name 为“性别”的记录的 dict_type 字段,通过该字段到 sys_dict_data 表中获取需要展示的 key(dict_value) 与 value(dict_label),通过 dict_sort 可对下拉框中内容进行排序,如:
在这里插入图片描述

2 字典类型表 sys_dict_type

  所有程序中的select,该表中 dict_type 需唯一

字段描述
dict_id主键
dict_name字典名称(例:性别)
dict_type字典类型(例:sex)
remark备注(例:性别)

3 字典数据表 sys_dict_data

  所有程序中的select所对应的option,例:< option value=“1”>女< /option>,其中键值为1,标签为女

字段描述
dict_code主键
dict_type字典类型
dict_label字典标签(例:女)
dict_value字典键值(例:1)
dict_sort字典排序(对下拉列表数据进行排序)
remark备注

4 客户表 t_customer

  客户表中存储 source_value 与 school_value 通过关联上表,获取所需的数据,建议使用左连接防止由于字段NULL时查询丢失数据

字段描述
id主键
name客户名称
phone联系方式
source_value常住地
school_value最高学历

三 项目搭建

1 pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.tj</groupId>
    <artifactId>sys-dict-demo</artifactId>
    <version>1.0.0</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.13.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <druid.version>1.2.6</druid.version>
        <mybatis-spring-boot.version>2.1.4</mybatis-spring-boot.version>
        <fastjson.version>1.2.76</fastjson.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- SpringBoot集成thymeleaf模板 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!--阿里数据库连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>${druid.version}</version>
        </dependency>
        <!-- SpringBoot集成mybatis框架 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis-spring-boot.version}</version>
        </dependency>
        <!-- Mysql驱动包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- spring-boot-devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional> <!-- 表示依赖不会传递 -->
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!-- MyBatis 逆向工程插件 -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <verbose>true</verbose>
                    <overwrite>false</overwrite>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.45</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

2 主启动类 – SysDictApp

@SpringBootApplication
// mapper包不在同一目录下,填写路径(所有mapper接口)
@MapperScan("cn.tj.**.mapper")
public class SysDictApp {
    public static void main(String[] args) {
        SpringApplication.run(SysDictApp.class,args);
    }
}

3 yml配置文件 – application.yml

# Spring配置
spring:
  # 模板引擎
  thymeleaf:
    mode: HTML
    encoding: utf-8
    # 禁用缓存
    cache: false
  # 数据库配置信息
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/zd?serverTimezone=GMT%2B8
    username: root
    password: root
# MyBatis
mybatis:
  # 配置mapper的扫描,找到所有的mapper.xml映射文件
  mapperLocations: classpath*:mapper/*Mapper.xml
# 日志配置
logging:
  level:
    cn.tj: debug
    org.springframework: warn
server:
  port: 80

4 实体类 – SysDictType

@Getter
@Setter
public class SysDictType{
    private Long dictId;
    private String dictName;
    private String dictType;
    private String remark;
}

5 实体类 – SysDictData

@Getter
@Setter
public class SysDictData {
    private Long dict_code;
    private Integer dict_sort;
    private String dict_label;
    private String dict_value;
    private String dict_type;
    private String remark;
}

6 实体类 – Customer

@Setter
@Getter
public class Customer {
    private Long id;
    private String name;
    private String phone;
    // 关联字段
    private SysDictData sourceType;
    private SysDictData schoolType;
}

7 数据库表 – sys_dict_type

CREATE TABLE `sys_dict_type` (
  `dict_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '字典主键',
  `dict_name` varchar(100) DEFAULT '' COMMENT '字典名称',
  `dict_type` varchar(100) DEFAULT '' COMMENT '字典类型',
  `remark` varchar(500) DEFAULT NULL COMMENT '备注',
  PRIMARY KEY (`dict_id`),
  UNIQUE KEY `dict_type` (`dict_type`)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8 COMMENT='字典类型表';

8 数据库表 – sys_dict_data

CREATE TABLE `sys_dict_data` (
  `dict_code` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '字典编码',
  `dict_sort` int(4) DEFAULT '0' COMMENT '字典排序',
  `dict_label` varchar(100) DEFAULT '' COMMENT '字典标签',
  `dict_value` varchar(100) DEFAULT '' COMMENT '字典键值',
  `dict_type` varchar(100) DEFAULT '' COMMENT '字典类型',
  `remark` varchar(500) DEFAULT NULL COMMENT '备注',
  PRIMARY KEY (`dict_code`)
) ENGINE=InnoDB AUTO_INCREMENT=67 DEFAULT CHARSET=utf8 COMMENT='字典数据表';

9 数据库表 – t_customer

CREATE TABLE `t_customer` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `phone` varchar(255) DEFAULT NULL,
  `source_value` bigint(20) DEFAULT NULL,
  `school_value` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

10 mapper接口 – SysDictTypeMapper

public interface SysDictTypeMapper {
    List<SysDictType> selectAll();
    int getCountByDictType(String dictType);
    void insert(SysDictType sysDictType);
    SysDictType get(Long dictId);
    void update(SysDictType sysDictType);
    SysDictType getByDictType(String dictType);
}

11 mapper接口 – SysDictDataMapper

public interface SysDictDataMapper {
    int deleteByPrimaryKey(Long dict_code);
    int insert(SysDictData record);
    SysDictData selectByPrimaryKey(Long dict_code);
    List<SysDictData> selectAll();
    int updateByPrimaryKey(SysDictData record);
    List<SysDictData> queryByDictType(String dictType);
}

12 mapper接口 – CustomerMapper

public interface CustomerMapper {
    List<Customer> selectAll();
    void insert(Customer customer);
    void update(Customer customer);
    Customer get(Long id);
}

13 mapper.xml – SysDictTypeMapper

<?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="cn.tj.mapper.SysDictTypeMapper">
    <resultMap id="sysDictTypeResult" type="cn.tj.domain.SysDictType">
        <result column="dict_id" property="dictId"/>
        <result column="dict_name" property="dictName"/>
        <result column="dict_type" property="dictType"/>
        <result column="remark" property="remark"/>
    </resultMap>
    <insert id="insert" keyProperty="dictId" useGeneratedKeys="true">
        insert into sys_dict_type(dict_name, dict_type, remark) values (#{dictName},#{dictType},#{remark})
    </insert>
    <update id="update">
        update sys_dict_type set
            dict_name = #{dictName},
            remark = #{remark}
            where dict_id = #{dictId}
    </update>
    <select id="selectAll" resultMap="sysDictTypeResult">
        select dict_id, dict_name, dict_type, remark  from sys_dict_type
    </select>
    <select id="getCountByDictType" resultType="java.lang.Integer">
        select count(*) from sys_dict_type where dict_type=#{dictType}
    </select>
    <select id="get" resultMap="sysDictTypeResult">
        select dict_id, dict_name, dict_type, remark  from sys_dict_type
        where dict_id=#{dictId}
    </select>
    <select id="getByDictType" resultMap="sysDictTypeResult">
        select dict_id, dict_name, dict_type, remark  from sys_dict_type
        where dict_type=#{dictType}
    </select>
</mapper>

14 mapper.xml – SysDictDataMapper

<?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="cn.tj.mapper.SysDictDataMapper" >
  <resultMap id="BaseResultMap" type="cn.tj.domain.SysDictData" >
    <id column="dict_code" property="dict_code" />
    <result column="dict_sort" property="dict_sort" />
    <result column="dict_label" property="dict_label" />
    <result column="dict_value" property="dict_value" />
    <result column="dict_type" property="dict_type" />
    <result column="remark" property="remark" />
  </resultMap>
  <delete id="deleteByPrimaryKey" >
    delete from sys_dict_data
    where dict_code = #{dict_code}
  </delete>
  <insert id="insert" >
    insert into sys_dict_data (dict_code, dict_sort, dict_label, dict_value, dict_type, remark
      )
    values (#{dict_code}, #{dict_sort}, #{dict_label}, #{dict_value}, #{dict_type}, #{remark}
      )
  </insert>
  <update id="updateByPrimaryKey" >
    update sys_dict_data
    set dict_sort = #{dict_sort},
      dict_label = #{dict_label},
      dict_value = #{dict_value},
      remark = #{remark}
    where dict_code = #{dict_code}
  </update>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" >
    select dict_code, dict_sort, dict_label, dict_value, dict_type, remark
    from sys_dict_data
    where dict_code = #{dict_code}
  </select>
  <select id="selectAll" resultMap="BaseResultMap" >
    select dict_code, dict_sort, dict_label, dict_value, dict_type, remark
    from sys_dict_data
  </select>
  <select id="queryByDictType" resultMap="BaseResultMap">
    select dict_code, dict_sort, dict_label, dict_value, dict_type, remark
    from sys_dict_data
    where dict_type=#{dictType}
    order by dict_sort asc
  </select>
</mapper>

15 mapper.xml – CustomerMapper

<?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="cn.tj.mapper.CustomerMapper">
    <resultMap id="CustomerResult" type="cn.tj.domain.Customer">
        <result column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="phone" property="phone"/>
        <result column="s1_label" property="sourceType.dict_label"/>
        <result column="s2_label" property="schoolType.dict_label"/>
        <result column="s1_value" property="sourceType.dict_value"/>
        <result column="s2_value" property="schoolType.dict_value"/>
    </resultMap>
    <select id="selectAll" resultMap="CustomerResult">
        select c.id,c.name,c.phone,s1.dict_value s1_value,s1.dict_label s1_label,s2.dict_value s2_value,s2.dict_label s2_label from t_customer c
        left join sys_dict_data s1 on (c.source_value = s1.dict_value and s1.dict_type="sourceType")
        left join sys_dict_data s2 on (c.school_value = s2.dict_value and s2.dict_type="schoolType")
    </select>
    <insert id="insert">
        insert into t_customer(name,phone,source_value,school_value)
        values (#{name},#{phone},#{sourceType.dict_value},#{schoolType.dict_value})
    </insert>
    <update id="update">
        update t_customer set
        name = #{name},
        phone = #{phone},
        source_value = #{sourceType.dict_value},
        school_value = #{schoolType.dict_value}
        where id  = #{id}
    </update>
    <select id="get" resultMap="CustomerResult">
        select c.id,c.name,c.phone,s1.dict_value s1_value,s1.dict_label s1_label,s2.dict_value s2_value,s2.dict_label s2_label from t_customer c
        left join sys_dict_data s1 on (c.source_value = s1.dict_value and s1.dict_type="sourceType")
        left join sys_dict_data s2 on (c.school_value = s2.dict_value and s2.dict_type="schoolType")
        where id = #{id}
    </select>
</mapper>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2157566.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

SSC338D/SSC338Q CA7*2+IPU5M/Multi-sensorISP: HDR/3DNR

SSC338D/SSC338Q系列产品是高度集成的多媒体片上系统&#xff08;SoC&#xff09;产品&#xff0c;适用于IP摄像机、车载摄像机和USB摄像机等高分辨率智能视频录制应用。该芯片包括32位双核RISC处理器、高级图像信号处理器&#xff08;ISP&#xff09;、高性能MJPEG/H.264/H.26…

Maven-三、聚合

Maven 文章目录 Maven前言创建聚合模块设置管理的子模块总结 前言 在使用了maven进行多模块开发后&#xff0c;随着模块变多会变得难以管理&#xff0c;所以需要使用聚合模块进行统一管理。 分模块开发的项目中会有多个模块&#xff0c;那么可以单独使用一个模块专门管理整个工…

毫米波雷达预警功能 —— 倒车预警(RCTA)

文档声明&#xff1a; 以下资料均属于本人在学习过程中产出的学习笔记&#xff0c;如果错误或者遗漏之处&#xff0c;请多多指正。并且该文档在后期会随着学习的深入不断补充完善。感谢各位的参考查看。 笔记资料仅供学习交流使用&#xff0c;转载请标明出处&#xff0c;谢谢配…

【Web】御网杯信息安全大赛2024 wp(全)

目录 input_data admin flask 如此多的FLAG 一夜醒来之全国CTF水平提升1000倍&#x1f60b; input_data 访问./.svn后随便翻一翻拿到flag admin dirsearch扫出来 访问./error看出来是java框架 测出来是/admin;/路由打Spring View Manipulation(Java)的SSTI https:/…

HTML中直接创建一个“onoff”图形开关包括css+script

1. HTML中直接创建一个“onoff”图形开关 下面是一个完整的HTML文档示例 在HTML中直接创建一个“onoff”图形开关&#xff08;通常指的是一个类似于物理开关的UI组件&#xff0c;可以切换开或关的状态&#xff09;&#xff0c;并不直接支持&#xff0c;因为HTML主要用于内容的…

[数据集][目标检测]中草药类型识别检测数据集VOC+YOLO格式7976张45类别

数据集格式&#xff1a;Pascal VOC格式YOLO格式(不包含分割路径的txt文件&#xff0c;仅仅包含jpg图片以及对应的VOC格式xml文件和yolo格式txt文件) 图片数量(jpg文件个数)&#xff1a;7976 标注数量(xml文件个数)&#xff1a;7976 标注数量(txt文件个数)&#xff1a;7976 标注…

STM32篇:按键点亮LED灯

输入&#xff08;按键&#xff09;&#xff1a;KEY1---PA0 KEY2---PA1 输出&#xff08;LED灯&#xff09;&#xff1a;LED1---PB8 LED2---PB9

数字孪生技术如何推动企业可持续发展:监控与优化企业可持续目标的新视角

数字孪生助力可持续发展的新机遇 在全球推进可持续发展战略的背景下&#xff0c;企业需要创新型的技术工具来实现高效管理&#xff0c;数字孪生技术成为了实现这一目标的重要工具。数字孪生通过虚拟与现实的互动&#xff0c;将物理世界中的企业活动、运营数据及生产流程进行精…

FreeSWITCH 简单图形化界面29 - 使用mod_xml_curl 动态获取配置、用户、网关数据

FreeSWITCH 简单图形化界面29 - 使用mod_xml_curl 动态获取配置、用户、网关数据 FreeSWITCH GUI界面预览安装FreeSWITCH GUI先看使用手册1、简介2、安装mod_xml_curl模块3、配置mod_xml_curl模块3、编写API接口4、测试一下5、其他注意的地方 FreeSWITCH GUI界面预览 http://m…

LDO选型

LDO原理 mos管工作在可变电阻区&#xff0c;输出端电压会因为输出负载的变化而变化&#xff0c;则可通过误差放大器来控制Rds从而维持输出电压不变&#xff0c;行成一个动态平衡。 低压差 线性调整率 负载调整率 电源&#xff08;纹波&#xff09;抑制比 瞬态响应 外部元器件作…

神经网络(二):卷积神经网络

文章目录 一、图像的本质1.1单通道图像&#xff1a;灰度图1.2多通道图像 二、卷积神经网络2.1基本结构2.2卷积层2.2.1卷积操作2.2.2填充padding2.2.3步幅strides2.2.4多通道图像卷积&#xff1a;单卷积核2.2.5多通道图像卷积&#xff1a;多卷积核2.2.5卷积层的参数与激活函数 2…

算法练习题24——leetcode3296移山所需的最小秒数(二分模拟)

【题目描述】 【代码示例&#xff08;java&#xff09;】 class Solution {// 计算让工人们将山的高度降到0所需的最少时间public long minNumberOfSeconds(int mountainHeight, int[] workerTimes) {long left 0; // 最少时间初始为0long right 0; // 最大时间初始化为0// …

Linux,uboot,kernel启动流程,S5PV210芯片的启动流程,DRAM控制器初始化流程

一、S5PV210芯片的DRAM控制器介绍、初始化DDR的流程分析 1、DRAM的地址空间 1)从地址映射图可以知道&#xff0c;S5PV210有两个DRAM端口。 DRAM0的内存地址范围&#xff1a;0x20000000&#xff5e;0x3FFFFFFF&#xff08;512MB&#xff09;&#xff1b;DRAM1:的内存地址范围…

AI大模型教程 Prompt提示词工程 AI原生应用开发零基础入门到实战【2024超细超全,建议收藏】

在AGI&#xff08;通用人工智能&#xff09;时代&#xff0c;那些既精通AI技术、又具备编程能力和业务洞察力的复合型人才将成为最宝贵的资源。为此&#xff0c;我们提出了‘AI全栈工程师’这一概念&#xff0c;旨在更精准地描述这一复合型人才群体&#xff0c;而非过分夸大其词…

全栈项目小组【算法赛】题目及解题

题目&#xff1a;全栈项目小组【算法赛】 题目&#xff1a; 解题思路 1.遍历简历信息&#xff1a;我们需要读取所有简历&#xff0c;根据期望薪资和岗位类型进行分类和统计。 2.分类统计&#xff1a;使用哈希表来存储每个薪资下的前端&#xff08;F&#xff09;和后端&#…

传统产品经理如何快速转行成为顶尖的AI产品经理?

前言 产品经理本身便是一个需要不断学习、不断实践的岗位&#xff0c;即使是AI产品经理&#xff0c;也不能脱离产品经理岗位的本质。 另外&#xff0c;要想知道具体如何转行成为顶尖的AI产品经理&#xff0c;我们首先要明确两个问题&#xff0c;即&#xff1a; 什么是AI产品…

HTML5简介的水果蔬菜在线商城网站源码系列模板3

文章目录 1.设计来源1.1 主界面1.2 商品列表1.3 商品信息1.4 购物车1.5 其他页面效果 2.效果和源码2.1 动态效果2.2 源代码 源码下载万套模板&#xff0c;程序开发&#xff0c;在线开发&#xff0c;在线沟通 作者&#xff1a;xcLeigh 文章地址&#xff1a;https://blog.csdn.ne…

AI Native平台,跨越AI应用从创新到生产的鸿沟

2024年是AI应用的元年&#xff0c;以大模型为中心的 AI Native 应用大爆发正在从理想变成现实。云计算带来的应用创新潮&#xff0c;经历了虚拟机时代和云原生时代&#xff0c;正在全面拥抱以大模型为核心的 AI Native 阶段&#xff0c;推动大数据与AI的工作流前所未有地紧密结…

【C语言进阶】第四节:自定义类型详解

1、结构体 1.1 结构体变量的定义和初始化 struct Point//类型声明 {int x;int y; }p1;//声明类型的同时定义变量p1struct Point p2;//定义结构体变量p2//初始化&#xff1a;定义变量的同时赋初值。 struct Point p3 { x, y };struct Node {int data;struct Point p;struct N…

智谱清影的魅力:使用CogVideoX-2b生成6秒视频的真实体验!

文章目录 1 3D变分自编码器与3D RoPE2 精确描述与多样化输入3 社区的力量与未来展望 在8月6日&#xff0c;智谱 AI 发布了一则令人振奋的消息&#xff1a;他们决定开源其视频生成模型CogVideoX。 1 3D变分自编码器与3D RoPE 作为一名开发者&#xff0c;我近期才来体验这个新工…