本文介绍: 通过Java–业务场景:在Spring项目启动时加载Java枚举类到Redis中一文,我们成功将Java项目里的枚举类加载到Redis中了,接下来我们只需要写接口获取需要的枚举值数据就可以了,下面一起来编写这个接口吧。

文章目录

前言
步骤
  1. 在EnumService接口创建一个方法,负责查询枚举类的值。

    public interface EnumService {
        /**
         * 获取枚举类
         * 支持通过field模糊查询
         * 
         * @param field redis hash 存储中 的 field(HashMap中的key)
         * @return 枚举类
         */
        Map<String, List<EnumDto>> getEnumValues(String field);
        
        //其他方法....
    }
    
  2. 在EnumServiceImpl中实现getEnumValues方法。

    @Service
    @Slf4j
    public class EnumServiceImpl implements EnumService {
        @Autowired
        private RedisOperation redisOperation;
    
        @Override
        public Map<String, List<EnumDto>> getEnumValues(String field) {
            Map<String, List<EnumDto>> returnObj = new HashMap<>();
            Map<Object, Object> obj;
            if (StringUtils.isBlank(field)) {
                //获取所有枚举类的信息
                obj = redisOperation.hgetAll(RedisKeyConstant.SYSTEM_ENUMS_CACHE_KEY);
            } else {
                //下面是针对需要进行模糊匹配进行的查询
                obj = redisOperation.hscan(RedisKeyConstant.SYSTEM_ENUMS_CACHE_KEY, field);
            }
            obj.forEach((redisKey, val) ->
                    returnObj.put((String) redisKey, (ArrayList) val));
            return returnObj;
        }
    }
    
  3. 下面给出EnumServiceImpl中出现的一些RedisOperation中的方法代码

    @Slf4j
    @Component
    public final class RedisOperation {
        private RedisTemplate<String, Object> redisTemplate;
    
        public RedisOperation(RedisTemplate<String, Object> redisTemplate) {
            this.redisTemplate = redisTemplate;
        }
        /**
         * 返回哈希表 key 中,所有的域和值
         */
        public Map<Object, Object> hgetAll(String key) {
             return redisTemplate.opsForHash().entries(key);
        }
    
        /**
         * 针对HashKey进行  field 的模糊匹配
         *
         * @param key   redis的HashKey  精确匹配
         * @param field reidis 的HashKey中的 field 类似于java中的HashMap中的key
         * @return 根据精确匹配key 和 模糊匹配 field 获取存储在redis中 的 HashMap
         */
        public Map<Object, Object> hscan(String key, String field) {
            Cursor<Map.Entry<Object, Object>> cursor = null;
            Map<Object, Object> map = new HashMap<>();
            try {
                cursor = redisTemplate.opsForHash().scan(key, ScanOptions.scanOptions().count(Integer.MAX_VALUE).match("*" + field + "*").build());
                while (cursor.hasNext()) {
                    Map.Entry<Object, Object> entry = cursor.next();
                    map.put(entry.getKey(), entry.getValue());
                }
                return map;
            } catch (Exception e) {
                log.error("redis模糊查询获取 HashMap error!", e);
            } finally {
                if (null != cursor) {
                    cursor.close();
                }
            }
            return map;
        }
    
        //其他方法...
    }
    
  4. 在Controller里定义接口,还记得EnumInterface接口里的enumDesc()方法吧,它返回了我们定义的枚举类描述值,我们可以通过这个描述来作为下面接口的field属性,进行模糊查询。

    @RestController
    @RequestMapping("/part/util")
    public class UtilController {
        @Autowired
        private EnumService enumService;
        @ApiOperation("获取JAVA枚举值列表")
        @GetMapping("/getEnumValues")
        public Result getEnumValues(@ApiParam(name = "field", value = "field(HashMap中的key)") @RequestParam(value = "field") String field) {
            return Result.ok().data(enumService.getEnumValues(field));
        }
    }
    
测试结果
  1. 采用postman测试结果,当输入的field为空时,返回所有枚举值:

在这里插入图片描述
2. 当输入的field不为空,根据field进行模糊查询,返回结果:
在这里插入图片描述

原文地址:https://blog.csdn.net/weixin_46599489/article/details/135481794

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_54588.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注