0
点赞
收藏
分享

微信扫一扫

SpringBoot项目启动的时候执行一次数据库查询的功能

Separes 2022-04-26 阅读 51
java

实现CommandLineRunner接口,重写run方法

package com.example.qw_goods.utils;

import com.example.qw_common.model.GoodsVO;
import com.example.qw_goods.service.GoodsService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author qw
 * @Description
 * @date 25/4/2022
 */
@Component
public class RunnerLoadOne implements CommandLineRunner {
     static Map<String,Object> urlMap = new HashMap<>();

    @Resource
    private GoodsService goodsService ;
    @Override
    public void run(String... args) throws Exception {
        List<GoodsVO> goodsList = goodsService.getGoodsList();
        Optional.ofNullable(goodsList).ifPresent(list->{
            urlMap.put("url",list.get(0).getCode());
            urlMap.put("soapUrl",list.get(0).getName());
        });
    }

    public static Map<String,Object> getInfo(){
        return urlMap;
    }

}

举报

相关推荐

0 条评论