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

简述: 启动项目后, 加载数据库公共配置数据到redis中 [已集成: springboot + mybatis + 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;
    }
    }   */

注:  上述代码放到自己项目时候, 可以根据项目情况进行修改

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值