package com.brframework.commonwebbase.service; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.TypeReference; import com.brframework.commonwebbase.dao.DictionaryDao; import com.brframework.commonwebbase.entity.Dictionary; import com.google.common.base.Strings; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import java.time.LocalDateTime; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Created by xu *
* 字典
*
* @author xu
* @date 18-4-19 上午10:07
*/
@Service
@CacheConfig(cacheNames = "default")
public class DictionaryService {
@Autowired
DictionaryDao dictionaryDao;
/**
* 保存key value
*
* @param key
* @param value
*/
@CacheEvict(key = "targetClass + '-key-' + #key")
public void set(String key, String value) {
Dictionary dictionary = dictionaryDao.findByKey(key);
if (dictionary == null) {
dictionary = new Dictionary();
dictionary.setKey(key);
dictionary.setCreateDate(LocalDateTime.now());
}
dictionary.setValue(value);
dictionary.setUpdateDate(LocalDateTime.now());
dictionaryDao.save(dictionary);
}
/**
* 获取 value 对象
*
* @param key
* @return
*/
@Cacheable(key = "targetClass + '-key-' + #key")
public String get(String key) {
Dictionary dictionary = dictionaryDao.findByKey(key);
if (dictionary == null) {
return "";
}
return dictionary.getValue();
}
public > type){
return new ListHandle<>(dictionaryKey, this, type);
}
/**
* 字典的List操作形式
* @param
> type;
private ListHandle(String dictionaryKey, DictionaryService dictionaryService,
TypeReference
> type){
this.dictionaryKey = dictionaryKey;
this.dictionaryService = dictionaryService;
this.type = type;
}
public List