1 pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2 application.yml
spring:
redis:
database: 0
host: 127.0.0.1
port: 6379
password: china@asia
jedis:
pool:
max-active: 8
max-idle: 8
max-wait: -1ms
min-idle: 0
3 配置
package com.sb.config;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.io.Serializable;
@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisConfig{
@Bean
public RedisTemplate<String, Serializable> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory){
RedisTemplate<String, Serializable> template = new RedisTemplate<>();
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
4 实体类
package com.sb.po;
import java.io.Serializable;
public class PeopleInfos implements Serializable{
private String name;
private String address;
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setAddress(String address){
this.address = address;
}
public String getAddress(){
return address;
}
}
5 增删改查
package com.sb.controller;
import com.sb.po.PeopleInfos;
import java.util.Map;
import java.util.HashMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.stereotype.Controller;
@CrossOrigin(origins="*", maxAge=3600)
@Controller
@RequestMapping("/api/redis")
public class RedisOpController{
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private RedisTemplate redisTemplate;
@RequestMapping(value="/add", method=RequestMethod.POST)
@ResponseBody
public Map addDataToRedis(@RequestBody Map infos){
Map tempMap = new HashMap();
try{
if(infos.containsKey("username")&&infos.containsKey("password")&&infos.containsKey("address")&&infos.containsKey("objname")){
String username = infos.get("username").toString();
String password = infos.get("password").toString();
String address = infos.get("address").toString();
String objname = infos.get("objname").toString();
stringRedisTemplate.opsForValue().set("username", username);
stringRedisTemplate.opsForValue().set("password", password);
PeopleInfos peopleInfos = new PeopleInfos();
peopleInfos.setName(username);
peopleInfos.setAddress(address);
redisTemplate.opsForValue().set(objname, peopleInfos);
tempMap.put("code", 200);
tempMap.put("infos", "添加成功");
return tempMap;
}else{
tempMap.put("code", 400);
tempMap.put("infos", "检查参数");
return tempMap;
}
}catch (Exception e){
e.printStackTrace();
}
return tempMap;
}
@RequestMapping(value="/delete")
@ResponseBody
public Map deleteByKey(@RequestBody Map infos){
Map tempMap = new HashMap();
try{
if(infos.containsKey("username")&&infos.containsKey("password")&&infos.containsKey("objname")){
String username = infos.get("username").toString();
String password = infos.get("password").toString();
String objname = infos.get("objname").toString();
stringRedisTemplate.delete(username);
stringRedisTemplate.delete(password);
redisTemplate.delete(objname);
tempMap.put("code", 200);
tempMap.put("infos", "删除成功");
}else{
tempMap.put("code", 400);
tempMap.put("infos", "检查参数");
}
}catch (Exception e){
e.printStackTrace();
}
return tempMap;
}
@RequestMapping(value="/search/obj", method=RequestMethod.POST)
@ResponseBody
public PeopleInfos searchDataInRedis(@RequestBody Map infos){
PeopleInfos peopleInfos = new PeopleInfos();
try{
if(infos.containsKey("objname")){
String objname = infos.get("objname").toString();
peopleInfos = (PeopleInfos) redisTemplate.opsForValue().get(objname);
redisTemplate.opsForValue().set("userInfosObj", peopleInfos);
}
}catch (Exception e){
e.printStackTrace();
}
return peopleInfos;
}
@RequestMapping(value="/search/string", method=RequestMethod.POST)
@ResponseBody
public Map searchDataInRedisString(@RequestBody Map infos){
Map infosOut = new HashMap();
String usernameR, passwordR;
try{
if(infos.containsKey("username")&&infos.containsKey("password")){
String username = infos.get("username").toString();
String password = infos.get("password").toString();
usernameR = stringRedisTemplate.opsForValue().get(username);
passwordR = stringRedisTemplate.opsForValue().get(password);
infosOut.put("username", usernameR);
infosOut.put("password", passwordR);
}
}catch (Exception e){
e.printStackTrace();
}
return infosOut;
}
}
6 小结
序号 | 描述 |
---|---|
1 | 配置redis使用序列化存储RedisTemplate<String, Serializable> |
2 | 实体类数据存储使用序列化 |
3 | 存储字符串使用StringRedisTemplate,存储对象使用RedisTemplate |
4 | 数据查询使用键名搜索 |
【参考文献】
[1]https://blog.csdn.net/stronglyh/article/details/81173563
[2]https://blog.csdn.net/pw191410147/article/details/82144183