静态内部类单例缓存地区

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中,静态内部类单例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值