1. 找到字典切面类(DictAspect)

  2. 改造方法(parseDictText)

支持自动生成的列表接口/单个实体类查询翻译
代码如下:

private void parseDictText(Object result) {if (result instanceof Result) {if (((Result) result).getResult() instanceof IPage) {List<JSONObject> items = new ArrayList<>();for (Object record : ((IPage) ((Result) result).getResult()).getRecords()) {ObjectMapper mapper = new ObjectMapper();String json = "{}";try {//解决@JsonFormat注解解析不了的问题详见SysAnnouncement类的@JsonFormatjson = mapper.writeValueAsString(record);} catch (JsonProcessingException e) {log.error("json解析失败" + e.getMessage(), e);}JSONObject item = JSONObject.parseObject(json);//update-begin--Author:scott -- Date:20190603 ----for:解决继承实体字段无法翻译问题------//for (Field field : record.getClass().getDeclaredFields()) {for (Field field : oConvertUtils.getAllFields(record)) {//update-end--Author:scott  -- Date:20190603 ----for:解决继承实体字段无法翻译问题------if (field.getAnnotation(Dict.class) != null) {String code = field.getAnnotation(Dict.class).dicCode();String text = field.getAnnotation(Dict.class).dicText();String table = field.getAnnotation(Dict.class).dictTable();String key = String.valueOf(item.get(field.getName()));//翻译字典值对应的txtString textValue = translateDictValue(code, text, table, key);log.debug(" 字典Val : " + textValue);log.debug(" __翻译字典字段__ " + field.getName() + CommonConstant.DICT_TEXT_SUFFIX + ": " + textValue);item.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue);}//date类型默认转换string格式化日期if (field.getType().getName().equals("java.util.Date") && field.getAnnotation(JsonFormat.class) == null && item.get(field.getName()) != null) {SimpleDateFormat aDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");item.put(field.getName(), aDate.format(new Date((Long) item.get(field.getName()))));}}items.add(item);}((IPage) ((Result) result).getResult()).setRecords(items);} else {Object record = (Object) ((Result) result).getResult();ObjectMapper mapper = new ObjectMapper();String json = "{}";try {//解决@JsonFormat注解解析不了的问题详见SysAnnouncement类的@JsonFormatjson = mapper.writeValueAsString(record);} catch (JsonProcessingException e) {log.error("json解析失败" + e.getMessage(), e);}try {JSONObject item = JSONObject.parseObject(json);//update-begin--Author:scott -- Date:20190603 ----for:解决继承实体字段无法翻译问题------//for (Field field : record.getClass().getDeclaredFields()) {for (Field field : oConvertUtils.getAllFields(record)) {//update-end--Author:scott  -- Date:20190603 ----for:解决继承实体字段无法翻译问题------if (field.getAnnotation(Dict.class) != null) {String code = field.getAnnotation(Dict.class).dicCode();String text = field.getAnnotation(Dict.class).dicText();String table = field.getAnnotation(Dict.class).dictTable();String key = String.valueOf(item.get(field.getName()));//翻译字典值对应的txtString textValue = translateDictValue(code, text, table, key);log.debug(" 字典Val : " + textValue);log.debug(" __翻译字典字段__ " + field.getName() + CommonConstant.DICT_TEXT_SUFFIX + ": " + textValue);item.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue);}//date类型默认转换string格式化日期if (field.getType().getName().equals("java.util.Date") && field.getAnnotation(JsonFormat.class) == null && item.get(field.getName()) != null) {SimpleDateFormat aDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");item.put(field.getName(), aDate.format(new Date((Long) item.get(field.getName()))));}}
//                }((Result) result).setResult(item);}catch (Exception e){log.info("########################此返回类型不支持字典翻译########################");}}}}

单个查询返回数据格式样例:

/*** 通过id查询** @param id* @return*/@GetMapping(value = "/queryById")
public Result<?> queryById(@RequestParam(name="id",required=true) String id) {ShgBanner shgBanner = shgBannerService.getById(id);if(shgBanner==null) {return Result.error("未找到对应数据");}return Result.ok(shgBanner);
}

第二版改造 : 2023/2/22

总结:

  1. 支持单个实体类解析
  2. 支持实体类集合解析
  3. 支持过滤基本类型集合不进行解析, 暂时过滤了三种 java.lang.String java.lang.long java.lang.Integer

完整方法如下 :

private void parseDictTextOne(Object result) {if (result instanceof Result) {if (((Result) result).getResult() instanceof IPage) {List<JSONObject> items = new ArrayList<>();for (Object record : ((IPage) ((Result) result).getResult()).getRecords()) {ObjectMapper mapper = new ObjectMapper();String json = "{}";try {//解决@JsonFormat注解解析不了的问题详见SysAnnouncement类的@JsonFormatjson = mapper.writeValueAsString(record);} catch (JsonProcessingException e) {log.error("json解析失败" + e.getMessage(), e);}JSONObject item = JSONObject.parseObject(json);//update-begin--Author:scott -- Date:20190603 ----for:解决继承实体字段无法翻译问题------//for (Field field : record.getClass().getDeclaredFields()) {for (Field field : oConvertUtils.getAllFields(record)) {//update-end--Author:scott  -- Date:20190603 ----for:解决继承实体字段无法翻译问题------if (field.getAnnotation(Dict.class) != null) {String code = field.getAnnotation(Dict.class).dicCode();String text = field.getAnnotation(Dict.class).dicText();String table = field.getAnnotation(Dict.class).dictTable();String key = String.valueOf(item.get(field.getName()));//翻译字典值对应的txtString textValue = translateDictValue(code, text, table, key);log.debug(" 字典Val : " + textValue);log.debug(" __翻译字典字段__ " + field.getName() + CommonConstant.DICT_TEXT_SUFFIX + ": " + textValue);item.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue);}//date类型默认转换string格式化日期if (field.getType().getName().equals("java.util.Date") && field.getAnnotation(JsonFormat.class) == null && item.get(field.getName()) != null) {SimpleDateFormat aDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");item.put(field.getName(), aDate.format(new Date((Long) item.get(field.getName()))));}}items.add(item);}((IPage) ((Result) result).getResult()).setRecords(items);} else if (((Result) result).getResult() instanceof List) {List result1 = (List) ((Result) result).getResult();if (CollectionUtils.isNotEmpty(result1)) {Class<?> aClass = result1.get(0).getClass();String className = aClass.getName();log.info("------- List<T> 泛型 T is {}", className);//只解析result中list泛型是object的数据, 基本数据类型不解析/*** 以下类型不解析* java.lang.String* java.lang.long* java.lang.Integer*/if (!className.equals("java.lang.String") && !className.equals("java.lang.long") && !className.equals("java.lang.Integer")) {List<JSONObject> items = new ArrayList<>();for (Object record : (List) ((Result) result).getResult()) {ObjectMapper mapper = new ObjectMapper();String json = "{}";try {//解决@JsonFormat注解解析不了的问题详见SysAnnouncement类的@JsonFormatjson = mapper.writeValueAsString(record);} catch (JsonProcessingException e) {log.error("json解析失败" + e.getMessage(), e);}JSONObject item = JSONObject.parseObject(json);//update-begin--Author:scott -- Date:20190603 ----for:解决继承实体字段无法翻译问题------//for (Field field : record.getClass().getDeclaredFields()) {for (Field field : oConvertUtils.getAllFields(record)) {//update-end--Author:scott  -- Date:20190603 ----for:解决继承实体字段无法翻译问题------if (field.getAnnotation(Dict.class) != null) {String code = field.getAnnotation(Dict.class).dicCode();String text = field.getAnnotation(Dict.class).dicText();String table = field.getAnnotation(Dict.class).dictTable();String key = String.valueOf(item.get(field.getName()));//翻译字典值对应的txtString textValue = translateDictValue(code, text, table, key);log.debug(" 字典Val : " + textValue);log.debug(" __翻译字典字段__ " + field.getName() + CommonConstant.DICT_TEXT_SUFFIX + ": " + textValue);item.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue);}//date类型默认转换string格式化日期if (field.getType().getName().equals("java.util.Date") && field.getAnnotation(JsonFormat.class) == null && item.get(field.getName()) != null) {SimpleDateFormat aDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");item.put(field.getName(), aDate.format(new Date((Long) item.get(field.getName()))));}}items.add(item);}((Result) result).setResult(items);}else {log.info("只解析result中list<T>泛型是实体类的数据, 基本数据类型不解析: T is {}", className);}}else {log.info(" result中List<T> 为空,跳过不解析 ");}} else {Object record = (Object) ((Result) result).getResult();ObjectMapper mapper = new ObjectMapper();String json = "{}";try {//解决@JsonFormat注解解析不了的问题详见SysAnnouncement类的@JsonFormatjson = mapper.writeValueAsString(record);} catch (JsonProcessingException e) {log.error("json解析失败" + e.getMessage(), e);}try {JSONObject item = JSONObject.parseObject(json);//update-begin--Author:scott -- Date:20190603 ----for:解决继承实体字段无法翻译问题------//for (Field field : record.getClass().getDeclaredFields()) {for (Field field : oConvertUtils.getAllFields(record)) {//update-end--Author:scott  -- Date:20190603 ----for:解决继承实体字段无法翻译问题------if (field.getAnnotation(Dict.class) != null) {String code = field.getAnnotation(Dict.class).dicCode();String text = field.getAnnotation(Dict.class).dicText();String table = field.getAnnotation(Dict.class).dictTable();String key = String.valueOf(item.get(field.getName()));//翻译字典值对应的txtString textValue = translateDictValue(code, text, table, key);log.debug(" 字典Val : " + textValue);log.debug(" __翻译字典字段__ " + field.getName() + CommonConstant.DICT_TEXT_SUFFIX + ": " + textValue);item.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue);}//date类型默认转换string格式化日期if (field.getType().getName().equals("java.util.Date") && field.getAnnotation(JsonFormat.class) == null && item.get(field.getName()) != null) {SimpleDateFormat aDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");item.put(field.getName(), aDate.format(new Date((Long) item.get(field.getName()))));}}((Result) result).setResult(item);} catch (Exception e) {log.info("########################此返回类型不支持字典翻译########################");}}}}

改造部分如下 :

else if (((Result) result).getResult() instanceof List) {List result1 = (List) ((Result) result).getResult();if (CollectionUtils.isNotEmpty(result1)) {Class<?> aClass = result1.get(0).getClass();String className = aClass.getName();log.info("------- List<T> 泛型 T is {}", className);//只解析result中list泛型是object的数据, 基本数据类型不解析/*** 以下类型不解析* java.lang.String* java.lang.long* java.lang.Integer*/if (!className.equals("java.lang.String") && !className.equals("java.lang.long") && !className.equals("java.lang.Integer")) {List<JSONObject> items = new ArrayList<>();for (Object record : (List) ((Result) result).getResult()) {ObjectMapper mapper = new ObjectMapper();String json = "{}";try {//解决@JsonFormat注解解析不了的问题详见SysAnnouncement类的@JsonFormatjson = mapper.writeValueAsString(record);} catch (JsonProcessingException e) {log.error("json解析失败" + e.getMessage(), e);}JSONObject item = JSONObject.parseObject(json);//update-begin--Author:scott -- Date:20190603 ----for:解决继承实体字段无法翻译问题------//for (Field field : record.getClass().getDeclaredFields()) {for (Field field : oConvertUtils.getAllFields(record)) {//update-end--Author:scott  -- Date:20190603 ----for:解决继承实体字段无法翻译问题------if (field.getAnnotation(Dict.class) != null) {String code = field.getAnnotation(Dict.class).dicCode();String text = field.getAnnotation(Dict.class).dicText();String table = field.getAnnotation(Dict.class).dictTable();String key = String.valueOf(item.get(field.getName()));//翻译字典值对应的txtString textValue = translateDictValue(code, text, table, key);log.debug(" 字典Val : " + textValue);log.debug(" __翻译字典字段__ " + field.getName() + CommonConstant.DICT_TEXT_SUFFIX + ": " + textValue);item.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue);}//date类型默认转换string格式化日期if (field.getType().getName().equals("java.util.Date") && field.getAnnotation(JsonFormat.class) == null && item.get(field.getName()) != null) {SimpleDateFormat aDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");item.put(field.getName(), aDate.format(new Date((Long) item.get(field.getName()))));}}items.add(item);}((Result) result).setResult(items);}else {log.info("只解析result中list<T>泛型是实体类的数据, 基本数据类型不解析: T is {}", className);}}else {log.info(" result中List<T> 为空,跳过不解析 ");}} else {Object record = (Object) ((Result) result).getResult();ObjectMapper mapper = new ObjectMapper();String json = "{}";try {//解决@JsonFormat注解解析不了的问题详见SysAnnouncement类的@JsonFormatjson = mapper.writeValueAsString(record);} catch (JsonProcessingException e) {log.error("json解析失败" + e.getMessage(), e);}try {JSONObject item = JSONObject.parseObject(json);//update-begin--Author:scott -- Date:20190603 ----for:解决继承实体字段无法翻译问题------//for (Field field : record.getClass().getDeclaredFields()) {for (Field field : oConvertUtils.getAllFields(record)) {//update-end--Author:scott  -- Date:20190603 ----for:解决继承实体字段无法翻译问题------if (field.getAnnotation(Dict.class) != null) {String code = field.getAnnotation(Dict.class).dicCode();String text = field.getAnnotation(Dict.class).dicText();String table = field.getAnnotation(Dict.class).dictTable();String key = String.valueOf(item.get(field.getName()));//翻译字典值对应的txtString textValue = translateDictValue(code, text, table, key);log.debug(" 字典Val : " + textValue);log.debug(" __翻译字典字段__ " + field.getName() + CommonConstant.DICT_TEXT_SUFFIX + ": " + textValue);item.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue);}//date类型默认转换string格式化日期if (field.getType().getName().equals("java.util.Date") && field.getAnnotation(JsonFormat.class) == null && item.get(field.getName()) != null) {SimpleDateFormat aDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");item.put(field.getName(), aDate.format(new Date((Long) item.get(field.getName()))));}}((Result) result).setResult(item);} catch (Exception e) {log.info("########################此返回类型不支持字典翻译########################");}}

支持如下返回方式 :

// 单个实体解析
@GetMapping(value = "/queryById")
public Result<?> queryById(@RequestParam(name="id",required=true) String id) {ShgBanner shgBanner = shgBannerService.getById(id);if(shgBanner==null) {return Result.error("未找到对应数据");}return Result.ok(shgBanner);
}// 列表实体解析
@GetMapping("/queryRawMaters")
public Result<?> queryRawMaters(){List<HgRawMaterial> rawMaterialList = hgFinishedProductSonService.queryRawMaters();return Result.OK(rawMaterialList);
}

jeecg-boot字典翻译改造(支持实体类详情查询自动翻译)相关推荐

  1. Jeecg-boot字典翻译改造

    一.找到字典切面类(DictAspect) 二.改造方法(parseDictText) 三.修改后的parseDictText方法,支持IPage.List.Object private void p ...

  2. SpringBoot+MybatisPlus无实体类,查询数据库

    因为要查询表的数据,表的名称是从另一个表中取出的,查询哪个表不一定,所以想到了不写实体类查询. 为了防止字段内容为null时,字段不出现,在application.yml中加 mybatis-plus ...

  3. java实体类中有枚举类型_实体类的枚举属性--原来支持枚举类型这么简单,没有EF5.0也可以...

    通常,我们都是在业务层和界面层使用枚举类型,这能够为我们编程带来便利,但在数据访问层,不使用枚举类型,因为很多数据库都不支持,比如我们现在用的SqlServer2008就不支持枚举类型的列,用的时候也 ...

  4. 转换实体类_yue-library 2.3.0发布,替换Db JavaBean转换方案,性能提升约300%+

    yue-library简介 yue-library是一个基于SpringBoot封装的增强库 内置丰富的JDK工具 自动装配了一系列的基础Bean与环境配置项 快速构建SpringCloud项目,让微 ...

  5. java 实体类 临时注解_JPA:Java持久层API--配置流程

    一.JPA概述 1.1 JPA是什么 JPA (Java Persistence API) Java持久化API.是一套Sun公司 Java官方制定的ORM 方案,是规范,是标准 ,sun公司自己并没 ...

  6. hibernate注解实体类(Emp.java)

    Emp.java 员工信息表的注解实体类详情: package cn.bdqn.hibernate_Criteria.entity;import java.util.Date; import java ...

  7. hibernate注解实体类(Dept.java)

    Dept.java 部门信息表的实体类详情 package cn.bdqn.hibernate_Criteria.entity;import java.util.HashSet; import jav ...

  8. Jeecg Boot 2.3 里程碑版本发布,支持微服务和单体自由切换、提供新行编辑表格JVXETable

    项目介绍 JeecgBoot是一款基于代码生成器的低代码平台,开源界"小普元"超越传统商业级平台!采用前后端分离架构:SpringBoot 2.x,Ant Design&V ...

  9. spring boot添加 LocalDateTime 等 java8 时间类序列化和反序列化的支持

    由于项目将原有的  Date类型的字段改造为 LocalDate,LocalDateTime,LocalTime 类型, 发现  spring  对项目的时间格式无法自动转换,故需手动配置下. 在sp ...

最新文章

  1. 中国编程第一人,李开复欣赏他,百度留不住他...
  2. 任艳频 | 竞赛12年纪念文集--后记
  3. python从入门到实践回顾——字典
  4. 计算机二级一年几次湖南省,湖南省计算机二级多少分可以通过
  5. Python 内部:可调用对象是如何工作的
  6. 困扰我多年的Java泛型〈? extends T 〉和 〈? super T 〉,终于搞清楚了!
  7. 剑指offer之斐波那契问题(C++/Java双重实现)
  8. sicily vector有序插入
  9. 前端学习(1286):node运行环境安装失败
  10. SpringMvc2 使用注解形式发布请求地址
  11. 从零实现深度学习框架——常见运算的计算图
  12. spring boot 用dbcp2连接数据库出现(Access denied for user 'root'@'localhost' (using password: YES)) 异常
  13. Excel表格-数据统计
  14. LFS(Linux From Scratch)构建过程全记录(一):准备工作
  15. 计算机科学中atm是什么,计算机专业知识:ATM网络基本原理
  16. msfvenom生成木马的简单利用
  17. 绝对位置运动指令(MoveAbsJ)
  18. System.CommandLine选项Option
  19. 用NetTerm连接虚拟机的telnet服务,打造轻松自如的虚拟机实验环境
  20. java搭建房子图片,漂亮的农村一层半自建楼房户型图片大全

热门文章

  1. 软件过程--XP 与 RUP 的比较与分析
  2. 接口和抽象类有什么区别
  3. 下雨天睡不够 去湿气功略拿走不谢
  4. 机械工程研究生转行计算机,机械类考研可以转什么专业?能不跨专业最好,跨专业定当考虑周全...
  5. mysql useradd_一天一个linux基础命令之添加用户useradd
  6. 使用C++开发RPC框架
  7. MySQL 数据库渗透及漏洞利用总结
  8. 按规律摆放红蓝花盆(python)实现
  9. 深度学习-MATLAB数据增广
  10. 如果要让人听懂动物的语言,中间的代沟到底该怎么填平?