1构造
2User类:
package springboot_redis.bean;
import java.util.Date;
public class User {
/**
* 用户年龄
*/
private int age;
/**
* 用户密码
*/
private String pwd;
/**
* 用户手机
*/
private String phone;
/**
* 用户创建时间
*/
private Date createTime;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public User(int age, String pwd, String phone, Date createTime) {
super();
this.age = age;
this.pwd = pwd;
this.phone = phone;
this.createTime = createTime;
}
public User() {
super();
}
}
3JsonUtils
package springboot_redis.util;
import org.springframework.util.StringUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonUtils {
private static ObjectMapper objectMapper=new ObjectMapper();
/**
* 对象转字符串
* @param <T>
* @param obj
* @return
*/
public static <T> String objZString(T obj) {
if(obj==null) {
return null;
}
try {
return obj instanceof String ? (String) obj:objectMapper.writeValueAsString(obj);
} catch (Exception e) {
return null;
}
}
/**
* 字符串转对象
* @param <T>
* @param str
* @param clazz
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T stringZObj(String str,Class<T> clazz) {
if(StringUtils.isEmpty(str)||clazz==null) {
return null;
}
try {
return clazz.equals(String.class)?(T) str :objectMapper.readValue(str, clazz);
} catch (Exception e) {
return null;
}
}
}
4RedisClient
package springboot_redis.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
/**
* redis工具类
* @author Administrator
*
*/
@Component
public class RedisClient {
@Autowired
private StringRedisTemplate redisImpl;
/**
* 设置key-value到redis
* @param key
* @param value
* @return
*/
public boolean set(String key,String value) {
try {
redisImpl.opsForValue().getAndSet(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 通过key获取redis中的值
* @param key
* @return
*/
public String get(String key) {
return redisImpl.opsForValue().get(key);
}
}
5SpringbootRedisController
package springboot_redis.controller;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springboot_redis.bean.User;
import springboot_redis.util.JsonData;
import springboot_redis.util.JsonUtils;
import springboot_redis.util.RedisClient;
@RestController
@RequestMapping("api/v1/redis")
public class SpringbootRedisController {
/**
* StringRedisTemplate是redis的模版
* 可以通过ctrl+鼠标左键查看对应的redis的实现方法
* 用的比较多的是StringRedisTemplate.ops开头的方法
*/
//@Autowired
//private StringRedisTemplate redisImpl;
/**
* redis的封装
*/
@Autowired
private RedisClient redis;
@GetMapping(value = "add")
public Object add() {
//redisImpl.opsForValue().set("name","李明");
redis.set("name", "李明");
return JsonData.buildSuccess("ok");
}
@GetMapping(value = "get")
public Object get() {
//String value=redisImpl.opsForValue().get("name");
String value=redis.get("name");
return JsonData.buildSuccess(value);
}
/**
* 向redis中存key-value,返回布尔值
* @return
*/
@GetMapping(value = "save_user")
public Object saveUser() {
//String value=redisImpl.opsForValue().get("name");
User user=new User(1, "lily", "ss", new Date());
String useStr=JsonUtils.objZString(user);
boolean flag=redis.set("base:user:ss", useStr);
return JsonData.buildSuccess(flag);
}
/**
* 通过key来查找redis中对应的value
* @return
*/
@GetMapping(value = "find_user")
public Object findUser() {
String userStr=redis.get("base:user:ss");
User user=JsonUtils.stringZObj(userStr, User.class);
return JsonData.buildSuccess(user);
}
}
7通过接口访问
set:localhost:8080/api/v1/redis/save_user
get:localhost:8080/api/v1/redis/find_user
8通过https://redisdesktop.com/pricing 访问官网,下载redis_cli,或者去下载我的redisclient :https://www.cnblogs.com/zhushilai/p/13628297.html
可以在其中查看redis中对应的值
9单元测试,右键点击testOne这个方法,可以去redis的客户端查看是否生成了对应的值