教学来源:
https://www.bilibili.com/video/BV1cr4y1671t?p=1一、Redis 入门
Redis在虚拟机中安装和配置开机自启-CSDN博客
https://blog.csdn.net/qq_69183322/article/details/138168301






引入依赖:
- <dependency>
- <groupId>redis.clients</groupId>
- <artifactId>jedis</artifactId>
- <version>3.7.0</version>
- </dependency>
代码示例:
- package com.example.redisdemo;
-
- import org.junit.jupiter.api.AfterEach;
- import org.junit.jupiter.api.BeforeEach;
- import org.junit.jupiter.api.Test;
- import org.springframework.boot.test.context.SpringBootTest;
- import redis.clients.jedis.Jedis;
-
- import java.util.Map;
-
- @SpringBootTest
- class RedisDemoApplicationTests {
-
- private Jedis jedis;
-
- @BeforeEach
- void setUp(){
- jedis = new Jedis("localhost",6379);
- //密码
- // jedis.auth("123");
- jedis.select(0);
- }
-
- @Test
- void testString(){
- String result = jedis.set("name","张四");
- System.out.println("result="+result);
-
- String name = jedis.get("name");
- System.out.println("name="+name);
- }
-
- @Test
- void testHash(){
- jedis.hset("user:1","name","Jack");
- jedis.hset("user:1","age","21");
-
- Map<String,String> map = jedis.hgetAll("user:1");
-
- System.out.println(map);
- }
-
- @AfterEach
- void tearDown(){
- if(jedis !=null){
- jedis.close();
- }
- }
-
- }
在utils中写Jedis连接池的配置:
- package com.example.redisdemo.utils;
-
- import redis.clients.jedis.Jedis;
- import redis.clients.jedis.JedisPool;
- import redis.clients.jedis.JedisPoolConfig;
-
- public class JedisConnectionFactory {
-
- private static final JedisPool jedisPool;
-
- static {
- JedisPoolConfig poolConfig = new JedisPoolConfig();
- poolConfig.setMaxTotal(8);
- poolConfig.setMaxIdle(8);
- poolConfig.setMaxIdle(0);
- poolConfig.setMaxWaitMillis(1000);
-
- jedisPool = new JedisPool(poolConfig,"192.168.92.136",6379,1000,"123456");
- }
-
- public static Jedis getJedis(){
- return jedisPool.getResource();
- }
- }
在test中进行测试:
- package com.example.redisdemo;
-
- import com.example.redisdemo.utils.JedisConnectionFactory;
- import org.junit.jupiter.api.AfterEach;
- import org.junit.jupiter.api.BeforeEach;
- import org.junit.jupiter.api.Test;
- import org.springframework.boot.test.context.SpringBootTest;
- import redis.clients.jedis.Jedis;
-
- import java.util.Map;
-
- @SpringBootTest
- class RedisDemoApplicationTests {
-
- private Jedis jedis;
-
- @BeforeEach
- void setUp(){
- // jedis = new Jedis("192.168.92.136",6379);
- jedis = JedisConnectionFactory.getJedis();
- jedis.auth("123456");
- jedis.select(0);
- }
-
- @Test
- void testString(){
- String result = jedis.set("name","张四");
- System.out.println("result="+result);
-
- String name = jedis.get("name");
- System.out.println("name="+name);
- }
-
- @Test
- void testHash(){
- jedis.hset("user:1","name","Jack");
- jedis.hset("user:1","age","21");
-
- Map<String,String> map = jedis.hgetAll("user:1");
-
- System.out.println(map);
- }
-
- @AfterEach
- void tearDown(){
- if(jedis !=null){
- jedis.close();
- }
- }
-
- }

- <!-- Redis依赖-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-redis</artifactId>
- </dependency>
- <!-- 连接池依赖-->
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-pool2</artifactId>
- </dependency>
- server:
- port: 8080
-
- spring:
- redis:
- host: 192.168.92.136
- port: 6379
- password: 123456
- lettuce:
- pool:
- max-active: 8 #最大连接
- max-idle: 8 #最大空闲连接
- min-idle: 0 #最小空闲连接
- max-wait: 100 #连接等待时间
- package com.example.redisdemo;
-
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.data.redis.core.RedisTemplate;
-
- @SpringBootTest
- class RedisDemoApplicationTests {
-
- @Autowired
- private RedisTemplate redisTemplate;
-
- @Test
- void testString(){
- redisTemplate.opsForValue().set("name","小龙");
- Object name = redisTemplate.opsForValue().get("name");
- System.out.println("name="+name);
- }
-
- }
配置序列化 config.RedisConfig
- package com.example.redisdemo.config;
-
- 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.GenericJackson2JsonRedisSerializer;
- import org.springframework.data.redis.serializer.RedisSerializer;
-
- @Configuration
- public class RedisConfig {
-
- @Bean
- public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory connectionFactory){
-
- RedisTemplate<String,Object> template = new RedisTemplate<>();
- //设置连接工厂
- template.setConnectionFactory(connectionFactory);
- //创建Json序列化工具
- GenericJackson2JsonRedisSerializer jsonRedisSerializer =new GenericJackson2JsonRedisSerializer();
- //设置Key的序列化
- template.setKeySerializer(RedisSerializer.string());
- template.setHashKeySerializer(RedisSerializer.string());
- //设置Value的序列化
- template.setValueSerializer(jsonRedisSerializer);
- template.setHashValueSerializer(jsonRedisSerializer);
-
- return template;
- }
-
- }
创建实体 pojo.User
- package com.example.redisdemo.pojo;
-
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- @Data
- @AllArgsConstructor
- @NoArgsConstructor
- public class User {
- private String name;
- private Integer age;
- }
在Test中编写测试用例
- package com.example.redisdemo;
-
- import com.example.redisdemo.pojo.User;
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.data.redis.core.RedisTemplate;
-
- @SpringBootTest
- class RedisDemoApplicationTests {
-
- @Autowired
- private RedisTemplate<String,Object> redisTemplate;
-
- @Test
- void testString(){
- redisTemplate.opsForValue().set("name","小龙");
- Object name = redisTemplate.opsForValue().get("name");
- System.out.println("name="+name);
- }
-
- @Test
- void testSaveUser(){
- redisTemplate.opsForValue().set("user:100",new User("小虎",21));
- User user = (User) redisTemplate.opsForValue().get("user:100");
- System.out.println("user="+user);
- }
-
-
- }
报错可能没有引入Jackson依赖
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-databind</artifactId>
- </dependency>

示例代码:
- package com.example.redisdemo;
-
- import com.example.redisdemo.pojo.User;
- import com.fasterxml.jackson.core.JsonProcessingException;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.data.redis.core.StringRedisTemplate;
-
- import java.util.Map;
-
- @SpringBootTest
- class RedisDemoApplicationTests {
-
- @Autowired
- private StringRedisTemplate stringRedisTemplate;
-
- //Json工具
- private static final ObjectMapper mapper = new ObjectMapper();
-
- @Test
- void testStringTemplate() throws JsonProcessingException{
- User user = new User("小明",20);
- //手动序列化
- String json = mapper.writeValueAsString(user);
-
- stringRedisTemplate.opsForValue().set("user:200",json);
-
- String val = stringRedisTemplate.opsForValue().get("user:200");
- //反序列化
- User user1 = mapper.readValue(val, User.class);
- System.out.println("user1:"+user1);
- }
-
- @Test
- void testHash(){
- stringRedisTemplate.opsForHash().put("user:400","name","小胡");
- stringRedisTemplate.opsForHash().put("user:400","age","21");
-
- Map<Object,Object> entries = stringRedisTemplate.opsForHash().entries("user:400");
- System.out.println("entries:"+entries);
- }
-
- }