我是春季新手用户,请帮助我。这是我的要求:
也许数据库结构像:
想知道是否有人可以指导我。非常感谢!
您可以使用创建一些Util
类(在@PostConstruct
上)来从DB加载您的作业配置。
例如:
@Entity
public class Configuration{
@Id
private long id;
private String field;
private String value;
// getter and setter
}
@Component
public interface ConfigurationRepo extends JpaRepository<Configuration, Long> {
}
public final class ConfigurationUtil {
private ConfigurationUtil() {
}
private static List<Configuration> defaultConfiguration;
/**
* @return the defaultConfiguration
*/
public static List<Configuration> getDefaultConfiguration() {
return defaultConfiguration;
}
/**
* @param defaultConfiguration the defaultConfiguration to set
*/
public static void setDefaultConfiguration(List<Configuration> defaultConfiguration) {
ConfigurationUtil.defaultConfiguration = defaultConfiguration;
}
public static String getValueByField(String field) {
return defaultConfiguration.stream()
.filter(s -> s.getField()
.equalsIgnoreCase(field))
.findFirst()
.get()
.getValue();
}
}
@Component
public class ConfigurationContextInitializer {
@Autowired
ConfigurationRepo configurationRepo;
@PostConstruct
public void init() {
ConfigurationUtil.setDefaultConfiguration(configurationRepo.findAll());
}
}
//To access DB value
ConfigurationUtil.getValueByField("JOB_NAME"); // depends on your DB key