InitDataConfig.java
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.lang.Dict; import com.ebc.db.MyDb; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.web.context.ServletContextAware; import javax.servlet.ServletContext; import java.math.BigDecimal; import java.sql.SQLException; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @Configuration @Slf4j public class InitDataConfig implements InitializingBean, ServletContextAware { /** * 全局每月应上小时数 */ public static Map<String, BigDecimal> MONTH_SHOULD_HOURS_MAP = new ConcurrentHashMap<String, BigDecimal>(); /** * 月份列表,用于页面供用户选择 */ public static List<String> MONTH_LIST = CollUtil.newArrayList(); @Autowired private MyDb myDb; @Override public void setServletContext(ServletContext servletContext) { List<Dict> dictList = null; try { dictList = myDb.selectWorkinghoursAllList(); } catch (SQLException e) { e.printStackTrace(); } if (dictList != null && dictList.size()>0) { Dict d = null; for (int i = 0; i < dictList.size(); i++) { d = dictList.get(i); MONTH_SHOULD_HOURS_MAP.put(d.getStr("month_date"),d.getBigDecimal("month_should_hours")); MONTH_LIST.add(d.getStr("month_date")); } log.info("初始化每月应上小时数到本地map完成"); } else { log.error("每月应上小时数表数据是空的,请先初始化数据!"); } } @Override public void afterPropertiesSet() throws Exception { } }
调用:
@RequestMapping("/") public String index(ModelMap modelMap) { modelMap.addAttribute("MONTH_LIST", InitDataConfig.MONTH_LIST); return "index"; }