在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,一运行就报错,写进去啥事没有
像
MyBatis Plus
和MyBatis Flex
这种增强型框架,都是在原有MyBatis
的功能上做了增强,需要在项目启动时,增强原生MyBatis
的扫描步骤,例如扫描@TableField
等注解,做typeHandler
和实体类属性的映射,但原生MyBatis
在启动时并未关注到typeHandler
和表与实体类的映射关系,也不会扫描注解,仅会扫描Mapper的xml文件中的resultMap
标签,里面有column
与typeHandler
的对应关系原生
MyBatis
要求typeHandler
与类型是一对一关系,但是JacksonTypeHandler
这种拓展的typeHandler
实际上与类型是一对多关系,因为缺少jdbcType
和javaType
修改建议:
1.
User
类中使用@TableField
指定typeHandler
没有问题,但是如果自定义了列info
的resultMap
或者有使用#{}
手动的SQL,需要指定typeHandler
,例如
<result column="info" property="info" jdbcType="OTHER" typeHandler="cn.xxx.JacksonTypeHandler" />
和#{info, typeHandler=BigDecimalTypeHandler}
2.
UserVO
类不要作用到xml的返回值上User
类查询,用java代码映射到UserVO
类UserVO
类查询,UserVO
类也要加对应的注解