项目启动就加载信息,通过实现CommandLineRunner类,来加载
package com.xxx.customerserviceapi.common.config;
import com.xxx.base.util.CommonUtil;
import com.xxx.common.base.constant.CommonConstant;
import com.xxx.common.base.entity.Product;
import com.xxx.common.redis.RedisDao;
import com.xxx.customerserviceapi.dao.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* 初始化 信息
*/
@Component
public class InitConfigApi implements CommandLineRunner {
@Autowired
private ProductMapper productMapper;
@Autowired
private RedisDao redisDao;
@Override
public void run(String... strings) throws Exception {
//查询产品线信息,存到redis中
List<Product> list = productMapper.getList();
for(Product product:list){
redisDao.put(CommonConstant.REDIS_PRODUCTLIST, product.getSign(), CommonUtil.beanToString(product));
}
}
}