spring boot项目初始化,字典加载到内存中

1.项目初始化把常用的字典存到到内存中,
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class CacheUtils {
//存储字典code和name的值
public static Map facilityTypeMap = new HashMap();

        @Autowired
        private FacilityTypeMapper facilityTypeMapper;       

@PostConstruct
public void init() {
    //查询字典的数据
    List facilityTypeDOS = facilityTypeMapper.selectList(null);
    //封装到map里
    facilityTypeDOS.forEach(item->{
        facilityTypeMap.put(item.getCode(),item.getName());
    });
    }

}
2.调用
public static void main(String[] args) {
//根据code获取对应的name
CacheUtils.facilityTypeMap.get(“字典code”);
}

你可能感兴趣的:(spring,boot,java)