继续在此前SpringBoot - 使用MyBatis操作数据库项目上做延伸扩展
1、pom文件引入redis架包
<!-- 引入 redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<!-- commons lang3-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
2、application.properties文件配置redis信息
服务器搭建redis参考https://blog.csdn.net/weixin_40009737/article/details/111880742
#redis配置
spring.redis.host=服务器IP地址
spring.redis.port=6379
spring.redis.password=123456
#最大连接数
spring.redis.jedis.pool.max-active=8
#最大阻塞等待时间
spring.redis.jedis.pool.max-wait=6000
#最小空闲连接
spring.redis.jedis.pool.min-idle=0
#最大空闲连接
spring.redis.jedis.pool.max-idle=8
spring.redis.database=0
3.RedisConfiguration.java
实例化RedisTemplate
package com.example.demo.common.util;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
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;
@Configuration
public class RedisConfiguration {
@Bean
@SuppressWarnings("all")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(factory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
template.setKeySerializer(stringRedisSerializer);
template.setHashKeySerializer(stringRedisSerializer);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
4、编写RedisUtil工具类(便于开发使用工具类进行开发提高效率,更加方便快捷)
package com.example.demo.common.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* CreateTime: 2019-08-01 18:14
* ClassName: RedisUtil
* Package: com.example.api.utils
* Describe:Redis工具类
*
* @author XieZhiXin
*/
@Component
public class RedisUtil {
private static final Long SUCCESS = 1L;
@Autowired
private RedisTemplate redisTemplate;
//---------------------- common --------------------------
/**
* 指定缓存失效时间
*
* @param key key值
* @param time 缓存时间
*/
public void expire(String key, long time) {
if (time > 0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
} else {
throw MyExceptionUtils.mxe("设置的时间不能为0或者小于0!!");
}
}
/**
* 判断key是否存在
*
* @param key 传入ke值
* @return true 存在 false 不存在
*/
public Boolean existsKey(String key) {
return redisTemplate.hasKey(key);
}
/**
* 判断key存储的值类型
*
* @param key key值
* @return DataType[string、list、set、zset、hash]
*/
public DataType typeKey(String key) {
return redisTemplate.type(key);
}
/**
* 删除指定的一个数据
*
* @param key key值
* @return true 删除成功,否则返回异常信息
*/
public Boolean deleteKey(String key) {
try {
redisTemplate.delete(key);
return true;
} catch (Exception ex) {
throw MyExceptionUtils.mxe("删除失败!", ex);
}
}
/**
* 删除多个数据
*
* @param keys key的集合
* @return true删除成功,false删除失败
*/
public Boolean deleteKey(Collection<String> keys) {
try {
redisTemplate.delete(keys);
return true;
} catch (Exception ex) {
throw MyExceptionUtils.mxe("删除失败!", ex);
}
}
//-------------------- String ----------------------------
/**
* 普通缓存放入
*
* @param key 键值
* @param value 值
* @return true成功 要么异常
*/
public Boolean setString(String key, Object value) {
try {
redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception ex) {
throw MyExceptionUtils.mxe("插入缓存失败!", ex);
}
}
/**
* 普通缓存获取
*
* @param key 键
* @return 值
*/
public Object getString(String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}
/**
* 设置缓存存在时间
*
* @param key key值
* @param value value值
* @param time 时间 秒为单位
* @return 成功返回true,失败返回异常信息
*/
public boolean setString(String key, Object value, long time) {
try {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
return true;
} catch (Exception ex) {
throw MyExceptionUtils.mxe("插入缓存失败!", ex);
}
}
//-----------------------------hash----------------------------------
/**
* 设置hash值,并设置过期时间
*
* @param key
* @param hk
* @param hv
* @param time
* @return
*/
public Boolean setHash(String key, Object hk, Object hv, long time) {
redisTemplate.opsForHash().put(key, hk, hv);
redisTemplate.expire(key, time, TimeUnit.SECONDS);
return true;
}
public Boolean setHash(String key, Map map, long time) {
redisTemplate.opsForHash().putAll(key, map);
redisTemplate.expire(key, time, TimeUnit.SECONDS);
return true;
}
/**
* 获取hash的值
*
* @param key
* @param hk
* @return
*/
public Object getHash(String key, String hk) {
return key == null ? null : (hk == null ? null : redisTemplate.opsForHash().get(key, hk));
}
/**
* hash累加
*/
public Long hincrease(String key, String hk, long l) {
return redisTemplate.opsForHash().increment(key, hk, l);
}
//----------------------------- list ------------------------------
/**
* 将list放入缓存
*
* @param key key的值
* @param value 放入缓存的数据
* @return true 代表成功,否则返回异常信息
*/
public Boolean setList(String key, Object value) {
try {
redisTemplate.opsForList().rightPush(key, value);
return true;
} catch (Exception ex) {
throw MyExceptionUtils.mxe("插入List缓存失败!", ex);
}
}
/**
* 将Object数据放入List缓存,并设置时间
*
* @param key key值
* @param value 数据的值
* @param time 缓存的时间
* @return true插入成功,否则返回异常信息
*/
public Boolean setList(String key, Object value, long time) {
try {
if (time > 0) {
redisTemplate.opsForList().rightPush(key, value);
expire(key, time);
return true;
}
return false;
} catch (Exception ex) {
throw MyExceptionUtils.mxe("插入List缓存失败!", ex);
}
}
/**
* 将list集合放入List缓存,并设置时间
*
* @param key key值
* @param value 数据的值
* @param time 缓存的时间
* @return true插入成功,否则返回异常信息
*/
public Boolean setListAll(String key, Object value, long time) {
try {
if (time > 0) {
redisTemplate.opsForList().rightPushAll(key, value);
this.expire(key, time);
return true;
}
return false;
} catch (Exception ex) {
throw MyExceptionUtils.mxe("插入List缓存失败!", ex);
}
}
/**
* 根据索引获取缓存List中的内容
*
* @param key key的值
* @param start 索引开始
* @param end 索引结束 0 到 -1代表所有值
* @return 返回数据
*/
public List<Object> getList(String key, long start, long end) {
try {
return redisTemplate.opsForList().range(key, start, end);
} catch (Exception ex) {
throw MyExceptionUtils.mxe("获取缓存List中的内容失败了!", ex);
}
}
/**
* 删除List缓存中多个list数据
*
* @param key key值
* @param count 移除多少个
* @param value 可以传null 或者传入存入的Value的值
* @return 返回删除了多少个
*/
public long deleteListIndex(String key, long count, Object value) {
try {
return redisTemplate.opsForList().remove(key, count, value);
} catch (Exception ex) {
throw MyExceptionUtils.mxe("删除List中的内容失败了!", ex);
}
}
/**
* 获取List缓存的数据
*
* @param key key值
* @return 返回长度
*/
public long getListSize(String key) {
try {
return redisTemplate.opsForList().size(key);
} catch (Exception ex) {
throw MyExceptionUtils.mxe("获取List长度失败", ex);
}
}
//----------------------set-------------------
/**
* 判断是否包含在Set中
*
* @param key
* @param o
*/
public void isContainsKey(String key, HashSet o) {
redisTemplate.opsForSet().isMember(key, o);
}
//-----------------------lock----------------------
/**
* 获取分布式锁
* @param lockKey 锁
* @param requestId 请求标识
* @param expireTime 单位秒
* @param waitTimeout 单位毫秒
* @return 是否获取成功
*/
public boolean tryLock(String lockKey, String requestId, int expireTime,long waitTimeout) {
// 当前时间
long nanoTime = System.nanoTime();
try{
String script = "if redis.call('setNx',KEYS[1],ARGV[1]) then if redis.call('get',KEYS[1])==ARGV[1] then return redis.call('expire',KEYS[1],ARGV[2]) else return 0 end end";
int count = 0;
do{
RedisScript<String> redisScript = new DefaultRedisScript<>(script, String.class);
Object result = redisTemplate.execute(redisScript, Collections.singletonList(lockKey),requestId,expireTime);
if(SUCCESS.equals(result)) {
return true;
}
//休眠500毫秒
Thread.sleep(500L);
count++;
}while ((System.nanoTime() - nanoTime) < TimeUnit.MILLISECONDS.toNanos(waitTimeout));
}catch(Exception e){
System.out.println("尝试获取分布式锁-key[{}]异常"+lockKey);
}
return false;
}
/**
* 释放锁
* @param lockKey 锁
* @param requestId 请求标识
* @return 是否释放成功
*/
public boolean releaseLock(String lockKey, String requestId) {
String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
RedisScript<String> redisScript = new DefaultRedisScript<>(script, String.class);
Object result = redisTemplate.execute(redisScript, Collections.singletonList(lockKey), requestId);
if (SUCCESS.equals(result)) {
return true;
}
return false;
}
}
5、编写异常工具类MyExceptionUtils,便于开发捕捉异常并抛出自定义异常信息,便于定位问题
package com.example.demo.common.exception;
/**
* CreateTime: 2018-12-18 22:31
* ClassName: MyException
* Package: com.ywh.common.exception
* Describe:
* 自定义异常,可以throws的时候用自己的异常类
*
* @author YWH
*/
public class MyException extends RuntimeException {
public MyException(String msg) {
super(msg);
}
public MyException(String message, Throwable throwable) {
super(message, throwable);
}
public MyException(Throwable throwable) {
super(throwable);
}
}
6.编写StringUtils字符串工具类
pom已引入lang3依赖
package com.example.demo.common.util;
import java.util.Collection;
import java.util.Map;
/**
* 字符串工具类
*
* @author XieZhiXin
*/
public class StringUtils extends org.apache.commons.lang3.StringUtils {
/**
* 空字符串
*/
private static final String NULLSTR = "";
/**
* 下划线
*/
private static final char SEPARATOR = '_';
/**
* 获取参数不为空值
*
* @param value defaultValue 要判断的value
* @return value 返回值
*/
public static <T> T nvl(T value, T defaultValue) {
return value != null ? value : defaultValue;
}
/**
* * 判断一个Collection是否为空, 包含List,Set,Queue
*
* @param coll 要判断的Collection
* @return true:为空 false:非空
*/
public static boolean isEmpty(Collection<?> coll) {
return isNull(coll) || coll.isEmpty();
}
/**
* * 判断一个Collection是否非空,包含List,Set,Queue
*
* @param coll 要判断的Collection
* @return true:非空 false:空
*/
public static boolean isNotEmpty(Collection<?> coll) {
return !isEmpty(coll);
}
/**
* * 判断一个对象数组是否为空
*
* @param objects 要判断的对象数组
* * @return true:为空 false:非空
*/
public static boolean isEmpty(Object[] objects) {
return isNull(objects) || (objects.length == 0);
}
/**
* * 判断一个对象数组是否非空
*
* @param objects 要判断的对象数组
* @return true:非空 false:空
*/
public static boolean isNotEmpty(Object[] objects) {
return !isEmpty(objects);
}
/**
* * 判断一个Map是否为空
*
* @param map 要判断的Map
* @return true:为空 false:非空
*/
public static boolean isEmpty(Map<?, ?> map) {
return isNull(map) || map.isEmpty();
}
/**
* * 判断一个Map是否为空
*
* @param map 要判断的Map
* @return true:非空 false:空
*/
public static boolean isNotEmpty(Map<?, ?> map) {
return !isEmpty(map);
}
/**
* * 判断一个字符串是否为空串
*
* @param str String
* @return true:为空 false:非空
*/
public static boolean isEmpty(String str) {
return isNull(str) || NULLSTR.equals(str.trim());
}
/**
* * 判断一个字符串是否为非空串
*
* @param str String
* @return true:非空串 false:空串
*/
public static boolean isNotEmpty(String str) {
return !isEmpty(str);
}
/**
* * 判断一个对象是否为空
*
* @param object Object
* @return true:为空 false:非空
*/
public static boolean isNull(Object object) {
return object == null;
}
/**
* * 判断一个对象是否非空
*
* @param object Object
* @return true:非空 false:空
*/
public static boolean isNotNull(Object object) {
return !isNull(object);
}
/**
* * 判断一个对象是否是数组类型(Java基本型别的数组)
*
* @param object 对象
* @return true:是数组 false:不是数组
*/
public static boolean isArray(Object object) {
return isNotNull(object) && object.getClass().isArray();
}
/**
* 去空格
*/
public static String trim(String str) {
return (str == null ? "" : str.trim());
}
/**
* 截取字符串
*
* @param str 字符串
* @param start 开始
* @return 结果
*/
public static String substring(final String str, int start) {
if (str == null) {
return NULLSTR;
}
if (start < 0) {
start = str.length() + start;
}
if (start < 0) {
start = 0;
}
if (start > str.length()) {
return NULLSTR;
}
return str.substring(start);
}
/**
* 截取字符串
*
* @param str 字符串
* @param start 开始
* @param end 结束
* @return 结果
*/
public static String substring(final String str, int start, int end) {
if (str == null) {
return NULLSTR;
}
if (end < 0) {
end = str.length() + end;
}
if (start < 0) {
start = str.length() + start;
}
if (end > str.length()) {
end = str.length();
}
if (start > end) {
return NULLSTR;
}
if (start < 0) {
start = 0;
}
if (end < 0) {
end = 0;
}
return str.substring(start, end);
}
/**
* 格式化文本, {} 表示占位符<br>
* 此方法只是简单将占位符 {} 按照顺序替换为参数<br>
* 如果想输出 {} 使用 \\转义 { 即可,如果想输出 {} 之前的 \ 使用双转义符 \\\\ 即可<br>
* 例:<br>
* 通常使用:format("this is {} for {}", "a", "b") -> this is a for b<br>
* 转义{}: format("this is \\{} for {}", "a", "b") -> this is \{} for a<br>
* 转义\: format("this is \\\\{} for {}", "a", "b") -> this is \a for b<br>
*
* @param template 文本模板,被替换的部分用 {} 表示
* @param params 参数值
* @return 格式化后的文本
*/
public static String format(String template, Object... params) {
if (isEmpty(params) || isEmpty(template)) {
return template;
}
return template;
}
/**
* 下划线转驼峰命名
*/
public static String toUnderScoreCase(String str) {
if (str == null) {
return null;
}
StringBuilder sb = new StringBuilder();
// 前置字符是否大写
boolean preCharIsUpperCase = true;
// 当前字符是否大写
boolean curreCharIsUpperCase = true;
// 下一字符是否大写
boolean nexteCharIsUpperCase = true;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (i > 0) {
preCharIsUpperCase = Character.isUpperCase(str.charAt(i - 1));
} else {
preCharIsUpperCase = false;
}
curreCharIsUpperCase = Character.isUpperCase(c);
if (i < (str.length() - 1)) {
nexteCharIsUpperCase = Character.isUpperCase(str.charAt(i + 1));
}
if (preCharIsUpperCase && curreCharIsUpperCase && !nexteCharIsUpperCase) {
sb.append(SEPARATOR);
} else if ((i != 0 && !preCharIsUpperCase) && curreCharIsUpperCase) {
sb.append(SEPARATOR);
}
sb.append(Character.toLowerCase(c));
}
return sb.toString();
}
/**
* 是否包含字符串
*
* @param str 验证字符串
* @param strs 字符串组
* @return 包含返回true
*/
public static boolean inStringIgnoreCase(String str, String... strs) {
if (str != null && strs != null) {
for (String s : strs) {
if (str.equalsIgnoreCase(trim(s))) {
return true;
}
}
}
return false;
}
/**
* 将下划线大写方式命名的字符串转换为驼峰式。如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。 例如:HELLO_WORLD->HelloWorld
*
* @param name 转换前的下划线大写方式命名的字符串
* @return 转换后的驼峰式命名的字符串
*/
public static String convertToCamelCase(String name) {
StringBuilder result = new StringBuilder();
// 快速检查
if (name == null || name.isEmpty()) {
// 没必要转换
return "";
} else if (!name.contains("_")) {
// 不含下划线,仅将首字母大写
return name.substring(0, 1).toUpperCase() + name.substring(1);
}
// 用下划线将原始字符串分割
String[] camels = name.split("_");
for (String camel : camels) {
// 跳过原始字符串中开头、结尾的下换线或双重下划线
if (camel.isEmpty()) {
continue;
}
// 首字母大写
result.append(camel.substring(0, 1).toUpperCase());
result.append(camel.substring(1).toLowerCase());
}
return result.toString();
}
}
7.修改AuthServiceImpl.java
@Autowired
private RedisUtil redisUtil;
@Override
public int addAuth(Auth auth){
int i1 = authMapper.addAuth(auth);
redisUtil.setString(auth.getId(), auth);
System.out.println("插入一条数据>>>" + i1);
return i1;
}
@Override
public int updateAuth(Auth auth){
redisUtil.deleteKey(auth.getId());
// 修改数据
int i2 = authMapper.updateAuth(auth);
redisUtil.setString(auth.getId(), auth);
System.out.println("更新一条数据>>>" + i2);
return i2;
}
@Override
public int deleteAuthById(String id){
// 删除数据
int i3 = authMapper.deleteAuthById(id);
redisUtil.deleteKey(id);
System.out.println("删除一条数据>>>" + i3);
return i3;
}
@Override
public Object getAuthById(String id){
// 查询单条数据
if(redisUtil.getString(id)==null){
Auth auth4 = authMapper.getAuthById(id);
System.out.println("查询1条数据>>>" + auth4.toString());
return auth4;
}else {
Object flag = redisUtil.getString(id);
System.out.println("redis查询1条数据>>>" + flag);
return redisUtil.getString(id);
}
}
8.启动类 添加注解@EnableCaching
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
9.运行接口
(1.)新增
http://localhost:8080/authAdd
RedisDesktopManager工具查看
(2.)修改
http://localhost:8080/deleteAuth
(3.)查询
在库里手动添加了一条数据(没有在缓存里)
http://localhost:8080/queryAuth?id=1
查看控制台
查询id=2
http://localhost:8080/queryAuth?id=2
查看控制台
(4.)删除
http://localhost:8080/deleteAuth/1