当前位置: 首页 > 知识库问答 >
问题:

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

陶英纵
2024-07-16

在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个答案

梁丘安晏
2024-07-16

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类也要加对应的注解
 类似资料:
  • 我有一个代表学生实体的字符串: 学生实体类是: 对于解析字符串,我使用以下代码: 响应1是一个httpresponse的主体,它代表我来自描述的字符串。 例外情况:

  • 本文向大家介绍springboot+mybatis-plus实现内置的CRUD使用详解,包括了springboot+mybatis-plus实现内置的CRUD使用详解的使用技巧和注意事项,需要的朋友参考一下 springboot+mybatis-plus实现内置的CRUD使用详情,具体修改删除操作内容后文也有详细说明 mybatis-plus的特性 无侵入:只做增强不做改变,引入它不会对现有工程产

  • 本文向大家介绍mybatis-plus批处理IService的实现示例,包括了mybatis-plus批处理IService的实现示例的使用技巧和注意事项,需要的朋友参考一下 一、pom文件引入 二、Controller层 三、IService层(此处请确保继承的是 mybatisplus下的 IService,上述的UserInfoEntity为实体类) 四、ServiceImpl(UserIn

  • 问题内容: 为什么以下算法对我来说不停止?(str是我要搜索的字符串,findStr是我要寻找的字符串) 问题答案: 最后一行造成了问题。永远不会为-1,所以会有无限循环。可以通过将代码的最后一行移到if块中来解决此问题。

  • 本文向大家介绍delphi 字符串处理中的怪异现象与处理方式,包括了delphi 字符串处理中的怪异现象与处理方式的使用技巧和注意事项,需要的朋友参考一下 1, 怪异现象:字符串相加操作不正常! 以上代码,明显输出字符串应含有后缀“.jpg”,但实际输出却不含后缀(如下),字符串加法操作似乎不起作用了! 采用showMessage进行输出,看看结果如何? 结果仍是不显示字符串后缀,但可以看到字符串

  • 在 Bash 脚本中可以调用字符串处理工具 awk 来替换内置的字符串处理操作。 样例 10-6. 使用另一种方式来截取和定位子字符串 #!/bin/bash # substring-extraction.sh String=23skidoo1 # 012345678 Bash # 123456789 awk # 注意不同字符串索引系统: # Bash 中第一个字符

  • 本文向大家介绍IDEA项目使用SpringBoot+MyBatis-Plus的方法,包括了IDEA项目使用SpringBoot+MyBatis-Plus的方法的使用技巧和注意事项,需要的朋友参考一下 步骤如下: 1.打开IDEA 2.File—>new—> project 3.选择spring initializr—>Next 4.填写Grouphe和Artifact,选择Java version

  • 1. 前言 在spring-boot 集成 MyBatis小节中,我们介绍了如何在 spring-boot 中集成 MyBatis,MyBatis 虽然灵活,但是对于业务开发还略显不够。MyBatis-Plus 是国内开发者为 MyBatis 定制的一款增强工具,在不侵入 MyBatis 的基础上能够快速地提升 MyBatis 的开发能力,为开发者节省大量的时间。 提示: 本小节建立在spring