spring boot——集成redis——入门学习
我这个工程是在集成mybatis的基础上添加的:
启动redis服务:
redis安装后,输入如下数据:
pom文件:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>anxinfu</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>anxinfu Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.5</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- 依赖Spring Boot Data Redis --> <!-- 依赖Spring Boot Data Redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <exclusions> <exclusion> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> </exclusion> </exclusions> </dependency> <!--对象池化组件,配合Java客户端JedisPool使用--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency> <!--jedis的jar包--> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.3.0</version> </dependency> </dependencies> <build> <finalName>anxinfu</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build> </project>
application.properties文件中配置连接信息:(windows本地安装的redis,什么也不要修改)
#Redis\u6570\u636E\u5E93\u7D22\u5F15\uFF08\u9ED8\u8BA4\u4E3A0\uFF09 spring.redis.database=0 #Redis\u670D\u52A1\u5668\u5730\u5740\uFF08\u9ED8\u8BA4\u4E3Alocalhost\uFF09 spring.redis.host=127.0.0.1 #Redis\u670D\u52A1\u5668\u8FDE\u63A5\u7AEF\u53E3\uFF08\u9ED8\u8BA4\u4E3A6379\uFF09 spring.redis.port=6379 #Redis\u670D\u52A1\u5668\u8FDE\u63A5\u5BC6\u7801\uFF08\u9ED8\u8BA4\u4E3A\u7A7A\uFF09 spring.redis.password= #\u8FDE\u63A5\u8D85\u65F6\u65F6\u95F4\uFF08\u6BEB\u79D2\uFF09 spring.redis.timeout=3600 #\u662F\u5426\u4F7F\u7528ssl\uFF0C\u9ED8\u8BA4\u4E3Afalse spring.redis.ssl=false #\u914D\u7F6E\u8FDE\u63A5\u6C60 #\u5982\u679C\u4F7F\u7528lettuce \u5219\u5C06jedis\u6539\u6210lettuce\u5373\u53EF #\u8FDE\u63A5\u6C60\u6700\u5927\u963B\u585E\u7B49\u5F85\u65F6\u95F4\uFF08\u4F7F\u7528\u8D1F\u503C\u8868\u793A\u6CA1\u6709\u9650\u5236\uFF09 \u9ED8\u8BA4\u503C\u4E3A-1 spring.redis.lettuce.pool.max-wait=-1 #\u8FDE\u63A5\u6C60\u6700\u5927\u8FDE\u63A5\u6570\uFF08\u4F7F\u7528\u8D1F\u503C\u8868\u793A\u6CA1\u6709\u9650\u5236\uFF09 \u9ED8\u8BA4\u503C\u4E3A8 spring.redis.lettuce.pool.max-active=8 #\u8FDE\u63A5\u6C60\u4E2D\u7684\u6700\u5927\u7A7A\u95F2\u8FDE\u63A5 \u9ED8\u8BA4\u503C\u4E3A8 spring.redis.lettuce.pool.max-idle=8 #\u8FDE\u63A5\u6C60\u4E2D\u7684\u6700\u5C0F\u7A7A\u95F2\u8FDE\u63A5 \u9ED8\u8BA4\u503C\u4E3A0 spring.redis.lettuce.pool.min-idle=0 #\u8FDE\u63A5\u6C60\u6700\u5927\u963B\u585E\u7B49\u5F85\u65F6\u95F4\uFF0C\u5355\u4F4D\u6BEB\u79D2\uFF08\u4F7F\u7528\u8D1F\u503C\u8868\u793A\u6CA1\u6709\u9650\u5236\uFF09 spring.redis.jedis.pool.max-wait=3600 #\u8FDE\u63A5\u6C60\u6700\u5927\u8FDE\u63A5\u6570\uFF08\u4F7F\u7528\u8D1F\u503C\u8868\u793A\u6CA1\u6709\u9650\u5236\uFF09 \u9ED8\u8BA4\u503C\u4E3A8 spring.redis.jedis.pool.max-active=8 #\u8FDE\u63A5\u6C60\u4E2D\u7684\u6700\u5927\u7A7A\u95F2\u8FDE\u63A5 \u9ED8\u8BA4\u503C\u4E3A8 spring.redis.jedis.pool.max-idle=8 #\u8FDE\u63A5\u6C60\u4E2D\u7684\u6700\u5C0F\u7A7A\u95F2\u8FDE\u63A5 \u9ED8\u8BA4\u503C\u4E3A0 spring.redis.jedis.pool.min-idle=0
RedisConfig配置:
package com.test.redisonfig; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator; 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.*; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration public class RedisConfig { /** * 配置自定义redisTemplate * @return */ @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { // 创建一个RedisTemplate对象 RedisTemplate<String, Object> template = new RedisTemplate<>(); // 设置连接工厂 template.setConnectionFactory(redisConnectionFactory); // 创建Jackson2JsonRedisSerializer序列化类 Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class); // 自定义ObjectMapper ObjectMapper mapper = new ObjectMapper(); // 指定要序列化的域和可见性,ALL表示可以访问所有的属性,ANY表示所有的包括private和public可见 mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); // 指定序列化输入类型,整个类、除final外的的属性信息都需要被序列化和反序列化 mapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance , ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_ARRAY); serializer.setObjectMapper(mapper); // 使用StringRedisSerializer来序列化和反序列化redis的key值 template.setKeySerializer(new StringRedisSerializer()); // 使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认用的是JDK序列化) template.setValueSerializer(serializer); //配置hash的key、value序列化 template.setHashKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(serializer); // 初始化操作 template.afterPropertiesSet(); return template; } /** * 对hash类型的数据操作 * @param redisTemplate * @return */ @Bean public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForHash(); } /** * 对列表类型的数据操作 * @param redisTemplate * @return */ @Bean public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForList(); } /** * 对无序集合类型的数据操作 * @param redisTemplate * @return */ @Bean public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForSet(); } /** * 对字符串类型的数据操作 * @param redisTemplate * @return */ @Bean public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForValue(); } /** * 对有序集合类型的数据操作 * @param redisTemplate * @return */ @Bean public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForZSet(); } }
RedisService服务类:
package com.test.serviceredis; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; import java.util.Set; @Service public class RedisService { // @Autowired // private RedisTemplate redisTemplate; @Autowired private StringRedisTemplate stringRedisTemplate; public Set index() { return stringRedisTemplate.keys("*"); } public boolean clearKey(String key) { return stringRedisTemplate.delete(key); } public void set(String key,String value) { stringRedisTemplate.opsForValue().set(key,value); } public String get(String key) { return stringRedisTemplate.opsForValue().get(key); } }
控制类:
IndexController:
package com.test.controller; import com.test.serviceredis.RedisService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.Set; /** * @Auther: moerhai@qq.com * @Date: 2020/4/4 0:04 */ @RestController public class IndexController { @Autowired private RedisService redisService; @RequestMapping("/index") public Set index() { return redisService.index(); } @RequestMapping("/set") public String set(@RequestParam("key") String key,@RequestParam("value") String value){ redisService.set(key,value); return "SUCCESS"; } @RequestMapping("/get/{key}") public String get(@PathVariable String key){ return redisService.get(key); } @RequestMapping("/delete") public String delete(String key){ if(redisService.clearKey(key)){ return "SUCCESS"; }else{ return "FAIL"; } } }
http://localhost:8080/get/myKey003
http://localhost:8080/index
http://localhost:8080/set?key=myKey007&value=abc007
http://localhost:8080/index