Pre Merge pull request !20 from yawu_bear/yawu_branch
This commit is contained in:
commit
e4c986e7cc
|
|
@ -0,0 +1,112 @@
|
|||
/**
|
||||
* Copyright © 2018 All rights reserved.
|
||||
*/
|
||||
package com.ruoyi.common.utils;
|
||||
|
||||
import org.apache.shiro.cache.Cache;
|
||||
import org.apache.shiro.cache.CacheManager;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Cache工具类
|
||||
*
|
||||
* @author yawu_bear
|
||||
* @date 2018-08-10
|
||||
*/
|
||||
public class CacheUtils {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(CacheUtils.class);
|
||||
private static CacheManager cacheManager = SpringUtil.getBean(CacheManager.class);
|
||||
|
||||
/**
|
||||
* 获取缓存
|
||||
*
|
||||
* @param cacheName
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public static Object get(String cacheName, String key) {
|
||||
return getCache(cacheName).get(getKey(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓存
|
||||
*
|
||||
* @param cacheName
|
||||
* @param key
|
||||
* @param defaultValue
|
||||
* @return
|
||||
*/
|
||||
public static Object get(String cacheName, String key, Object defaultValue) {
|
||||
Object value = get(cacheName, getKey(key));
|
||||
return value != null ? value : defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入缓存
|
||||
*
|
||||
* @param cacheName
|
||||
* @param key
|
||||
* @param value
|
||||
*/
|
||||
public static void put(String cacheName, String key, Object value) {
|
||||
getCache(cacheName).put(getKey(key), value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从缓存中移除
|
||||
*
|
||||
* @param cacheName
|
||||
* @param key
|
||||
*/
|
||||
public static void remove(String cacheName, String key) {
|
||||
getCache(cacheName).remove(getKey(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* 从缓存中移除所有
|
||||
*
|
||||
* @param cacheName
|
||||
*/
|
||||
public static void removeAll(String cacheName) {
|
||||
Cache<String, Object> cache = getCache(cacheName);
|
||||
Set<String> keys = cache.keys();
|
||||
for (Iterator<String> it = keys.iterator(); it.hasNext(); ) {
|
||||
cache.remove(it.next());
|
||||
}
|
||||
logger.info("清理缓存: {} => {}", cacheName, keys);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓存键名,多数据源下增加数据源名称前缀
|
||||
*
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
private static String getKey(String key) {
|
||||
/*String dsName = DataSourceHolder.getDataSourceName();
|
||||
if (StringUtils.isNotBlank(dsName)) {
|
||||
return dsName + "_" + key;
|
||||
}*/
|
||||
return key;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得一个Cache,没有则显示日志。
|
||||
*
|
||||
* @param cacheName
|
||||
* @return
|
||||
*/
|
||||
protected static Cache<String, Object> getCache(String cacheName) {
|
||||
Cache<String, Object> cache = cacheManager.getCache(cacheName);
|
||||
if (cache == null) {
|
||||
throw new RuntimeException("当前系统中没有定义“" + cacheName + "”这个缓存。");
|
||||
}
|
||||
return cache;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,58 @@
|
|||
/**
|
||||
* Copyright © 2018 All rights reserved.
|
||||
*/
|
||||
package com.ruoyi.common.utils;
|
||||
|
||||
import com.ruoyi.project.system.dict.domain.DictData;
|
||||
import com.ruoyi.project.system.dict.mapper.DictDataMapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 字典工具类
|
||||
*
|
||||
* @author yawu_bear
|
||||
* @date 2018-08-10
|
||||
*/
|
||||
|
||||
public class DictUtils {
|
||||
|
||||
private static DictDataMapper dictDataMapper = SpringUtil.getBean(DictDataMapper.class);
|
||||
|
||||
public static final String DICT_CACHE = "dictCache";
|
||||
public static final String DICT_CACHE_TYPE = "type_";
|
||||
|
||||
/**
|
||||
* 根据类型和键值获取标签
|
||||
*
|
||||
* @param type
|
||||
* @param value
|
||||
* @return
|
||||
*/
|
||||
public static String getDictLabel(String type, String value) {
|
||||
String label = "";
|
||||
if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(value)) {
|
||||
for (DictData dictData : getDataByType(type)) {
|
||||
if (label.equals(dictData.getDictValue())) {
|
||||
label = dictData.getDictLabel();
|
||||
}
|
||||
}
|
||||
}
|
||||
return label;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据type获取data列表
|
||||
*
|
||||
* @param dictType
|
||||
* @return
|
||||
*/
|
||||
public static List<DictData> getDataByType(String dictType) {
|
||||
List<DictData> dictList = (List<DictData>) CacheUtils.get(DICT_CACHE, DICT_CACHE_TYPE + dictType);
|
||||
if (StringUtils.isEmpty(dictList)) {
|
||||
dictList = dictDataMapper.selectDictDataByType(dictType);
|
||||
CacheUtils.put(DICT_CACHE, DICT_CACHE_TYPE + dictType, dictList);
|
||||
}
|
||||
return dictList;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,77 @@
|
|||
package com.ruoyi.common.utils;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* SpringUtil 工具类
|
||||
* @author yawu_bear
|
||||
* @date 2018-08-10
|
||||
*/
|
||||
@Component
|
||||
public class SpringUtil implements ApplicationContextAware {
|
||||
|
||||
private static ApplicationContext applicationContext;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
if (SpringUtil.applicationContext == null) {
|
||||
SpringUtil.applicationContext = applicationContext;
|
||||
}
|
||||
System.out.println("---------------------------------------------------------------------");
|
||||
System.out.println("---------------------------------------------------------------------");
|
||||
System.out.println("---------------me.shijunjie.util.SpringUtil------------------------------------------------------");
|
||||
System.out.println("========ApplicationContext配置成功,在普通类可以通过调用SpringUtils.getAppContext()获取applicationContext对象,applicationContext=" + SpringUtil.applicationContext + "========");
|
||||
System.out.println("---------------------------------------------------------------------");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取applicationContext
|
||||
* @return
|
||||
*/
|
||||
public static ApplicationContext getApplicationContext() {
|
||||
return applicationContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过name获取Bean.
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
public static Object getBean(String name) {
|
||||
return getApplicationContext().getBean(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过class获取Bean.
|
||||
* @param clazz
|
||||
* @param <T>
|
||||
* @return
|
||||
*/
|
||||
public static <T> T getBean(Class<T> clazz) {
|
||||
return getApplicationContext().getBean(clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过name,以及Clazz返回指定的Bean.
|
||||
* @param name
|
||||
* @param clazz
|
||||
* @param <T>
|
||||
* @return
|
||||
*/
|
||||
public static <T> T getBean(String name, Class<T> clazz) {
|
||||
return getApplicationContext().getBean(name, clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
public static boolean containsBean(String name) {
|
||||
return applicationContext.containsBean(name);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,6 +1,9 @@
|
|||
package com.ruoyi.project.system.dict.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.ruoyi.common.utils.CacheUtils;
|
||||
import com.ruoyi.common.utils.DictUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import com.ruoyi.common.support.Convert;
|
||||
|
|
@ -10,7 +13,7 @@ import com.ruoyi.project.system.dict.mapper.DictDataMapper;
|
|||
|
||||
/**
|
||||
* 字典 业务层处理
|
||||
*
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
@Service
|
||||
|
|
@ -21,7 +24,7 @@ public class DictDataServiceImpl implements IDictDataService
|
|||
|
||||
/**
|
||||
* 根据条件分页查询字典数据
|
||||
*
|
||||
*
|
||||
* @param dictData 字典数据信息
|
||||
* @return 字典数据集合信息
|
||||
*/
|
||||
|
|
@ -33,19 +36,20 @@ public class DictDataServiceImpl implements IDictDataService
|
|||
|
||||
/**
|
||||
* 根据字典类型查询字典数据
|
||||
*
|
||||
*
|
||||
* @param dictType 字典类型
|
||||
* @return 字典数据集合信息
|
||||
*/
|
||||
@Override
|
||||
public List<DictData> selectDictDataByType(String dictType)
|
||||
{
|
||||
return dictDataMapper.selectDictDataByType(dictType);
|
||||
// return dictDataMapper.selectDictDataByType(dictType);
|
||||
return DictUtils.getDataByType(dictType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据字典类型和字典键值查询字典数据信息
|
||||
*
|
||||
*
|
||||
* @param dictType 字典类型
|
||||
* @param dictValue 字典键值
|
||||
* @return 字典标签
|
||||
|
|
@ -53,12 +57,13 @@ public class DictDataServiceImpl implements IDictDataService
|
|||
@Override
|
||||
public String selectDictLabel(String dictType, String dictValue)
|
||||
{
|
||||
return dictDataMapper.selectDictLabel(dictType, dictValue);
|
||||
// return dictDataMapper.selectDictLabel(dictType, dictValue);
|
||||
return DictUtils.getDictLabel(dictType, dictValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据字典数据ID查询信息
|
||||
*
|
||||
*
|
||||
* @param dictCode 字典数据ID
|
||||
* @return 字典数据
|
||||
*/
|
||||
|
|
@ -70,7 +75,7 @@ public class DictDataServiceImpl implements IDictDataService
|
|||
|
||||
/**
|
||||
* 通过字典ID删除字典数据信息
|
||||
*
|
||||
*
|
||||
* @param dictCode 字典数据ID
|
||||
* @return 结果
|
||||
*/
|
||||
|
|
@ -82,7 +87,7 @@ public class DictDataServiceImpl implements IDictDataService
|
|||
|
||||
/**
|
||||
* 批量删除字典数据
|
||||
*
|
||||
*
|
||||
* @param ids 需要删除的数据
|
||||
* @return 结果
|
||||
*/
|
||||
|
|
@ -94,7 +99,7 @@ public class DictDataServiceImpl implements IDictDataService
|
|||
|
||||
/**
|
||||
* 新增保存字典数据信息
|
||||
*
|
||||
*
|
||||
* @param dictData 字典数据信息
|
||||
* @return 结果
|
||||
*/
|
||||
|
|
@ -102,12 +107,20 @@ public class DictDataServiceImpl implements IDictDataService
|
|||
public int insertDictData(DictData dictData)
|
||||
{
|
||||
dictData.setCreateBy(ShiroUtils.getLoginName());
|
||||
return dictDataMapper.insertDictData(dictData);
|
||||
String dictType = dictData.getDictType();
|
||||
int row = dictDataMapper.insertDictData(dictData);
|
||||
if(row > 0) {
|
||||
// 新增成功修改缓存信息
|
||||
List<DictData> dictList = dictDataMapper.selectDictDataByType(dictType);
|
||||
CacheUtils.remove(DictUtils.DICT_CACHE, DictUtils.DICT_CACHE_TYPE + dictType);
|
||||
CacheUtils.put(DictUtils.DICT_CACHE, DictUtils.DICT_CACHE_TYPE + dictType, dictList);
|
||||
}
|
||||
return row;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改保存字典数据信息
|
||||
*
|
||||
*
|
||||
* @param dictData 字典数据信息
|
||||
* @return 结果
|
||||
*/
|
||||
|
|
@ -115,7 +128,15 @@ public class DictDataServiceImpl implements IDictDataService
|
|||
public int updateDictData(DictData dictData)
|
||||
{
|
||||
dictData.setUpdateBy(ShiroUtils.getLoginName());
|
||||
return dictDataMapper.updateDictData(dictData);
|
||||
String dictType = dictData.getDictType();
|
||||
int row = dictDataMapper.updateDictData(dictData);
|
||||
if(row > 0) {
|
||||
List<DictData> dictList = dictDataMapper.selectDictDataByType(dictType);
|
||||
// 修改成功更新缓存信息
|
||||
CacheUtils.remove(DictUtils.DICT_CACHE, DictUtils.DICT_CACHE_TYPE + dictType);
|
||||
CacheUtils.put(DictUtils.DICT_CACHE, DictUtils.DICT_CACHE_TYPE + dictType, dictList);
|
||||
}
|
||||
return row;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,9 +5,9 @@ spring:
|
|||
driverClassName: com.mysql.jdbc.Driver
|
||||
druid:
|
||||
master: #主库
|
||||
url: jdbc:mysql://localhost:3306/ry?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true
|
||||
url: jdbc:mysql://192.168.0.132:3306/rys?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true
|
||||
username: root
|
||||
password: password
|
||||
password: 123456
|
||||
#slave: #从库
|
||||
# open: true
|
||||
# url: jdbc:mysql://localhost:3306/ry?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true
|
||||
|
|
|
|||
|
|
@ -23,5 +23,15 @@
|
|||
statistics="true">
|
||||
</cache>
|
||||
|
||||
<!-- 系统字典缓存 -->
|
||||
<cache name="dictCache"
|
||||
maxEntriesLocalHeap="10000"
|
||||
overflowToDisk="false"
|
||||
eternal="false"
|
||||
diskPersistent="false"
|
||||
timeToLiveSeconds="0"
|
||||
timeToIdleSeconds="0"
|
||||
statistics="true"/>
|
||||
|
||||
</ehcache>
|
||||
|
||||
Loading…
Reference in New Issue