package com.qingxing.ManagerComplex.api.util;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by lcc on 2017/8/23.
*/
public class AreaMapSingleton {
public Map<String, Object> AreaMap = null;
private AreaMapSingleton() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
.getRequestAttributes()).getRequest();
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
JdbcTemplate jdbcTemplate = ctx.getBean(JdbcTemplate.class);
String sql = "select t.short_code, t.province,t.city,t.district from tb_ks_areas_all t where t.short_code like '33%' and length(t.short_code) <= '6' ";
List<Map<String, Object>> areamaplist = jdbcTemplate.queryForList(sql);
Map<String, Object> maps = new HashMap<String, Object>();
for (Map<String, Object> map : areamaplist) {
Object fullName = "";
String shortCode = (String) map.get("short_code");
if (shortCode.length() == 2) {
fullName = map.get("province");
} else if (shortCode.length() == 4) {
fullName = map.get("city");
} else if (shortCode.length() == 6) {
fullName = map.get("district");
}
maps.put(shortCode, fullName);
}
AreaMap = maps;
}
public static Map<String, Object> getAreaMap() {
return getInstance().AreaMap;
}
public static AreaMapSingleton getInstance() {
return Nested.instance;
}
static class Nested {
private static AreaMapSingleton instance = new AreaMapSingleton();
}
}
需要缓存地区放入map中,静态内部类单例