java - 如何在 Java 程序的开头加载一个表?
问题描述
有一个名为“Employees”的表,其中包含employeeName 和employeeId。我想在程序启动后立即将其加载到 HashMap<string,string> 中,以便其他函数可以访问它。我正在使用弹簧靴。我现在有以下文件,
应用程序.java
@SpringBootApplication
public class Application {
public static void main(String[] args){
SpringApplication.run(Application.class,args);
}
}
IEmployeeRepository.java
@Repository
public interface IEmployeeRepository extends JpaRepository {
@Query(value = "select * from Employee")
HashMap<String ,String> EmployeeReferenceTable();
}
IEmployeeRepository 将给出 HashMap。我有另一个名为 EmployeeTable.java 的文件,它应该调用 Repository 并获取整个表。
EmployeeTable.java
public class rdEmployee {
@Bean
public HashMap<String, String> EmployeeHashMap(){
// Should get the HashMap from Repository file.
}
}
除此之外,还有一些文件需要上述 Employee Table 值。怎么做?我做得对吗?使用 Bean 注释,以便在程序开始时完成。是否正确使用?IEmployeeRepository 是一个接口,那么如何从 EmployeeHashMap() 中调用呢?
解决方案
您应该添加 Class 字段的 @Component 注释,然后使用 @Bean 注释然后在那里实现您的逻辑
@Component
public class rdEmployee {
@Bean
public HashMap<String, String> EmployeeHashMap(){
// Should get the HashMap from Repository file.
}
}
推荐阅读
- ios - 关闭弹出框后如何调用函数?
- r - Should I put outcome variable in Matchit::matchit ()
- php - Unity WebGL PHP 数据库操作
- python - 将 MongoDB 与 Django 集成
- flutter - 在具有可变高度的小部件树中使用 DefaultTabController
- typescript - 如何返回链式异步函数的结果?
- python - Tensorflow v2 生成器 ValueError:未能找到可以处理输入的数据适配器:
, - java - 无法使用 java 在 Azure 存储 Blob 中检索 Blob 的元数据
- python - Telethon 异步功能到 tkinter 按钮
- python - 如何在python中自动查找列表中元素的开始和结束索引