mode实体定义
package cn.xxx.xxx.mode;
import java.io.Serializable;
/**
* 功能描述:响应结果类
*
*
*/
public class JsonData implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
// 状态码 0 表示成功,1表示处理中,-1表示失败
private Integer code;
// 数据
private Object data;
// 描述
private String msg;
public JsonData() {
}
public JsonData(Integer code, Object data, String msg) {
this.code = code;
this.data = data;
this.msg = msg;
}
// 成功,传入数据
public static JsonData buildSuccess() {
return new JsonData(0, null, null);
}
// 成功,传入数据
public static JsonData buildSuccess(Object data) {
return new JsonData(0, data, null);
}
// 失败,传入描述信息
public static JsonData buildError(String msg) {
return new JsonData(-1, null, msg);
}
// 失败,传入描述信息,状态码
public static JsonData buildError(String msg, Integer code) {
return new JsonData(code, null, msg);
}
// 成功,传入数据,及描述信息
public static JsonData buildSuccess(Object data, String msg) {
return new JsonData(0, data, msg);
}
// 成功,传入数据,及状态码
public static JsonData buildSuccess(Object data, int code) {
return new JsonData(code, data, null);
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
@Override
public String toString() {
return "JsonData [code=" + code + ", data=" + data + ", msg=" + msg
+ "]";
}
}
实体对象定义
package cn.xxx.xxx.mode;
import java.util.Date;
@Data
public class User {
private int age;
private String pwd;
private String phone;
private Date createTime;
}
工具类封装定义
package cn.xxx.xxx.utils;
import java.io.IOException;
import org.springframework.util.StringUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonUtils {
private static ObjectMapper objectMapper = new ObjectMapper();
//对象转字符串
public static <T> String obj2String(T obj){
if (obj == null){
return null;
}
try {
return obj instanceof String ? (String) obj : objectMapper.writeValueAsString(obj);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
//字符串转对象
public static <T> T string2Obj(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 (IOException e) {
e.printStackTrace();
return null;
}
}
}
redis工具类
package cn.xx.xx.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
/**
* 功能描述:redis工具类
*
*
*/
@Component
public class RedisClient {
//jdbcTemplate
@Autowired
private StringRedisTemplate redisTpl;
/**
* 功能描述:设置key-value到redis中
* @param key
* @param value
* @return
*/
public boolean set(String key ,String value){
try{
redisTpl.opsForValue().set(key, value);
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}
}
/**
* 功能描述:通过key获取缓存里面的值
* @param key
* @return
*/
public String get(String key){
return redisTpl.opsForValue().get(key);
}
/**
* 通过redis操作设置map
*/
public boolean setMap(String name,String key,Object value){
try {
redisTpl.opsForHash().put(name, key, value);
return true;
}catch (Exception e){
return false;
}
}
/**
* 通过reids操作获取map
*/
public Object getMap(String name,String key){
Object result=redisTpl.opsForHash().get(name,key);
return result;
}
}
控制层编写
package cn.xx.xx.controller;
import java.util.Date;
import cn.hello.bike.mode.JsonData;
import cn.hello.bike.mode.User;
import cn.hello.bike.utils.JsonUtils;
import cn.hello.bike.utils.RedisClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/v1/redis")
public class RdisTestController {
//jdbcTemplate
@Autowired
private StringRedisTemplate redisTpl;
@Autowired
private RedisClient redis;
@GetMapping(value="add")
public Object add(){
//redisTpl.opsForValue().set("name", "xdclass2018");
redis.set("username", "xxxx");
return JsonData.buildSuccess();
}
}