报错
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'activity_cover' from result set. Cause: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.util.ArrayList<java.lang.Object>` from Object value (token `JsonToken.START_OBJECT`)
at [Source: (String)"[{"filePath":"/minio-file/jdy-rd-dev/infra-common/1.jpg","isCover":true},{"filePath":"/minio-file/jdy-rd-dev/infra-common/1.jpg","isCover":false}]"; line: 1, column: 2] (through reference chain: java.util.ArrayList[0])
分析
很简单的通过没有mybatis-plus提供的raManger.getById(id)来查询竟然报错,日了够了。虽然我实体的列List有点厉害,但是我也指定ActivityCoverListTypeHandler了呀,怎么还报错!!
后来debug,发现查询的时候根本没进来ActivityCoverListTypeHandler,也就是说自定义的Handler没有生效,于是在yml文件手动指定
mybatis-plus:
type-handlers-package: com.nimbus.common.mybatis.typehandler.common
可是还是无效!!!
@Data
@TableName(value = "recruit_activity")
@EqualsAndHashCode(callSuper = true)
public class RecruitActivityEntity extends AbstractProjectBasedEntity {
/**
* 活动封面.
*/
@TableField(value = "activity_cover", typeHandler = ActivityCoverListTypeHandler.class)
private List<ActivityCoverVo> activityCover;
}
解决方案
mybatis-plus需要对复杂类型的字段声明自动化结果集映射,如果是xml就是自定义resultMap,如果是使用mybatis自带的方法,就需要实体类务必设置 autoResultMap = true
主要解决以下场景:
1、复杂类型字段映射:自动为包含@TableField(typeHandler = XxxTypeHandler)的字段生成
2、JSON/XML免配置:避免手动编写标签,特别适用于PostgreSQL的jsonb、几何类型等特殊字段
@Data
@TableName(value = "recruit_activity", autoResultMap = true)
@EqualsAndHashCode(callSuper = true)
public class RecruitActivityEntity extends AbstractProjectBasedEntity {
/**
* 活动封面.
*/
@TableField(value = "activity_cover", typeHandler = ActivityCoverListTypeHandler.class)
private List<ActivityCoverVo> activityCover;
}