尚医通(二)
一、数据字典系统
什么是数据字典?数据字典是管理系统中常用的分类数据或一些固定数据,如省市三级联动数据、国家数据、行业数据、教育数据等。由于该系统广泛使用这些数据,我们需要进行数据管理,以便于管理系统数据。一般来说,系统基本上会进行数据管理。
service下创建service_cmn模块,将dict表逆向工程配置分页和swagger
parent_id:
上级id,通过id与parent_id例如,如果我们想获取所有的行业数据,我们只需要查询parent_id=20000的数据
name:例如,我们需要填写用户信息select汉族是数据字典的名字
value:例如,我们需要填写用户信息select1(汉族标志)是数据字典的值
dict_code:编码,编码是我们自定义的,整体上是唯一的例如,我们可以通过获取行业数据parent_id获取,但是parent_id这是不确定的,所以我们可以根据编码获取行业数据
若要获取民族数据,dict_code = “民族”
element ui 树形表格数据
二、数据字典
这里只写service 实现类
@Service public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {
@Autowired private DictMapper dictMapper; //根据数据id查询子数据列表 @Override @Cacheable(value = "dict",keyGenerator = "keyGenerator") //添加缓存 public List<Dict> findChildData(Long id) {
QueryWrapper<Dict> wrapper = new QueryWrapper<>(); wrapper.eq("parent_id", id); List<Dict> dictList = baseMapper.selectList(wrapper); //向list每个dict对象中设置hasChildren值 for (Dict dict : dictList) {
Long dictId = dict.getId();
boolean ischild = this.ischildren(dictId);
dict.setHasChildren(ischild);
}
return dictList;
}
//判断id下面是否有子节点
private boolean ischildren(Long id) {
QueryWrapper<Dict> wrapper = new QueryWrapper<>();
wrapper.eq("parent_id", id);
Integer count = baseMapper.selectCount(wrapper);
return count > 0;
}
//导出数据字典接口
@Override
public void exportDictData(HttpServletResponse response) {
//设置下载信息
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String fileName = "dict";
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
//查询数据库
List<Dict> dictList = baseMapper.selectList(null);
//把Dict对象转换成DictEeVo对象
List<DictEeVo> dictVoList = new ArrayList<>(dictList.size());
for(Dict dict :dictList){
DictEeVo dictEeVo = new DictEeVo();
BeanUtils.copyProperties(dict,dictEeVo);
dictVoList.add(dictEeVo);
}
//调用方法进行写操作
try {
EasyExcel.write(response.getOutputStream(), DictEeVo.class)
.sheet("dict")
.doWrite(dictVoList);
} catch (IOException e) {
e.printStackTrace();
}
}
//导入数据字典
@Override
@CacheEvict(value = "dict", allEntries=true) //表示清空缓存中的所有内容
public void importDictData(MultipartFile file) {
try {
EasyExcel.read(file.getInputStream(),DictEeVo.class,new DictListener(dictMapper))
.sheet().doRead();
} catch (IOException e) {
e.printStackTrace();
}
}
//查询名称
@Override
public String getDictName(String dictCode, String value) {
//如果dictCode为空,直接根据value查询
if(StringUtils.isEmpty(dictCode)){
QueryWrapper queryWrapper = new QueryWrapper();
queryWrapper.eq("value",value);
Dict dict = dictMapper.selectOne(queryWrapper);
return dict.getName();
}else{
//如果dictCode不为空,根据dictCode和value查询
//根据dictcode查询dict对象,目的是得到dict的id值,在根据此id值查询此id下的子id
QueryWrapper<Dict> queryWrapper1 = new QueryWrapper();
queryWrapper1.eq("dict_code",dictCode);
Dict codeDict = dictMapper.selectOne(queryWrapper1);
Long parent_id = codeDict.getId();
//根据parentId和value值进行查询
QueryWrapper<Dict> queryWrapper2 = new QueryWrapper();
queryWrapper2.eq("parent_id",parent_id).eq("value",value);
Dict finalDict = baseMapper.selectOne(queryWrapper2);
return finalDict.getName();
}
}
//根据dictcode查询查询子节点
@Override
public List<Dict> findByDictCode(String dictCode) {
//根据dictcode获取对应的id
QueryWrapper<Dict> queryWrapper1 = new QueryWrapper();
queryWrapper1.eq("dict_code",dictCode);
Dict codeDict = dictMapper.selectOne(queryWrapper1);
//根据id获取子节点
List<Dict> childData = this.findChildData(codeDict.getId());
return childData;
}
二、Spring Cache Redis
Spring Cache 是一个非常优秀的缓存组件。自Spring 3.1起,提供了类似于@Transactional注解事务的注解Cache支持,且提供了Cache抽象,方便切换各种底层Cache(如:redis)
使用Spring Cache的好处:
1,提供基本的Cache抽象,方便切换各种底层Cache;
2,通过注解Cache可以实现类似于事务一样,缓存逻辑透明的应用到我们的业务代码上,且只需要更少的代码就可以完成;
3,提供事务回滚时也自动回滚缓存;
4,支持比较复杂的缓存逻辑;
service_util配置redis
package com.atguigu.yygh.common.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.lang.reflect.Method;
import java.time.Duration;
/** * @author wangyihui * @create 2021-04-25 15:40 */
@Configuration
@EnableCaching // 开启缓存处理
public class RedisConfig {
private RedisCacheManager build;
/** * 自定义key规则 * * @return */
@Bean
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append(method.getName());
for (Object obj : params) {
sb.append(obj.toString());
}
return sb.toString();
}
};
}
/** * 设置RedisTemplate规则 * * @param redisConnectionFactory * @return */
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//解决查询缓存转换异常的问题
ObjectMapper om = new ObjectMapper();
// 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
//序列号key value
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
/** * 设置CacheManager缓存规则 * * @param factory * @return */
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//解决查询缓存转换异常的问题
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
// 配置序列化(解决乱码的问题),过期时间600秒
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofSeconds(600))
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
.disableCachingNullValues();
RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
.cacheDefaults(config)
.build();
return cacheManager;
}
}
Spring Cache相当于自动缓存,对于redis需要用RedisTemplate不需要手动set,get
常用注解
根据方法对其返回结果进行缓存,下次请求时,如果缓存存在,则直接读取缓存数据返回;如果缓存不存在,则执行方法,并把返回的结果存入缓存中。一般用在查询方法上。
使用该注解标志的方法,每次都会执行,并将结果存入指定的缓存中。其他方法可以直接从响应的缓存中读取缓存数据,而不需要再去查询数据库。一般用在新增方法上。
@CacheEvict
使用该注解标志的方法,会清空指定的缓存。
spring cache 与redis的区别
1、对于redis的缓存,springcache只支持String,其它的Hash、List、Set、Zset都不支持,所以对于Hash、List、Set、Zset只能用RedisTemplate
2、Spring cache 只支持单表的数据缓存,Redis支持多表的数据缓存
三、整合MongoDB数据库
docker安装mongodb 这里不要配置用用户密码使用mongo
直接原生启动容器
docker run -d --restart=always -p 27017:27017 --name mymongo -v /data/db:/data/db -d mongo
配置mongodb连接,你的mongodb数据库没有yygh_hosp也可以配置连接
#mongodb
spring.data.mongodb.uri=mongodb://47.108.170.87:27017/yygh_hosp
**注意:**这里尽量别使用需要密码验证才能使用mongoDb数据库,不然你远程配置连接贼鸡儿烦
mongodb用来存储医院相关的数据信息
这里要自定义三个MongoDb的dao层,分别对应医院、科室、排版
@Repository
public interface HospitalRepository extends MongoRepository<Hospital,String> {
//判断是否存在数据
Hospital getHospitalByHoscode(String hoscode); //MongoRepository有一套规范,直接按照规范命名,会直接调用写好的方法
//根据医院名称查询
List<Hospital> findHospitalByHosnameLike(String hosname);
}
import com.wang.yygh.model.hosp.Department;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
/** * @author wangyihui * @create 2021-11-11 10:03 */
@Repository
public interface DepartmentRepository extends MongoRepository<Department,String> {
Department getDepartmentByHoscodeAndDepcode(String hoscode, String depcode);
}
import com.wang.yygh.model.hosp.Schedule;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
import java.util.Date;
import java.util.List;
/** * @author wangyihui * @create 2021-11-11 15:51 */
@Repository
public interface ScheduleRepository extends MongoRepository<Schedule,String> {
//根据医院编号和排班编号进行查询
Schedule getScheduleByHoscodeAndHosScheduleId(String hoscode, String hosScheduleId);
//根据医院编号、科室编号和工作日期,查询排班详情信息
List<Schedule> findScheduleByHoscodeAndDepcodeAndWorkDate(String hoscode, String depcode, Date toDate);
}
service impl
@Service
public class HospitalSevriceImpl implements HospitalService {
@Autowired
private HospitalRepository hospitalRepository;
@Autowired
private DictFeignClient dictFeignClient;
@Override
public void save(Map<String, Object> parapMap) {
//把参数的map集合转换成对象Hospital,方便操作
String mapString = JSONObject.toJSONString(parapMap); //先把map转换成字符串
Hospital hospital = JSONObject.parseObject(mapString, Hospital.class); // 把字符串转换成Hospital对象
//判断是否存在相同数据
String hoscode = hospital.getHoscode();
Hospital hospitalExist = hospitalRepository.getHospitalByHoscode(hoscode);
//如果存在,进行修改
if(hospitalExist != null){
hospital.setStatus(hospitalExist.getStatus());
hospital.setCreateTime(hospitalExist.getCreateTime());
hospital.