springboot 启动加载数据库数据到redis缓存

启动项目后, 加载数据库公共配置数据到redis中

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
 
/**
 *  启动项目后, 加载数据库公共配置数据到redis中
 * 
 */
@Component
public class PubConfigUtil {
 
    private static String pre = "pub_config"; // 存储到redis中的前缀
    private static String operation = ":"; // redis冒号分组
 
    @Resource
    private JdbcTemplate jdbcTemplate; // springboot集成mybatis-spring-boot-starter后本身封装的工具类
 
    @Resource
    private RedisTemplate redisTemplate;
 
    @PostConstruct // 是java注解,在方法上加该注解会在项目启动的时候执行该方法,也可以理解为在spring容器初始化的时候执行该方法。
    public void reload() {
        String sql = "SELECT config_id,config_block,config_name,config_value,config_desc FROM ad_basic_config WHERE data_status = '1'"; // 数据库配置表,根据你自己的配置表定义查询语句
        List<Map<String, Object>> mapsList = jdbcTemplate.queryForList(sql);
        if (!CollectionUtils.isEmpty(mapsList)) { // 存在值
            StringBuilder sbl = new StringBuilder();
            for (Map<String, Object> map : mapsList) {
                sbl.setLength(0);
                String config_block = map.get("config_block").toString(); // 数据库配置表中的字段名称| 模块: aliyun
                String config_name = map.get("config_name").toString(); // 数据库配置表中的字段名称| 配置名: SMS_TEMPLATECODE_LOGGIN (阿里云下短信模板号 - 登录)
                String config_value = map.get("config_value").toString(); // 数据库配置表中的字段名称| 配置具体值: xxxxx (阿里云下短信模板号)
                String key = sbl.append(pre).append(operation).append(config_block).append(operation).append(config_name).toString();
                redisTemplate.opsForValue().set(key, config_value);
            }
        }
    }
 
    /*    // 获取启动后加载的配置数据 - 放到自己项目的redis工具类中
       public Integer getInt(String configBlock,String configName, Integer defaultVal) {
        String string = getString(configBlock,configName, null);
        Integer value = defaultVal;
        try {
            value = Integer.valueOf(string);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }
    public Long getLong(String configBlock,String configName, Long defaultVal) {
        String string = getString(configBlock,configName, null);
        Long value = defaultVal;
        try {
            value = Long.valueOf(string);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }
    public Double getDouble(String configBlock,String configName, Double defaultVal) {
        String string = getString(configBlock,configName, null);
        Double value = defaultVal;
        try {
            value = Double.valueOf(string);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }
    public Boolean getBoolean(String configBlock,String configName, Boolean defaultVal) {
        String string = getString(configBlock,configName, null);
        Boolean value = defaultVal;
        try {
            value = Boolean.valueOf(string);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }
    }   */

可以根据项目情况进行修改

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

讓丄帝愛伱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值