在springboot中使用MyBatis-Plus处理JSON字符串时出现的异常?

新手上路,请多包涵

在springboot中使用MyBatis-Plus处理JSON字符串时出现的异常

数据库的user表中有一个info字段,是JSON类型;目前User实体类中却是String类型;为了解决这个问题我使用了MybatisPlus中的JacksonTypeHandler处理器所以我定义了单独实体类来与info字段的属性匹配,

@Data
public class UserInfo {
    private Integer age;
    private String intro;
    private String gender;
}

然后我将User类的info字段修改为UserInfo类型,并声明类型处理器

@Data

@TableName(autoResultMap = true)//开启自动映射
public class User {
    private Long id;
    private String username;
    private String password;
    private String phone;
      @TableField(typeHandler = JacksonTypeHandler.class)
    private UserInfo info;
    private UserStatus status;
    private Integer balance;
    private LocalDateTime createTime;
    private LocalDateTime updateTime;
}

为了让页面返回的结果也以对象格式返回,修改UserVO中的info字段:

@Data
@ApiModel(description = "用户VO实体")
public class UserVO {

    @ApiModelProperty("用户id")
    private Long id;

    @ApiModelProperty("用户名")
    private String username;

    @ApiModelProperty("详细信息")
    private UserInfo info;

    @ApiModelProperty("使用状态(1正常 2冻结)")
    private UserStatus status;

    @ApiModelProperty("账户余额")
    private Integer balance;

    @ApiModelProperty("用户的收获地址")
    private List<AddressVO> address;
}

然后开始运行,好家伙直接报错

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController' defined in file [F:\springcloud\mp-demo\target\classes\com\itheima\mp\controller\UserController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userMapper' defined in file [F:\springcloud\mp-demo\target\classes\com\itheima\mp\mapper\UserMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is java.io.IOException: Failed to parse mapping resource: 'file [F:\springcloud\mp-demo\target\classes\mapper\IUserService.xml]'

我把User中的info改回String类型,不报错,但是查询出错500因为跟用户VO实体UserVO中的info对不上,总的来说就是User中的info不能用UserInfo,一运行就报错,写进去啥事没有

阅读 1.6k
1 个回答

MyBatis PlusMyBatis Flex这种增强型框架,都是在原有MyBatis的功能上做了增强,需要在项目启动时,增强原生MyBatis的扫描步骤,例如扫描@TableField等注解,做typeHandler和实体类属性的映射,但原生MyBatis在启动时并未关注到 typeHandler和表与实体类的映射关系,也不会扫描注解,仅会扫描Mapper的xml文件中的resultMap标签,里面有columntypeHandler的对应关系

原生MyBatis要求typeHandler与类型是一对一关系,但是JacksonTypeHandler这种拓展的typeHandler实际上与类型是一对多关系,因为缺少jdbcTypejavaType

修改建议:
1.User类中使用@TableField指定typeHandler没有问题,但是如果自定义了列inforesultMap或者有使用#{}手动的SQL,需要指定typeHandler
例如<result column="info" property="info" jdbcType="OTHER" typeHandler="cn.xxx.JacksonTypeHandler" />#{info, typeHandler=BigDecimalTypeHandler}

2.UserVO类不要作用到xml的返回值上

  • 使用实体User类查询,用java代码映射到UserVO
  • 非要使用UserVO类查询,UserVO类也要加对应的注解
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题