Springboot项目启动时加载数据库数据到内存

简介: Springboot项目启动时加载数据库数据到内存


  1. 使用@PostConstruct注解
@Component
public class CodeCache {
  public static Map<String, String> codeMap = new HashMap<String, String>();
  @Autowired
  private ICodeService codeService;
  @PostConstruct
  public void init(){
    System.out.println("系统启动中。。。加载codeMap");
    List<Code> codeList = codeService.selectAll();
    for (Code code : codeList) {
      codeMap.put(code.getKey(), code.getValue());
    }
  }
  @PreDestroy
    public void destroy(){
        System.out.println("系统运行结束");
    }
}
  1. 获取缓存值
System.out.println(CodeCache.codeMap.get("name"));


相关文章
|
6天前
|
XML Java 关系型数据库
Action:Consider the following: If you want an embedde ,springBoot配置数据库,补全springBoot的xml和mysql配置信息就好了
Action:Consider the following: If you want an embedde ,springBoot配置数据库,补全springBoot的xml和mysql配置信息就好了
|
3天前
|
前端开发 JavaScript NoSQL
软件开发常见流程之若依项目修改,前后端本地启动,本地必须有Redis+Navicate启动数据库
软件开发常见流程之若依项目修改,前后端本地启动,本地必须有Redis+Navicate启动数据库
|
7天前
|
NoSQL Java MongoDB
如何在Spring Boot应用中集成MongoDB数据库
如何在Spring Boot应用中集成MongoDB数据库
|
7天前
|
Java 关系型数据库 MySQL
Spring Boot中集成MySQL数据库的步骤和技巧
Spring Boot中集成MySQL数据库的步骤和技巧
|
7天前
|
Java 数据库连接 数据库
Spring Boot中配置Liquibase进行数据库管理
Spring Boot中配置Liquibase进行数据库管理
|
10天前
|
Java 数据库连接 数据库
Spring Boot中如何使用Flyway进行数据库版本控制
Spring Boot中如何使用Flyway进行数据库版本控制
|
10天前
|
SQL Java 数据库
使用Spring Boot和Spring Data JPA进行数据库操作
使用Spring Boot和Spring Data JPA进行数据库操作
|
2天前
|
存储 关系型数据库 MySQL
探索MySQL:关系型数据库的基石
MySQL,作为全球最流行的开源关系型数据库管理系统(RDBMS)之一,广泛应用于各种Web应用、企业级应用和数据仓库中
|
5天前
|
关系型数据库 MySQL 数据库
关系型数据库mysql数据增量恢复
【7月更文挑战第3天】
16 2
|
5天前
|
关系型数据库 MySQL Shell
关系型数据库mysql数据完全恢复
【7月更文挑战第3天】
15 2