首先加入依赖
org.springframework.boot spring-boot-starter-data-redisorg.apache.commons commons-pool22.6.0
在配置文件中加入redis配置
这里要注意如果redis设置了密码要加上
spring.redis.password=*****
*****代表你redis设置的密码
如果链接不上
有可能是因为你的redis没开启
开启redis
输入命令查看开启redis没有
ps -ef | grep redis
如果还链接不上redis 可能是防火墙原因
输入命令关闭防火墙
systemctl stop firewalld
如果仍然链接不上
可能是redis端口未开放
去宝塔页面开放,这里就不做演示了
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss spring.jackson.time-zone=GMT+8 spring.redis.host=8.130.169.21 spring.redis.port=6379 spring.redis.database= 0 spring.redis.timeout=1800000 spring.redis.password=***** spring.redis.lettuce.pool.max-active=20 spring.redis.lettuce.pool.max-wait=-1 #最大阻塞等待时间(负数表示没限制) spring.redis.lettuce.pool.max-idle=5 spring.redis.lettuce.pool.min-idle=0
在config包中中加入redis的配置
package com.atguigu.yygh.common.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
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.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.lang.reflect.Method;
import java.time.Duration;
@Configuration
@EnableCaching
public class RedisConfig {
@Bean
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append(method.getName());
for (Object obj : params) {
sb.append(obj.toString());
}
return sb.toString();
}
};
}
@Bean
public RedisTemplate
在service层中配置要缓存的数据
package com.atguigu.yygh.cmn.service.impl; import com.alibaba.excel.EasyExcel; import com.atguigu.yygh.cmn.listener.DictListener; import com.atguigu.yygh.cmn.mapper.DictMapper; import com.atguigu.yygh.cmn.service.DictService; import com.atguigu.yygh.model.cmn.Dict; import com.atguigu.yygh.vo.cmn.DictEeVo; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.beans.BeanUtils; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; @Component public class DictServiceImpl extends ServiceImplimplements DictService { @CacheEvict(value = "dict", allEntries=true) @Override public void importData(MultipartFile file) { try { EasyExcel.read(file.getInputStream(),DictEeVo.class,new DictListener(baseMapper)).sheet().doRead(); } catch (IOException e) { e.printStackTrace(); } } @Cacheable(value = "dict",keyGenerator = "keyGenerator") @Override public List findChildData(Long id) { QueryWrapper wrapper=new QueryWrapper<>(); wrapper.eq("parent_id",id); List dictList = baseMapper.selectList(wrapper); for (Dict dict : dictList) { Long dictId = dict.getId(); boolean isChild = this.isChild(dictId); dict.setHasChildren(isChild); } return dictList; } @Override public void exportData(HttpServletResponse response) { try { response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系 String fileName = URLEncoder.encode("数据字典", "UTF-8"); response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx"); List dictList = baseMapper.selectList(null); List dictVoList = new ArrayList<>(dictList.size()); for(Dict dict : dictList) { DictEeVo dictVo = new DictEeVo(); BeanUtils.copyProperties(dict, dictVo, DictEeVo.class); dictVoList.add(dictVo); } EasyExcel.write(response.getOutputStream(), DictEeVo.class).sheet("数据字典").doWrite(dictVoList); } catch (IOException e) { e.printStackTrace(); } } //判断ID下面是否有子节点 public boolean isChild(Long id){ QueryWrapper wrapper=new QueryWrapper<>(); wrapper.eq("parent_id",id); Integer count = baseMapper.selectCount(wrapper); return count>0; } }



