fix:获取redis信息

上级 cc58c4cf
package com.kwan.springbootkwan.constant;
public class CommonConstant {
/**
* 成功
*/
public static final Integer SC_OK_200 = 200;
/**
* 服务器错误
*/
public static final Integer SC_INTERNAL_SERVER_ERROR_500 = 500;
/**
* 未认证
*/
public static final int SC_JEECG_NO_AUTHZ = 401;
/**
* redis前缀
*/
public static final String PREFIX_REDIS_KEY = "test:";
}
package com.kwan.springbootkwan.controller; package com.kwan.springbootkwan.controller;
import com.kwan.springbootkwan.entity.Result;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
...@@ -10,35 +12,33 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -10,35 +12,33 @@ import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static com.kwan.springbootkwan.constant.CommonConstant.PREFIX_REDIS_KEY;
@Slf4j
@Api(value = "redis测试信息", tags = "RedisController") @Api(value = "redis测试信息", tags = "RedisController")
@RestController @RestController
@RequestMapping("/test") @RequestMapping("/test")
public class RedisController { public class RedisController {
public static final String REDIS_KEY = "test:_w";
@Resource @Resource
private RedisTemplate redisTemplate; private RedisTemplate redisTemplate;
@ApiOperation(value = "redis添加数据", notes = "redis添加数据") @ApiOperation(value = "redis添加数据", notes = "redis添加数据")
@GetMapping("/add") @GetMapping("/add")
public void add() { public Result add(String key, String value) {
//当前时间时间戳
String nowStr = String.valueOf(System.currentTimeMillis());
//设置redis中的key值
String redisKey = REDIS_KEY.concat(nowStr);
//向对应的key值添加数据 //向对应的key值添加数据
redisTemplate.opsForValue().set(REDIS_KEY, "测试向redis中添加数据"); redisTemplate.opsForValue().set(PREFIX_REDIS_KEY + key, value);
//设置过期时间 //设置过期时间
redisTemplate.expire(REDIS_KEY, 2000000, TimeUnit.SECONDS); redisTemplate.expire(PREFIX_REDIS_KEY, 2000000, TimeUnit.SECONDS);
return Result.ok();
} }
@ApiOperation(value = "redis获取数据", notes = "redis获取数据") @ApiOperation(value = "redis获取数据", notes = "redis获取数据")
@GetMapping("/get") @GetMapping("/get")
public void get() { public Result get(String key) {
//获取redis中的数据 //获取redis中的数据
String str = (String) redisTemplate.opsForValue().get(REDIS_KEY); String value = (String) redisTemplate.opsForValue().get(PREFIX_REDIS_KEY + key);
System.out.println("获取redis中的数据-->" + str); log.info("获取redis中的数据-->{}", value);
return Result.ok(value);
} }
} }
package com.kwan.springbootkwan.entity;
import com.kwan.springbootkwan.constant.CommonConstant;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
/**
* 接口返回数据格式
*
* @author scott
* @email jeecgos@163.com
* @date 2019年1月19日
*/
@Data
@ApiModel(value = "接口返回对象", description = "接口返回对象")
public class Result<T> implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 成功标志
*/
@ApiModelProperty(value = "成功标志")
private boolean success = true;
/**
* 返回处理消息
*/
@ApiModelProperty(value = "返回处理消息")
private String message = "操作成功!";
/**
* 返回代码
*/
@ApiModelProperty(value = "返回代码")
private Integer code = 0;
/**
* 返回数据对象 data
*/
@ApiModelProperty(value = "返回数据对象")
private T result;
/**
* 时间戳
*/
@ApiModelProperty(value = "时间戳")
private long timestamp = System.currentTimeMillis();
public Result() {
}
public Result<T> success(String message) {
this.message = message;
this.code = CommonConstant.SC_OK_200;
this.success = true;
return this;
}
public Result<T> good(T t) {
this.setResult(t);
this.code = CommonConstant.SC_OK_200;
this.success = true;
return this;
}
public Result<T> good() {
this.code = CommonConstant.SC_OK_200;
this.success = true;
this.setMessage("成功");
return this;
}
public Result<T> fail(String msg) {
this.setCode(CommonConstant.SC_INTERNAL_SERVER_ERROR_500);
this.setMessage(msg);
this.setSuccess(false);
return this;
}
public static Result<Object> ok() {
Result<Object> r = new Result<>();
r.setSuccess(true);
r.setCode(CommonConstant.SC_OK_200);
r.setMessage("成功");
return r;
}
public static Result<Object> ok(Object data) {
Result<Object> r = new Result<>();
r.setSuccess(true);
r.setCode(CommonConstant.SC_OK_200);
r.setResult(data);
return r;
}
public static Result<Object> error(String msg) {
return error(CommonConstant.SC_INTERNAL_SERVER_ERROR_500, msg);
}
public static Result<Object> error(int code, String msg) {
Result<Object> r = new Result<Object>();
r.setCode(code);
r.setMessage(msg);
r.setSuccess(false);
return r;
}
public Result<T> error500(String message) {
this.message = message;
this.code = CommonConstant.SC_INTERNAL_SERVER_ERROR_500;
this.success = false;
return this;
}
/**
* 无权限访问返回结果
*/
public static Result<Object> noauth(String msg) {
return error(CommonConstant.SC_JEECG_NO_AUTHZ, msg);
}
}
\ No newline at end of file
server: server:
port: 8761 port: 8888
servlet: servlet:
encoding: encoding:
force: true force: true
...@@ -38,7 +38,7 @@ spring: ...@@ -38,7 +38,7 @@ spring:
database: 0 # Redis数据库索引(默认为0) database: 0 # Redis数据库索引(默认为0)
host: 120.79.36.53 #Redis服务器地址 host: 120.79.36.53 #Redis服务器地址
port: 6379 # Redis服务器连接端口 port: 6379 # Redis服务器连接端口
password: # Redis服务器连接密码(默认为空) password: 123456 # Redis服务器连接密码(默认为空)
jedis: jedis:
pool: pool:
max-active: 200 # 连接池最大连接数(使用负值表示没有限制) max-active: 200 # 连接池最大连接数(使用负值表示没有限制)
...@@ -66,15 +66,15 @@ spring: ...@@ -66,15 +66,15 @@ spring:
password: 15671628341Qwe. password: 15671628341Qwe.
seata: #seata:
application-id: spring-boot-name # application-id: spring-boot-name
tx-service-group: my-tx-group # tx-service-group: my-tx-group
service: # service:
vgroup-mapping: # vgroup-mapping:
my-tx-group: seata-server # my-tx-group: seata-server
grouplist: # grouplist:
seata-server: 127.0.0.1:8091 # seata-server: 127.0.0.1:8091
enabled: true # enabled: true
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册