SpringBoot集成redis

SpringBoot集成redis

1.通过注解方式

1.1导入依赖

  implementation group: 'org.springframework.boot', name: 'spring-boot-starter-data-redis', version: '2.4.10'

1.2 在yml文件中配置redis

spring:
  redis:
    host: localhost
    password:
    port: 6379

1.3 在使用的类中注入

	@Autowired
    private RedisTemplate redisTemplate;
	...........
 	@Override
    public Map getAllPolyline() {
        Map map = new HashMap();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        OcPolylineData polyline = (OcPolylineData)redisTemplate.opsForValue().get("Polyline1");
        log.info("第一次从查询redis"+polyline);
        List<OcPolylineData> ocPolylineDataList = polylineDataMapper.selectAllType();
        redisTemplate.opsForValue().set("Polyline1",ocPolylineDataList);
		map.put("polylineDataList",ocPolylineDataList);
        map.put("Code", 200);
        map.put("Desc", "成功");
        return map;
    }

1.4测试一下

第一此查询
![在这里插入图片描述]( https://img-blog.csdnimg.cn/f665e19c8aa641fbaea3022e89b72938.png?x-oss-process=image/watermark ,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBARE5ZRFlT,size_20,color_FFFFFF,t_70,g_se,x_16)

可以看到redis 里面是空的

第二次 可以看到redis里面是有的
![在这里插入图片描述]( https://img-blog.csdnimg.cn/486a9f06f7e04b06b3efbf63ddc8d09c.png?x-oss-process=image/watermark ,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBARE5ZRFlT,size_20,color_FFFFFF,t_70,g_se,x_16)
emm其他的方式后续会更新
在这里插入图片描述

posted @ 2021-11-14 18:47  dnydys  阅读(50)  评论(0)    收藏  举报