编辑
2024-06-09
随记
00
请注意,本文编写于 245 天前,最后修改于 245 天前,其中某些信息可能已经过时。

昨晚做这个系统的时候遇到了这个问题,让我一直有点疑惑,后面检查报错信息我发现了问题所在

其中主要的错误信息是

Type handler was null on parameter mapping for property 'judgeConfig'. It was either not specified and/or could not be found for the javaType (cn.pepedd.hpoj.entity.dto.question.JudgeConfig) : jdbcType (null) combination.

也就是说没有类型处理器来处理judgeConfig这个属性,因为我定义的是一个对象,而对象要转换到数据库字段中的varchar类型,MyBatis和MySQL都没有做这件事,需要开发者自己来做。

像复杂属性的值转换为数据库字段不仅仅只有对象,还有数组、集合等

因此解决办法就是在写入数据库之前进行一次转换,将复杂属性转为字符串。

我是这样处理的。

对于前端常见的dto->pojo->vo流程在BizQuestion(数据库实体类)下定义,以便于便捷的转换

java
/** * 使用了 列中的json字符串,需要单独的方法进行dto->pojo->vo的转换 * * @param questionDTO * @return */ public static BizQuestion dtoToPojo(QuestionDTO questionDTO) { BizQuestion bizQuestion = new BizQuestion(); BeanUtils.copyProperties(questionDTO, bizQuestion); // 对tags、judgeCase、judgeConfig单独处理 bizQuestion.setTags(JSONUtil.toJsonStr(questionDTO.getTags())); bizQuestion.setJudgeCase(JSONUtil.toJsonStr(questionDTO.getJudgeCase())); bizQuestion.setJudgeConfig(JSONUtil.toJsonStr(questionDTO.getJudgeConfig())); return bizQuestion; } /** * 使用了 列中的json字符串,需要单独的方法进行dto->pojo->vo的转换 * * @param bizQuestion * @return */ public static QuestionVO pojoToVo(BizQuestion bizQuestion) { QuestionVO questionVO = new QuestionVO(); BeanUtils.copyProperties(bizQuestion, questionVO); // 对tags、judgeCase、judgeConfig单独处理 questionVO.setTags(JSONUtil.parseArray(bizQuestion.getTags()).toList(String.class)); questionVO.setJudgeCase(JSONUtil.parseArray(bizQuestion.getJudgeCase()).toList(JudgeCase.class)); JSONConfig jsonConfig = JSONConfig.create(); jsonConfig.setIgnoreNullValue(false); questionVO.setJudgeConfig(JSONUtil.parseObj(bizQuestion.getJudgeConfig(), jsonConfig).toBean(JudgeConfig.class)); return questionVO; }

对于系统内对db->obj->db的转换,定义完整的实体类Question,并在其中定义db->obj->db转换的方法

java
/** * 使用了 列中的json字符串,需要单独的方法进行转换 * * @param question * @return */ public static BizQuestion objToDb(Question question) { BizQuestion bizQuestion = new BizQuestion(); BeanUtils.copyProperties(question, bizQuestion); // 对tags、judgeCase、judgeConfig单独处理 bizQuestion.setTags(JSONUtil.toJsonStr(question.getTags())); bizQuestion.setJudgeCase(JSONUtil.toJsonStr(question.getJudgeCase())); bizQuestion.setJudgeConfig(JSONUtil.toJsonStr(question.getJudgeConfig())); return bizQuestion; } /** * 使用了 列中的json字符串,需要单独的方法进行转换 * * @param bizQuestion * @return */ public static Question dbToObj(BizQuestion bizQuestion) { Question question = new Question(); BeanUtils.copyProperties(bizQuestion, question); // 对tags、judgeCase、judgeConfig单独处理 question.setTags(JSONUtil.parseArray(bizQuestion.getTags()).toList(String.class)); question.setJudgeCase(JSONUtil.parseArray(bizQuestion.getJudgeCase()).toList(JudgeCase.class)); JSONConfig jsonConfig = JSONConfig.create(); jsonConfig.setIgnoreNullValue(false); question.setJudgeConfig(JSONUtil.parseObj(bizQuestion.getJudgeConfig(), jsonConfig).toBean(JudgeConfig.class)); return question; }

其他情况以此类推。

它在代码中的使用也是相当简单,就像这样转换即可。

这样就能在接口中灵活的转换了

比如像这个接口,原本如果是查询数据库返回数据库实体类的话,复杂属性就会变成字符串,而使用单独的类和方法进行转换,就能得到完整的值

如果对你有用的话,可以打赏哦
打赏
ali pay
wechat pay

本文作者:peepdd864

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!