什么是数据字典? 管理系统常用的分类数据或一些固定数据,如省市区三级联动数据、民族数据、行业数据、学历数据等,需要做一个数据管理方便管理系统数据
一、数据库开发
1.创建数据库
微服务架构设计模式:分库分表
create database if not exists yygh_cmn character set utf8mb4;
use yygh_cmn;
CREATE TABLE `dict` (
`id` BIGINT(20) NOT NULL DEFAULT '0' COMMENT '编号',
`parent_id` BIGINT(20) NOT NULL DEFAULT '0' COMMENT '上级id',
`name` VARCHAR(100) NOT NULL DEFAULT '' COMMENT '名称',
`value` BIGINT(20) DEFAULT NULL COMMENT '值',
`dict_code` VARCHAR(20) DEFAULT NULL COMMENT '编码',
`create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`is_deleted` TINYINT(3) NOT NULL DEFAULT '0' COMMENT '逻辑删除(1:已删除,0:未删除)',
PRIMARY KEY (`id`),
KEY `idx_dict_code` (`dict_code`),
KEY `idx_parent_id` (`parent_id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8 COMMENT='组织架构表';
2.添加数据
insert into dict(id,parent_id,name,value,dict_code) values(1,0,'全部分类',1,'ROOT')
insert into dict(id,parent_id,name,value,dict_code) values(86,1,'省',86,'Province')
insert into dict(id,parent_id,name,value) values(110000,86,'北京市',110000),
(130000,86,'河北省',130000),
(140000,86,'山西省',140000),
(150000,86,'内蒙古自治区',150000),
(220000,86,'吉林省',220000),
(230000,86,'黑龙江省',230000),
(310000,86,'上海市',310000),
(320000,86,'江苏省',320000),
(340000,86,'安徽省',340000),
(360000,86,'江西省',360000),
(370000,86,'山东省',370000),
(420000,86,'湖北省',420000),
(610000,86,'陕西省',610000)
insert into dict(id,parent_id,name,value,dict_code) values(10000,1,'医院等级',null,'Hosptype')
insert into dict(id,parent_id,name,value) values(10001,10000,'三级甲等',1),
(10002,10000,'三级乙等',2),
(10003,10000,'二级甲等',3),
(10004,10000,'二级乙等',4),
(10005,10000,'一级',5)
insert into dict(id,parent_id,name,value,dict_code) values(20000,1,'证件类型',20000,'CretificatesType')
insert into dict(id,parent_id,name,value) values(20001,20000,'身份证',10),
(20002,20000,'户口本',20)
insert into dict(id,parent_id,name,value,dict_code) values(30000,1,'学历',30000,'Education')
insert into dict(id,parent_id,name,value) values(30001,30000,'高中',30001),
(30002,30000,'大专',30002),
(30003,30000,'本科',30003),
(30004,30000,'研究生',30004),
(30005,30000,'博士',30005),
(30006,30000,'其他',30006)
insert into dict(id,parent_id,name,value,dict_code) values(99100,1,'民族',99100,'Nation')
insert into dict(id,parent_id,name,value) values(99101,99100,'汉族',99101),
(99102,99100,'满族',99102),
(99103,99100,'彝族',99103),
(99104,99100,'维吾尔族',99104)
二、数据字典后端开发
1.添加数据字典实体类
package com.zhengyibao.yygh.model.cmn;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.zhengyibao.yygh.model.base.BaseEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
@ApiModel(description = "数据字典")
@TableName("dict")
public class Dict extends BaseEntity {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value="上级id")
@TableField("parent_id")
private Long parentId;
@ApiModelProperty(value="名称")
@TableField("name")
private String name;
@ApiModelProperty(value="值")
@TableField("value")
private Long value;
@ApiModelProperty(value="编码")
@TableField("dict_code")
private String dictCode;
@ApiModelProperty(value="是否包含子节点")
@TableField(exist=false)
private boolean hasChildren;
}
2.创建微服务子模块service_cmn
在yygh_parent/service下创建 service_cmn 模块用来开发数据字典的后端 new - - module - - Maven
3.为微服务模块添加配置信息application.properties
server.port=8202
spring.application.name=service-cmn
spring.profiles.active=dev
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql:
spring.datasource.username=root
spring.datasource.password=root123
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
4.添加启动类ServiceCmnApplication
package com.zhengyibao.yygh.cmn;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = "com.zhengyibao")
public class ServiceCmnApplication {
public static void main(String[] args){
SpringApplication.run(ServiceCmnApplication.class, args);
}
}
5.三层架构 - - mapper持久层
package com.zhengyibao.yygh.cmn.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zhengyibao.yygh.model.cmn.Dict;
public interface DictMapper extends BaseMapper<Dict>{
}
6.三层架构 - - mapper持久层的xml文件
<?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.zhengyibao.yygh.cmn.mapper.DictMapper">
</mapper>
7.三层架构 - - mapper持久层的扫描实现类的配置类
package com.zhengyibao.yygh.cmn.config;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan("com.zhengyibao.yygh.cmn.mapper")
public class CmnConfig {
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
}
8.三层架构 - - service业务层接口
package com.zhengyibao.yygh.cmn.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.zhengyibao.yygh.model.cmn.Dict;
import java.util.List;
public interface DictService extends IService<Dict>{
List<Dict> findChildData(Long id);
}
9.三层架构 - - service业务层实现类
package com.zhengyibao.yygh.cmn.service.impl;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhengyibao.yygh.cmn.mapper.DictMapper;
import com.zhengyibao.yygh.cmn.service.DictService;
import com.zhengyibao.yygh.model.cmn.Dict;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper,Dict> implements DictService {
@Override
public List<Dict> findChildData(Long id) {
QueryWrapper<Dict> wrapper=new QueryWrapper<>();
wrapper.eq("parent_id",id);
List<Dict> dictList=baseMapper.selectList(wrapper);
for (Dict dict:dictList){
Long dictid=dict.getId();
boolean isChildren=this.isChildren(id);
dict.setHasChildren(isChildren);
}
return dictList;
}
private boolean isChildren(Long id){
QueryWrapper<Dict> wrapper=new QueryWrapper<>();
wrapper.eq("parent_id",id);
Integer count=baseMapper.selectCount(wrapper);
return count>0;
}
}
10.三层架构 - - controller表示层
package com.zhengyibao.yygh.cmn.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.jdbc.StringUtils;
import com.zhengyibao.yygh.cmn.service.DictService;
import com.zhengyibao.yygh.common.result.Result;
import com.zhengyibao.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Api(tags="数据字典接口")
@RestController
@RequestMapping("/admin/cmn/dict")
public class DictController {
@Autowired
private DictService dictService;
@ApiOperation(value = "根据id查询id下面的子分类")
@GetMapping("findChildData/{id}")
public Result findChildData(@PathVariable Long id){
List<Dict> dictList=dictService.findChildData(id);
return Result.ok(dictList);
}
}
11.测试
访问http://localhost:8202/swagger-ui.html进行swagger2测试
|