一、引入maven依赖
<!--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>
<version>2.11.1</version>
</dependency>
二、配置application
redis:
host: localhost
port: 6379
database: 0
timeout: 1000
jedis:
pool:
max-active: 8
min-idle: 5
max-wait: -1
三、redis的set、get
(1)RedisCtroller
package com.mgx.controller;
import com.mgx.service.RedisService;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/**
* @author mgx
* @date 2023/9/17 2:35 PM
*/
@RestController
@RequestMapping("/redis")
public class RedisController {
@Resource
private RedisService redisService;
@PostMapping("/add")
public String add(@Param("key") String key, @Param("value")String value) {
return redisService.add(key,value);
}
@GetMapping("/get")
public Object getUserInfo(@Param("key") String key) {
return redisService.get(key);
}
}
RedisService
package com.mgx.service;
/**
* @author mgx
* @date 2023/9/17 2:36 PM
*/
public interface RedisService {
String add(String key,String value);
Object get(String key);
}
RedisServiceImpl
package com.mgx.service.impl;
import com.mgx.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
/**
* @author mgx
* @date 2023/9/17 2:37 PM
*/
@Service
public class RedisServiceImpl implements RedisService {
@Autowired
private RedisTemplate redisTemplate;
@Override
public String add(String key, String value) {
redisTemplate.opsForValue().set(key,value);
return "添加成功";
}
@Override
public Object get(String key) {
return redisTemplate.opsForValue().get(key);
}
}
(2)使用postman接口调用
四、redis操作成功,但这个时候发现redis库中的key和value乱码,后期会不利于我们的维护。查看redisTemplate源码,我们发现是redis序列化的问题,因此需要对redis进行序列化指定。
package com.mgx.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.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* @author mgx
* @date 2023/9/17 3:09 PM
*/
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String , Object> redisTemplate(RedisConnectionFactory factory){
RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
//指定kv的序列化方式
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
return redisTemplate;
}
}
添加RedisConfig重启,调用接口,redis可视化界面已没有乱码
五、项目结构