irpas技术客

Redis深入学习(二)使用SpringBoot连接Redis_一路向茜_springboot连接redis

未知 1029

1. 新建SpringBoot项目

创建一个maven项目spring-boot。

2. 添加相应依赖

修改项目的pom.xml文件配置。

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://·/artifact/org.springframework.boot/spring-boot-starter-data-redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> 3. 添加配置文件

创建application.yml文件,编辑配置信息。

4. 编写代码

创建启动类SpringbootRedisApplication。

package com.jeff.study; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; @SpringBootApplication @EnableCaching public class SpringbootRedisApplication { public static void main(String[] args) { SpringApplication.run(SpringbootRedisApplication.class); } }

创建配置类RedisConfig。

package com.jeff.study.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer; /** * @author jeff * @version 1.0 * @ClassName RedisConf * @Description TODO * @date 2022/2/17 16:47 */ @Configuration public class RedisConf { @Autowired private RedisConnectionFactory redisConnectionFactory; @Bean public RedisTemplate<String,Object> redisTemplate(){ RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>(); // 暂时先设置key和value的序列化都是字符串 redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new StringRedisSerializer()); redisTemplate.setConnectionFactory(redisConnectionFactory); return redisTemplate; } }

创建RedisController。

package com.jeff.study.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.concurrent.TimeUnit; /** * @author jeff * @version 1.0 * @ClassName RedisController * @Description TODO * @date 2022/2/17 16:54 */ @RestController @RequestMapping(value = "/redis") public class RedisController { @Autowired private RedisTemplate redisTemplate; @GetMapping(value = "/put") public String put(@RequestParam String key, @RequestParam String value){ //设置过期时间30秒 redisTemplate.opsForValue().set(key,value,30, TimeUnit.SECONDS); return "SUCCESS"; } @GetMapping(value = "/get") public String get(@RequestParam String key){ return (String) redisTemplate.opsForValue().get(key); } } 5. 测试验证

启动springboot-redis微服务。 浏览器访问接口,分别测试 /redis/put 和 /redis/get 接口。 调用/redis/put接口: 调用/redis/get接口:

使用redis-cli连接到redis服务器,查看key为name的数据是否存在,如果存在查看其有效期。 可以看到存在key=name的数据,其value=jeff,和我们页面上请求的一直,有效期还有26秒钟。 当key=name这个数据过期之后,数据将查询不到

至此,使用SpringBoot连接Redis来记录数据和查询数据功能都实现了。


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #1 #2 #ampltxml