解决Mybatis-plus自定义TypeHandler查询映射结果一直为null问题
踩坑背景
需求
将实体类中的JSONObject对象与MySQL数据库中VARCHAR类型进行映射
//实体类
@TableName(autoResultMap = true)
@Data
public class ImportItem implements Serializable {
...
@ExcelIgnore
@TableField(typeHandler = Fastjson2TypeHandler.class , value = "dynamic_information")
private JSONObject dynamicInformation;
...
}
//自定义类Fastjson2TypeHandler
@MappedTypes({JSONObject.class})
@MappedJdbcTypes({JdbcType.VARCHAR})
public class Fastjson2TypeHandler extends AbstractJsonTypeHandler<JSONObject> {
private static final Logger log = LoggerFactory.getLogger(Fastjson2TypeHandler.class);
private final Class<JSONObject> type = JSONObject.class;
public Fastjson2TypeHandler() {
log.trace("Fastjson2TypeHandler({})", type);
}
@Override
protected JSONObject parse(String json) {
return JSON.parseObject(json, this.type);
}
@Override
protected String toJson(JSONObject obj) {
return obj != null ? obj.toJSONString() : null;
}
}
//自定义查询语句
public interface ImportItemMapper extends MyMapper<ImportItem> {
@Select("<script>"
+ "SELECT * from import_item \n"
+ "WHERE company_id=#{companyId} \n"
+ "<if test='batchNumber!= null and batchNumber!=\"\" '>AND batch_number=#{batchNumber}</if> \n"
+ "<if test='mark!= null'>AND mark=#{mark}</if> \n"
+ "${params.dataScope}"
+ "</script>")
List<ImportItem> getByBatchNumber(ItemQuery itemQuery);
}
此时在操作的时候增删改都没为问题,但是调用自定义的查询接口的时候dynamicInformation始终查询结果为空,通过开启Mybatis-plus日志打印发现sql查询结果是正常的,因此判断是自定义的Fastjson2TypeHandler没有生效。
问题排查
1、实体类注解
@TableName(autoResultMap = true)
2、需要映射处理的字段需要添加注解
@TableField(typeHandler = Fastjson2TypeHandler.class , value = "dynamic_information") private JSONObject dynamicInformation;
3、各种路径问题,字段拼写排查确保正确
4、可以尝试xml中resultMap使用 typeHandler 属性
此时问题依旧存在,于是怀疑ImportItemMapper接口问题,由于之前一直没有使用xml文件,于是决定尝试使用xml
public interface ImportItemMapper extends MyMapper<ImportItem> {
List<ImportItem> getByBatchNumber(ItemQuery itemQuery);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.plm.mapper.ImportItemMapper">
<resultMap id="BaseResultMap" type="com.ruoyi.plm.model.entity.ImportItem">
<id column="dynamic_information" jdbcType="VARCHAR" property="dynamicInformation" typeHandler="com.ruoyi.plm.util.Fastjson2TypeHandler"/>
</resultMap>
<select id="getByBatchNumber" resultMap="BaseResultMap" parameterType="com.ruoyi.system.api.model.plm.ItemQuery">
SELECT *
FROM import_item
WHERE company_id = #{companyId}
<if test="batchNumber != null and !batchNumber.isEmpty()">
AND batch_number = #{batchNumber}
</if>
<if test="mark != null">
AND mark = #{mark}
</if>
${params.dataScope}
</select>
</mapper>
此时再次尝试后发现问题已经解决,既然这种方式可以解决问题,那么很有可能是因为自定义 TypeHandler 没能正确注册到 MyBatis,因此可以尝试放弃使用xml选则最后一种方式
5、 注册自定义TypeHandler到 MyBatis
@Configuration
public class MyBatisConfig {
@Bean
public ConfigurationCustomizer mybatisConfigurationCustomizer() {
return configuration -> {
// 注册自定义 TypeHandler
configuration.getTypeHandlerRegistry().register(JSONObject.class, JdbcType.VARCHAR, new Fastjson2TypeHandler());
};
}
}
经测试注册后结合注解自定义sql语句查询一切正常,问题完美解决。
总结
出现增删改数据正常,但查询结果为null,很有可能是自定义的TypeHandler未生效,可以参考上述4或5的方法来解决问题。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
- MyBatis自定义TypeHandler实现字段加密解密
- MyBatis类型处理器TypeHandler的作用及说明
- MyBatis中TypeHandler的使用教程详解
- MyBatis-Plus 中 typeHandler 的使用实例详解
- SpringBoot中MyBatis使用自定义TypeHandler的实现
- Mybatis中TypeHandler使用小结
- Mybatis的TypeHandler实现数据加解密详解
- Mybatis中自定义TypeHandler处理枚举的示例代码
- MyBatisPlus自定义JsonTypeHandler实现自动转化JSON问题
- MyBatis自定义TypeHandler如何解决字段映射问题
- MyBatis中TypeHandler基本用法与示例
相关文章
springboot 中 inputStream 神秘消失之谜(终破)
这篇文章主要介绍了springboot 中 inputStream 神秘消失之谜,为了能够把这个问题说明,我们首先需要从简单的http调用说起,通过设置body等一些操作,具体实现代码跟随小编一起看看吧2021-08-08
spring boot项目如何采用war在tomcat容器中运行
这篇文章主要介绍了spring boot项目如何采用war在tomcat容器中运行呢,主要讲述将SpringBoot打成war包并放入tomcat中运行的方法分享,需要的朋友可以参考下2022-11-11
SpringBoot3.2.2整合MyBatis Plus3.5.5的详细过程
这篇文章给大家介绍了SpringBoot3.2.2整合MyBatis Plus3.5.5的详细过程,文中通过代码示例给大家介绍的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下2024-01-01
详谈Map的key、value值的数据类型不能为基本类型的原因
这篇文章主要介绍了详谈Map的key、value值的数据类型不能为基本类型的原因,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2020-09-09
SpringBoot配置多个数据源超简单步骤(连接多个数据库)
公司项目有连接多个不同数据库的需求,特研究了一下,根据网上的资料,这篇文章主要给大家介绍了关于SpringBoot配置多个数据源(连接多个数据库)的相关资料,需要的朋友可以参考下2024-05-05


最新评论