设为首页 收藏本站
查看: 1006|回复: 0

[经验分享] 【Spring】17、spring cache 与redis缓存整合

[复制链接]

尚未签到

发表于 2017-12-22 07:36:25 | 显示全部楼层 |阅读模式
/**  *
  * 公共接口
  *
  *
@author Administrator  *
@see [相关类/方法](可选)  *
@since [产品/模块版本] (可选)*/  
@Service(
"commonService")  

public>
/**  * 日志记录器
*/  private static final Logger LOGGER = LoggerFactory.getLogger(CommonServiceImpl.class);
  

  @Autowired
  private DalClient dalClient;
  

  /*
  * @Autowired RedisTemplate<?, ?> redisTemplate;
  */
  

  /**
  * 根据名称获取自增序列squence的当前值
  *
  * @param SequenceName 自增序列名称
  * @return 自增序列当前值
  * @see [相关类/方法](可选)
  * @since [产品/模块版本](可选)
  */
  @Override
  public String getSequenceByName(String SequenceName) {
  if (StringUtils.isEmpty(SequenceName)) {
  LOGGER.error("自增序列名称为空,无法返回正常的自增序列值");
  return null;
  } else {
  Map<String, String> paramMap = new HashMap<String, String>();
  paramMap.put("sequenceName", SequenceName);
  // 查询sequence当前值
  Map<String, Object> resultMap = dalClient.queryForMap("common.GET_SEQUENCE_BY_NAME", paramMap);
  if (null != resultMap && !resultMap.isEmpty()) {
  return String.valueOf(resultMap.get("sequenceValue"));
  } else {
  return null;
  }
  }
  }
  

  /**
  * 根据上一级的城市编码 查询 所有下属城市 列表
  *
  * @param parentCityCode
  * @return
  * @see [相关类/方法](可选)
  * @since [产品/模块版本](可选)
  */
  @Override
  @Cacheable(value = "commonService.queryCityListByParentCode", key = "new String('commonService.queryCityListByParentCode')+#parentCityCode.toString()", condition = "null != #parentCityCode")
  public List<CityBean> queryCityListByParentCode(final Integer parentCityCode) {
  Map<String, Object> paramMap = new HashMap<String, Object>();
  if (null != parentCityCode) {
  // 根据所选省份 \ 城市 查询所属城市列表
  paramMap.put("parentCityCode", parentCityCode);
  

  final List<CityBean> cityListResult = dalClient.queryForList("T_CITY.SELECT_BY_PARENTCODE", paramMap,
  CityBean.class);
  return cityListResult;
  } else {
  final List<CityBean> provinceListResult = dalClient.queryForList("T_CITY.SELECT_ALL_FIRST_STEP_CITY",
  paramMap, CityBean.class);
  return provinceListResult;
  }
  }
  

  /**
  * 根据上一级的行业编码 查询 所有下属所有行业
  *
  * @param parentCityCode
  * @return
  * @see [相关类/方法](可选)
  * @since [产品/模块版本](可选)
  */
  @Override
  @Cacheable(value = "commonService.queryIndustryListByParentCode", key = "new String('commonService.queryIndustryListByParentCode')+#parentIndustryCode.toString", condition = "null != #parentIndustryCode")
  public List<IndustryBean> queryIndustryListByParentCode(final Integer parentIndustryCode) {
  Map<String, Object> paramMap = new HashMap<String, Object>();
  if (null != parentIndustryCode) {
  paramMap.put("parentIndustryCode", parentIndustryCode);
  final List<IndustryBean> industryListResult = dalClient.queryForList("T_INDUSTRY.SELECT_BY_PARENTCODE",
  paramMap, IndustryBean.class);
  return industryListResult;
  } else {
  final List<IndustryBean> industryListResult = dalClient.queryForList(
  "T_INDUSTRY.SELECT_ALL_FIRST_STEP_INDUSTRY", paramMap, IndustryBean.class);
  return industryListResult;
  }
  }
  

  /**
  * 根据行业编码查询 行业信息
  *
  * @param industryCode
  * @return
  * @see [相关类/方法](可选)
  * @since [产品/模块版本](可选)
  */
  @Override
  @Cacheable(value = "commonService.queryIndustryInfoById", key = "new String('commonService.queryIndustryInfoById')+#industryCode", condition = "(null != #industryCode) and (#industryCode.length() > 0)")
  public IndustryBean queryIndustryInfoById(final String industryCode) {
  if (StringUtils.isEmpty(industryCode)) {
  return null;
  } else {
  Map<String, Object> paramMap = new HashMap<String, Object>();
  paramMap.put("industryCode", industryCode);
  final IndustryBean industryInfoResult = dalClient.queryForObject("T_INDUSTRY.SELECT_BY_ID", paramMap,
  IndustryBean.class);
  return industryInfoResult;
  }
  }
  

  /**
  * 递归删除 元素 因为可能存在重复的
  *
  * @param list 列表
  * @param item 要删除的元素
  * @see [相关类/方法](可选)
  * @since [产品/模块版本](可选)
  */
  private void deleteListElement(ArrayList<String> list, String item) {
  if (null != list && !list.isEmpty() && StringUtils.isNotBlank(item)) {
  if (list.contains(item)) {
  list.remove(item);
  if (list.contains(item)) {
  deleteListElement(list, item);
  }
  }
  }
  }
  

  /**
  * 根据行业id查询 行业名称
  *
  * @param industryIds 行业Id可能有多个 以分号分隔
  * @return 行业名称列表
  * @see [相关类/方法](可选)
  * @since [产品/模块版本](可选)
  */
  @Override
  @Cacheable(value = "commonService.queryIndustryNameByIds", key = "new String('commonService.queryIndustryNameByIds')+#industryIds", condition = "null != #industryIds and #industryIds.length() > 0")
  public List<String> queryIndustryNameByIds(final String industryIds) {
  if (StringUtils.isBlank(industryIds)) {
  return null;
  } else {
  String[] industryIdArr = industryIds.split(";");
  if (null != industryIdArr && industryIdArr.length > 0) {
  ArrayList<String> paramList = new ArrayList<String>();
  paramList.addAll(Arrays.asList(industryIdArr));
  if (null != paramList && !paramList.isEmpty()) {
  Map<String, Object> paramMap = new HashMap<String, Object>();
  paramMap.put("industryIdList", paramList);
  // 查询行业列表
  List<IndustryBean> queryResultList = dalClient.queryForList("T_INDUSTRY.SELECT_BY_ID_LIST",
  paramMap, IndustryBean.class);
  // 封装查询结果
  List<String> industryNameList = new ArrayList<String>();
  if (null != queryResultList && !queryResultList.isEmpty()) {
  // 遍历查询列表 将已经存在的编码去掉 剩下的 就是 根據编码查询不出行业的 直接将行业的名字返回
  
                        String tempId;
  for (IndustryBean industryInfo : queryResultList) {
  if (null != industryInfo) {
  if (null == industryInfo.getIndustryCode()) {
  continue;
  } else {
  tempId = industryInfo.getIndustryCode().toString();
  if (paramList.contains(tempId)) {
  deleteListElement(paramList, tempId);
  }
  if (StringUtils.isNotBlank(industryInfo.getIndustryName())) {
  industryNameList.add(industryInfo.getIndustryName());
  }
  }
  }
  }
  }
  // 将根据编码查询不出来 的 行业编码 直接返回
  
                    industryNameList.addAll(paramList);
  return industryNameList;
  }
  }
  return null;
  }
  }
  

  /**
  * 根据城市id查询 城市名称
  *
  * @param industryIds 行业Id可能有多个 以分号分隔
  * @return 行业名称列表
  * @see [相关类/方法](可选)
  * @since [产品/模块版本](可选)
  */
  @Override
  @Cacheable(value = "commonService.queryCityNameByIds", key = "new String('commonService.queryCityNameByIds')+#cityIds", condition = "null != #cityIds and #cityIds.length() > 0")
  public List<String> queryCityNameByIds(String cityIds) {
  if (StringUtils.isBlank(cityIds)) {
  return null;
  } else {
  String replacyedCityIds = cityIds.replace(";", ",");
  String[] industryIdArr = replacyedCityIds.split(",");
  if (null != industryIdArr && industryIdArr.length > 0) {
  ArrayList<String> paramList = new ArrayList<String>();
  paramList.addAll(Arrays.asList(industryIdArr));
  if (null != paramList && !paramList.isEmpty()) {
  Map<String, Object> paramMap = new HashMap<String, Object>();
  paramMap.put("cityIdList", paramList);
  // 查询行业列表
  List<CityBean> queryResultList = dalClient.queryForList("T_CITY.SELECT_BY_ID_LIST", paramMap,
  CityBean.class);
  List<String> industryNameList = new ArrayList<String>();
  if (null != queryResultList && !queryResultList.isEmpty()) {
  // 遍历查询列表 将已经存在的编码去掉 剩下的 就是 根據编码查询不出行业的 直接将行业的名字返回
  // 封装查询结果
  
                        String tempId;
  for (CityBean industryInfo : queryResultList) {
  if (null != industryInfo) {
  if (null == industryInfo.getCityCode()) {
  continue;
  } else {
  tempId = industryInfo.getCityCode().toString();
  if (paramList.contains(tempId)) {
  deleteListElement(paramList, tempId);
  }
  if (StringUtils.isNotBlank(industryInfo.getCityName())) {
  industryNameList.add(industryInfo.getCityName());
  }
  }
  }
  }
  }
  // 将根据编码查询不出来 的 行业编码 直接返回
  
                    industryNameList.addAll(paramList);
  return industryNameList;
  }
  }
  return null;
  }
  }
  

  /**
  * 查询第一级所有职位
  *
  * @return
  */
  @Override
  public List<JobTypeVo> queryFirstJobList() {
  /*
  * List<JobTypeVo> redisIndustryListResult = redisTemplate.execute(new RedisCallback<List<JobTypeVo>>() {
  * @Override public List<JobTypeVo> doInRedis(RedisConnection connection) { byte[] industryListList =
  * connection.get((RedisConstants.JOB_FIRST_LIST).getBytes()); if (null != industryListList &&
  * industryListList.length > 0) { return (List<JobTypeVo>) SerializableObjectUtil.unserialize(industryListList);
  * } else { return null; } } }); if (null != redisIndustryListResult && !redisIndustryListResult.isEmpty()) {
  * return redisIndustryListResult; } else {
  */
  final List<JobTypeVo> queryIndustryListResult = dalClient.queryForList("T_JOB_TYPE.SELECT_FIRST_JOB_CODE",
  null, JobTypeVo.class);
  /*
  * if (null != queryIndustryListResult && !queryIndustryListResult.isEmpty()) { redisTemplate.execute(new
  * RedisCallback<Boolean>() {
  * @Override public Boolean doInRedis(RedisConnection connection) {
  * connection.set((RedisConstants.JOB_FIRST_LIST).getBytes(),
  * SerializableObjectUtil.serialize(queryIndustryListResult)); return true; } }); }
  */
  return queryIndustryListResult;
  /* } */
  }
  

  /**
  * 查询 对应级别的职位信息
  *
  * @param typeValue
  * @param jobCode
  * @return
  */
  @Override
  public List<JobTypeBean> queryJobTypeList(final int typeValue, final int jobCode) {
  /*
  * List<JobTypeBean> redisIndustryListResult = redisTemplate.execute(new RedisCallback<List<JobTypeBean>>() {
  * @Override public List<JobTypeBean> doInRedis(RedisConnection connection) { byte[] industryListList =
  * connection.get((RedisConstants.JOB_FIRST_LIST + typeValue + jobCode) .getBytes()); if (null !=
  * industryListList && industryListList.length > 0) { return (List<JobTypeBean>)
  * SerializableObjectUtil.unserialize(industryListList); } else { return null; } } }); if (null !=
  * redisIndustryListResult && !redisIndustryListResult.isEmpty()) { return redisIndustryListResult; } else {
  */
  Map<String, Object> paramMap = new HashMap<String, Object>();
  paramMap.put("typeValue", typeValue);
  paramMap.put("jobFirstCode", jobCode);
  final List<JobTypeBean> queryResult = dalClient.queryForList("T_JOB_TYPE.SELECT_BY_JOB_CODE", paramMap,
  JobTypeBean.class);
  /*
  * if (null != queryResult && !queryResult.isEmpty()) { redisTemplate.execute(new RedisCallback<Boolean>() {
  * @Override public Boolean doInRedis(RedisConnection connection) {
  * connection.set((RedisConstants.JOB_FIRST_LIST + typeValue + jobCode).getBytes(),
  * SerializableObjectUtil.serialize(queryResult)); return true; } }); }
  */
  return queryResult;
  /* } */
  }
  

  /**
  * 判断学校是否 特殊学校
  *
  * @param schoolName 学校名称
  * @param schoolType 学校分类(1:211 暂无其他)
  * @return true:是, false:否
  * @see [相关类/方法](可选)
  * @since [产品/模块版本](可选)
  */
  @Override
  @Cacheable(value = "commonService.isSpecialSchool", key = "new String('commonService.isSpecialSchool')+#schoolName + #schoolType", condition = "null != #schoolName and null !=#schoolType and #schoolName.length() > 0")
  public boolean isSpecialSchool(String schoolName, int schoolType) {
  if (StringUtils.isEmpty(schoolName)) {
  return false;
  } else {
  Map<String, Object> paramMap = new HashMap<String, Object>();
  paramMap.put("schoolName", schoolName);
  paramMap.put("schoolType", schoolType);
  Map<String, Object> resultMap = dalClient.queryForMap("T_MY_SPECIAL_SCHOOL.SELECT_BY_NAME_TYPE", paramMap);
  if (null != resultMap && !resultMap.isEmpty() && resultMap.containsKey("NUM")) {
  return (long) resultMap.get("NUM") > 0;
  } else {
  return false;
  }
  }
  }
  

  /**
  * 根据城市名称获取 城市所在 省份名称
  *
  * @param cityNames
  * @return
  * @see [相关类/方法](可选)
  * @since [产品/模块版本](可选)
  */
  @Override
  @Cacheable(value = "commonService.getProvinceByCity", key = "new String('commonService.getProvinceByCity')+#cityNames", condition = "null != #cityNames and #cityNames.length() > 0")
  public String getProvinceByCity(final String cityNames) {
  if (StringUtils.isBlank(cityNames)) {
  return null;
  } else {
  String[] cityArr = cityNames.split("、");
  Map<String, Object> paramMap = new HashMap<String, Object>();
  Map<String, Object> resultMap;
  String provinceName;
  List<String> provinceLait = new ArrayList<String>();
  for (String cityName : cityArr) {
  if (StringUtils.isNotBlank(cityName)) {
  paramMap.put("cityName", cityName);
  resultMap = dalClient.queryForMap("T_CITY.SELECT_PROVINCE_NAMEBY_CITY_NAME", paramMap);
  if (null != resultMap && !resultMap.isEmpty() && resultMap.containsKey("provinceName")) {
  provinceName = String.valueOf(resultMap.get("provinceName"));
  if (!provinceLait.contains(provinceName)) {
  provinceLait.add(provinceName);
  }
  }
  }
  }
  StringBuffer sb = new StringBuffer(100);
  if (!provinceLait.isEmpty()) {
  for (int i = 0; i < provinceLait.size(); i++) {
  if (i < provinceLait.size() - 1) {
  sb.append(provinceLait.get(i)).append(",");
  } else {
  sb.append(provinceLait.get(i));
  }
  }
  }
  return sb.toString();
  }
  }

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-426696-1-1.html 上篇帖子: php redis pub/sub(Publish/Subscribe,发布/订阅的信息系统)之基本使用 下篇帖子: python3存入redis是bytes
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表