现状问题:
ExcelDictFormat注解转义数据,每条记录都需要重复查询字典数据性能瓶颈:
ExcelDictConvert.convertToExcelData() 方法中,每条记录都会调用 DictService.getDictLabel()getDictLabel() 每次都会调用 selectDictDataByType() 查询数据库selectDictDataByType() 有 @Cacheable 注解,但在导出大量数据时,缓存效果有限文件: backend/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/convert/ExcelDictPreloadConvert.java
核心特性:
ConcurrentHashMap存储字典数据,避免重复加载ConcurrentHashMap保证多线程安全关键方法:
// 预加载指定字典类型
public static void preloadDict(String dictType)
// 批量预加载多个字典类型
public static void preloadDicts(String... dictTypes)
// 从缓存获取字典标签
private String getDictLabelFromCache(String dictType, String value, String separator)
// 清理缓存
public static void clearCache()
// 获取缓存统计信息
public static Map<String, Integer> getCacheStats()
文件: backend/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/service/BaseExportService.java
功能特性:
文件: backend/ruoyi-modules/ruoyi-xg/src/main/java/org/dromara/xg/domain/vo/XgStudentMsgExportVo.java
修改内容:
ExcelDictConvert改为ExcelDictPreloadConvert修改示例:
// 修改前
@ExcelProperty(value = "性别", converter = ExcelDictConvert.class)
@ExcelDictFormat(dictType = "sys_user_sex")
private String gender;
// 修改后
@ExcelProperty(value = "性别", converter = ExcelDictPreloadConvert.class)
@ExcelDictFormat(dictType = "sys_user_sex")
private String gender;
文件: backend/ruoyi-modules/ruoyi-xg/src/main/java/org/dromara/xg/controller/XgStudentMsgController.java
优化内容:
preloadExportDicts()预加载字典logCacheStats()记录缓存统计信息预加载字典类型:
ExcelDictPreloadConvert.preloadDicts(
"sys_user_sex", // 性别
"xg_nation", // 民族
"sys_region", // 行政区划
"xg_political_status", // 政治面貌
"sys_yesno", // 是否选项
"wenhuachengdu", // 文化程度
"house_hold_register_type", // 户口类型
"marital_status", // 婚姻状况
"xg_family_economic", // 家庭经济状况
"xg_blood_type", // 血型
"disability_type", // 残疾类型
"admission_category", // 录取类别
"student_status", // 学籍状态
"zsjy_education_level", // 学历层次
"xg_training_mode" // 培养方式
);
ExcelDictPreloadConvert类BaseExportService类clearCache()清理缓存本次优化通过预加载字典缓存的方式,显著提升了学生信息导出的性能:
该方案为后续的导出性能优化提供了标准模板,可根据实际使用效果进一步优化和扩展。