💗wei_shuo的个人主页
💫wei_shuo的学习社区
🌐Hello World !
ExcelUtils
public class ExcelUtils {
/**
* 注入的具有排序功能的handle
*/
private static final SortRowWriteHandler SORT_ROW_WRITE_HANDLER = new SortRowWriteHandler();
/**
* 导出excel-指定excel表头列
*
* @param list 列表数据
* @param clazz 列表项类
* @param fileName 文件名称
* @param response 响应
* @param <T> 类
*/
public static <T> void exportExcelInclude(List<T> list, Class<T> clazz, String fileName, HttpServletResponse response, List<String> includeTitleList) throws IOException {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String exportFileName = createFileName(fileName);
exportFileName = URLEncoder.encode(exportFileName, "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + exportFileName + ".xlsx");
response.setHeader("filename", exportFileName + ".xlsx");
response.setHeader("Access-Control-Expose-Headers", "filename");
HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(ExcelStyleUtils.getHeadStyle(), ExcelStyleUtils.getContentStyle());
ExcelCellWidthStyleStrategy widthStyleStrategy = new ExcelCellWidthStyleStrategy();
ExcelWriterSheetBuilder excelWriterSheetBuilder = EasyExcel.write(response.getOutputStream(), clazz)
.useDefaultStyle(Boolean.TRUE)
.registerWriteHandler(horizontalCellStyleStrategy)
.registerWriteHandler(widthStyleStrategy)
.sheet(fileName);
if (!CollectionUtils.isEmpty(includeTitleList)) {
//存在包含的列时使用排序,将导出的文件按照指定的列进行排序
excelWriterSheetBuilder.includeColumnFieldNames(includeTitleList)
//注入的具有排序功能的handle
.registerWriteHandler(SORT_ROW_WRITE_HANDLER)
.registerWriteHandler(horizontalCellStyleStrategy)
.registerWriteHandler(widthStyleStrategy);
}
excelWriterSheetBuilder.doWrite(list);
}
public static String createFileName(String type) {
return type + "-" + LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
}
}
RdisUtils
/**
* Redis工具类
*/
@SuppressWarnings({"unused", "UnusedReturnValue", "ConstantConditions", "JavaDoc", "unchecked"})
@Slf4j
public class RedisOps {
private final RedisTemplate<String, Object> redisTemplate;
private final StringRedisSerializer stringRedisSerializer;
public RedisOps(RedisTemplate<String, Object> redisTemplate, StringRedisSerializer stringRedisSerializer) {
this.redisTemplate = redisTemplate;
this.stringRedisSerializer = stringRedisSerializer;
}
public Set<String> keys() {
return redisTemplate.keys("*");
}
public Set<String> keys(String pattern) {
return redisTemplate.keys(pattern);
}
public void hset(Collection<RedisHash> list) {
redisTemplate.executePipelined(
(RedisCallback<List<Object>>) connection -> {
list.forEach(hash -> connection.hashCommands().hSet(
Objects.requireNonNull(stringRedisSerializer.serialize(hash.getKey())),
Objects.requireNonNull(stringRedisSerializer.serialize(hash.getField())),
Objects.requireNonNull(stringRedisSerializer.serialize(hash.getValue()))
));
return null;
}
);
}
public void hset(String key, Map<String, String> map) {
redisTemplate.executePipelined(
(RedisCallback<List<Object>>) connection -> {
map.forEach((field, value) -> connection.hashCommands().hSet(
Objects.requireNonNull(stringRedisSerializer.serialize(key)),
Objects.requireNonNull(stringRedisSerializer.serialize(field)),
Objects.requireNonNull(stringRedisSerializer.serialize(value))
));
return null;
}
);
}
/**
* 指定缓存失效时间
*
* @param key 键
* @param time 时间(秒)
* @return boolean
*/
public boolean expire(String key, long time) {
try {
if (time > 0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 根据key 获取过期时间
*
* @param key 键 不能为null
* @return 时间(秒) 返回0代表为永久有效
*/
public long getExpire(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
/**
* 判断key是否存在
*
* @param key 键
* @return true 存在 false不存在
*/
public boolean hasKey(String key) {
try {
return redisTemplate.hasKey(key);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 根据 key 删除,适用于 Hash
*
* @param key 可以传一个值 或多个
*/
// public void del(String... key) {
// if (key != null && key.length > 0) {
// if (key.length == 1) {
// redisTemplate.delete(key[0]);
// } else {
// redisTemplate.delete(CollectionUtils.arrayToList(key));
// }
// }
// }
/**
* 普通缓存获取
*
* @param key 键
* @return 值
*/
public Object get(String key) {
Object object;
try {
object = key == null ? null : redisTemplate.opsForValue().get(key);
} catch (Exception e) {
log.error("failed to get redis: {}", key);
throw e;
}
return object;
}
/**
* 普通缓存放入
*
* @param key 键
* @param value 值
* @return true成功 false失败
*/
public boolean set(String key, Object value) {
try {
redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 普通缓存放入并设置时间
*
* @param key 键
* @param value 值
* @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期
* @return true成功 false 失败
*/
public boolean set(String key, Object value, long time) {
try {
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
set(key, value);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 递增
*
* @param key 键
* @param delta 要增加几(大于0)
* @return
*/
public long incr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递增因子必须大于0");
}
return redisTemplate.opsForValue().increment(key, delta);
}
/**
* 递减
*
* @param key 键
* @param delta 要减少几(小于0)
* @return
*/
public long decr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递减因子必须大于0");
}
return redisTemplate.opsForValue().increment(key, -delta);
}
/**
* HashGet
*
* @param key 键 不能为null
* @param item 项 不能为null
* @return 值
*/
public Object hget(String key, String item) {
Assert.notNull(key, "key 不能为 null");
Assert.notNull(item, "item 不能为 null");
return redisTemplate.opsForHash().get(key, item);
}
/**
* HashGet
*
* @param key 键 不能为null
* @param item 项 不能为null
* @return 值
*/
public <T> T hgetJson(String key, String item, Class<T> tClass) {
Object obj = hget(key, item);
if (obj == null) return null;
return JsonMapper.jsonToObject((String) obj, tClass);
}
public List<Object> multiGet(String key, List<String> hashKeys) {
Assert.notNull(key, "key 不能为 null");
Assert.notNull(hashKeys, "hashKeys 不能为 null");
return redisTemplate.opsForHash().multiGet(key, new ArrayList<>(hashKeys));
}
public <T> List<T> multiGetJson(String key, List<String> hashKeys, Class<T> tClass) {
Object obj = multiGet(key, hashKeys);
if (obj == null) return null;
return JsonMapper.jsonToList((String) obj, tClass);
}
/**
* 获取hashKey对应的所有键值
*
* @param key 键
* @return 对应的多个键值
*/
public Map<Object, Object> hmGet(String key) {
return redisTemplate.opsForHash().entries(key);
}
/**
* HashSet
*
* @param key 键
* @param map 对应多个键值
* @return true 成功 false 失败
*/
public boolean hmSet(String key, Map<String, Object> map) {
try {
redisTemplate.opsForHash().putAll(key, map);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* HashSet 并设置时间
*
* @param key 键
* @param map 对应多个键值
* @param time 时间(秒)
* @return true成功 false失败
*/
public boolean hmSet(String key, Map<String, Object> map, long time) {
try {
redisTemplate.opsForHash().putAll(key, map);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 向一张hash表中放入数据,如果不存在将创建
*
* @param key 键
* @param item 项
* @param value 值
* @return true 成功 false失败
*/
public boolean hset(String key, String item, Object value) {
try {
redisTemplate.opsForHash().put(key, item, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 向一张hash表中放入数据,如果不存在将创建
*
* @param key 键
* @param item 项
* @param value 值
* @param time 时间(秒) 注意:如果already exist的hash表有时间,这里将会替换原有的时间
* @return true 成功 false失败
*/
public boolean hset(String key, String item, Object value, long time) {
try {
redisTemplate.opsForHash().put(key, item, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 删除hash表中的值
*
* @param key 键 不能为null
* @param item 项 可以使多个 不能为null
*/
public void hdel(String key, Object... item) {
redisTemplate.opsForHash().delete(key, item);
}
/**
* 判断hash表中是否有该项的值
*
* @param key 键 不能为null
* @param item 项 不能为null
* @return true 存在 false不存在
*/
public boolean hHasKey(String key, String item) {
return redisTemplate.opsForHash().hasKey(key, item);
}
/**
* hash递增 如果不存在,就会创建一个 并把新增后的值返回
*
* @param key 键
* @param item 项
* @param by 要增加几(大于0)
* @return
*/
public double hIncr(String key, String item, double by) {
return redisTemplate.opsForHash().increment(key, item, by);
}
/**
* hash递减
*
* @param key 键
* @param item 项
* @param by 要减少记(小于0)
* @return
*/
public double hDecr(String key, String item, double by) {
return redisTemplate.opsForHash().increment(key, item, -by);
}
// ============================set=============================
/**
* 根据key获取Set中的所有值
*
* @param key 键
* @return
*/
public Set<Object> sGet(String key) {
try {
return redisTemplate.opsForSet().members(key);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 根据value从一个set中查询,是否存在
*
* @param key 键
* @param value 值
* @return true 存在 false不存在
*/
public boolean sHasKey(String key, Object value) {
try {
return redisTemplate.opsForSet().isMember(key, value);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 将数据放入set缓存
*
* @param key 键
* @param values 值 可以是多个
* @return 成功个数
*/
public long sSet(String key, Object... values) {
try {
return redisTemplate.opsForSet().add(key, values);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 将set数据放入缓存
*
* @param key 键
* @param time 时间(秒)
* @param values 值 可以是多个
* @return 成功个数
*/
public long sSetAndTime(String key, long time, Object... values) {
try {
Long count = redisTemplate.opsForSet().add(key, values);
if (time > 0)
expire(key, time);
return count;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 获取set缓存的长度
*
* @param key 键
* @return
*/
public long sGetSetSize(String key) {
try {
return redisTemplate.opsForSet().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 移除值为value的
*
* @param key 键
* @param values 值 可以是多个
* @return 移除的个数
*/
public long setRemove(String key, Object... values) {
try {
return redisTemplate.opsForSet().remove(key, values);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
// ===============================list=================================
/**
* 获取list缓存的内容
*
* @param key 键
* @param start 开始
* @param end 结束 0 到 -1代表所有值
* @return
*/
public List<Object> lGet(String key, long start, long end) {
try {
return redisTemplate.opsForList().range(key, start, end);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public List<String> lGetAll(String key) {
try {
List<Object> list = lGet(key, 0, -1);
return list.stream().map(Object::toString).collect(Collectors.toList());
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 获取list缓存的长度
*
* @param key 键
* @return
*/
public long lGetListSize(String key) {
try {
return redisTemplate.opsForList().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 通过索引 获取list中的值
*
* @param key 键
* @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
* @return
*/
public Object lGetIndex(String key, long index) {
try {
return redisTemplate.opsForList().index(key, index);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 将list放入缓存
*
* @param key 键
* @param value 值
* @return
*/
public void lSet(String key, String value) {
redisTemplate.opsForList().rightPush(key, value);
}
/**
* 将list放入缓存
*
* @param key 键
* @param value 值
* @param time 时间(秒)
* @return
*/
public void lSet(String key, Object value, long time) {
redisTemplate.opsForList().rightPush(key, value);
if (time > 0)
expire(key, time);
}
/**
* 将list放入缓存
*
* @param key 键
* @param value 值
* @return
*/
public void lSetList(String key, Collection<String> value) {
redisTemplate.opsForList().rightPushAll(key, value.toArray());
}
/**
* 将list放入缓存
*
* @param key 键
* @param value 值
* @param time 时间(秒)
* @return
*/
public void lSetList(String key, Collection<String> value, long time) {
redisTemplate.opsForList().rightPushAll(key, value);
if (time > 0)
expire(key, time);
}
/**
* 根据索引修改list中的某条数据
*
* @param key 键
* @param index 索引
* @param value 值
* @return
*/
public boolean lUpdateIndex(String key, long index, Object value) {
try {
redisTemplate.opsForList().set(key, index, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 移除N个值为value
*
* @param key 键
* @param count 移除多少个
* @param value 值
* @return 移除的个数
*/
public long lRemove(String key, long count, Object value) {
try {
return redisTemplate.opsForList().remove(key, count, value);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
public void delete(String key) {
redisTemplate.delete(key);
}
public void delete(Collection<String> keys) {
redisTemplate.delete(keys);
}
public Set<String> hkeys(String key) {
return redisTemplate.opsForHash().keys(key).stream()
.map(Object::toString)
.collect(Collectors.toSet());
}
}
EmailUtil
@Component
public class EmailUtil {
@Resource
private JavaMailSender javaMailSender;
@Resource
private MailProperties mailProperties;
public void sendSimpleMail( String to, String subject, String context) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(mailProperties.getUsername());
message.setTo(to);
message.setSubject(subject);
message.setText(context);
javaMailSender.send(message);
}
public void sendMimeMessage(String to, String subject, String text) throws MessagingException {
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(mailProperties.getUsername());
helper.setTo(to);
helper.setSubject(subject);
helper.setText(text, true);
javaMailSender.send(message);
}
}
🌼 结语:创作不易,如果觉得博主的文章赏心悦目,还请——
点赞
👍收藏
⭐️评论
📝