• 细节决定成败:探究Mybatis中javaType和ofType的区别


    一. 背景描述

    今天,壹哥给学生讲解了Mybatis框架,学习了基础的ORM框架操作及多对一的查询。在练习的时候,小张同学突然举手求助,说在做预习作业使用一对多查询时,遇到了ReflectionException异常

    二. 情景再现

    1. 实体类

    为了给大家讲清楚这个异常的产生原因,壹哥先列出今天案例中涉及到的两张表:书籍表和书籍类型表。这两张表中存在着简单的多对一关系,实体类如下:

    1. @Data
    2. @NoArgsConstructor
    3. @AllArgsConstructor
    4. @Builder
    5. public class Book {
    6. private Integer id;
    7. private String name;
    8. private String author;
    9. private String bookDesc;
    10. private String createTime;
    11. private BookType type;
    12. private String imgPath;
    13. }
    14. @Data
    15. @NoArgsConstructor
    16. @AllArgsConstructor
    17. @Builder
    18. public class BookType {
    19. private Integer id;
    20. private String name;
    21. }

    2.BookMapper.xml映射文件

    上课时,壹哥讲解的关联查询是通过查询书籍信息,并同时对书籍类型查询。即在查询Book对象时i,同时查询出BookType对象。BookMapper.xml映射文件如下:

    1. "1.0" encoding="UTF-8"?>
    2. mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    3. <mapper namespace="com.qf.day7.dao.BookDAO">
    4. <resultMap id="booksMap" type="com.qf.day7.entity.Books">
    5. <id property="id" column="id">id>
    6. <result property="name" column="name">result>
    7. <result property="author" column="author">result>
    8. <result property="bookDesc" column="book_desc">result>
    9. <result property="createTime" column="create_time">result>
    10. <result property="imgPath" column="img_path">result>
    11. <association property="type" javaType="com.qf.day7.entity.BookType">
    12. <id property="id" column="type_id">id>
    13. <result property="name" column="type_name">result>
    14. association>
    15. resultMap>
    16. <select id="findAll" resultMap="booksMap">
    17. SELECT
    18. b.id,
    19. b.`name`,
    20. b.author,
    21. b.book_desc,
    22. b.create_time,
    23. b.img_path,
    24. t.id type_id,
    25. t.`name` type_name
    26. FROM
    27. books AS b
    28. INNER JOIN book_type AS t ON b.type_id = t.id
    29. select>
    30. mapper>

    3. 核心配置

    核心配置文件如下:mybatisCfg.xml

    1. "1.0" encoding="UTF-8" ?>
    2. configuration
    3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
    5. <configuration>
    6. <typeAliases>
    7. <package name="com.qf.day7.entity"/>
    8. typeAliases>
    9. <environments default="development">
    10. <environment id="development">
    11. <transactionManager type="JDBC">transactionManager>
    12. <dataSource type="POOLED">
    13. <property name="driver" value="com.mysql.jdbc.Driver"/>
    14. <property name="url"
    15. value="jdbc:mysql://localhost:3306/books?useUnicode=true&characterEncoding=utf-8&useSSL=false"/>
    16. <property name="username" value="root"/>
    17. <property name="password" value="root"/>
    18. dataSource>
    19. environment>
    20. environments>
    21. <mappers>
    22. <mapper resource="mapper/BookMapper.xml">mapper>
    23. <mapper resource="mapper/BookTypeMapper.xml">mapper>
    24. mappers>
    25. configuration>

    4. 测试代码

    接着我们对上面的配置进行测试。

    1. public class BookDAOTest {
    2. private SqlSessionFactory factory;
    3. @Before
    4. public void setUp() throws Exception {
    5. final InputStream inputStream = Resources.getResourceAsStream("mybatisCfg.xml");
    6. factory = new SqlSessionFactoryBuilder().build(inputStream);
    7. }
    8. @Test
    9. public void findAll() {
    10. final SqlSession session = factory.openSession();
    11. final BookDAO bookDAO = session.getMapper(BookDAO.class);
    12. final List list = bookDAO.findAll();
    13. list.stream().forEach(System.out::println);
    14. session.close();
    15. }
    16. }

    学生按照我讲的内容,测试没有问题。在后续的预习练习中,要求实现在BookType中添加List属性books,在查询BookType对象同时将该类型的Book对象集合查出。小张同学有了如下实现思路。

    5. 修改实体类

    1. @Data
    2. @NoArgsConstructor
    3. @AllArgsConstructor
    4. @Builder
    5. public class Book {
    6. private Integer id;
    7. private String name;
    8. private String author;
    9. private String bookDesc;
    10. private String createTime;
    11. private String imgPath;
    12. }
    13. @Data
    14. @NoArgsConstructor
    15. @AllArgsConstructor
    16. @Builder
    17. public class BookType {
    18. private Integer id;
    19. private String name;
    20. private List books;
    21. }

    6. 添加映射文件BookTypeMapper.xml

    1. "1.0" encoding="UTF-8"?>
    2. mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    3. <mapper namespace="com.qf.day7.dao.BookTypeDAO">
    4. <resultMap id="bookTypeMap" type="com.qf.day7.entity.BookType">
    5. <id column="id" property="id">id>
    6. <result column="name" property="name">result>
    7. <collection property="books" javaType="com.qf.day7.entity.Book">
    8. <id property="id" column="book_id">id>
    9. <result property="name" column="book_name">result>
    10. <result property="author" column="author">result>
    11. <result property="bookDesc" column="book_desc">result>
    12. <result property="createTime" column="create_time">result>
    13. <result property="imgPath" column="img_path">result>
    14. collection>
    15. resultMap>
    16. <select id="findById" resultMap="bookTypeMap">
    17. SELECT
    18. b.id book_id,
    19. b.`name` book_name,
    20. b.author,
    21. b.book_desc,
    22. b.create_time,
    23. b.img_path,
    24. t.id,
    25. t.`name`
    26. FROM
    27. books AS b
    28. INNER JOIN book_type AS t ON b.type_id = t.id
    29. where t.id = #{typeId}
    30. select>
    31. mapper>

    7. 编写测试类

    1. public class BookTypeDAOTest {
    2. private SqlSessionFactory factory;
    3. @Before
    4. public void setUp() throws Exception {
    5. final InputStream inputStream = Resources.getResourceAsStream("mybatisCfg.xml");
    6. factory = new SqlSessionFactoryBuilder().build(inputStream);
    7. }
    8. @Test
    9. public void findById() {
    10. final SqlSession session = factory.openSession();
    11. final BookTypeDAO bookTypeDAO = session.getMapper(BookTypeDAO.class);
    12. BookType bookType = bookTypeDAO.findById(1);
    13. for (Book book : bookType.getBooks()) {
    14. System.out.println(book.getName());
    15. }
    16. session.close();
    17. }

    然后就出现了一开始提到的异常:

    1. org.apache.ibatis.exceptions.PersistenceException:
    2. ### Error querying database. Cause: org.apache.ibatis.reflection.ReflectionException: Could not set property 'books' of 'class com.qf.day7.entity.BookType' with value 'Book(id=1, name=Java从入门到精通, author=千锋, bookDesc=很不错的Java书籍, createTime=2022-05-27, type=null, imgPath=174cc662fccc4a38b73ece6880d8c07e)' Cause: java.lang.IllegalArgumentException: argument type mismatch
    3. ### The error may exist in mapper/BookTypeMapper.xml
    4. ### The error may involve com.qf.day7.dao.BookTypeDAO.findById
    5. ### The error occurred while handling results
    6. ### SQL: SELECT b.id book_id, b.`name` book_name, b.author, b.book_desc, b.create_time, b.img_path, t.id, t.`name` FROM books AS b INNER JOIN book_type AS t ON b.type_id = t.id where t.id = ?
    7. ### Cause: org.apache.ibatis.reflection.ReflectionException: Could not set property 'books' of 'class com.qf.day7.entity.BookType' with value 'Book(id=1, name=Java从入门到精通, author=千锋, bookDesc=很不错的Java书籍, createTime=2022-05-27, type=null, imgPath=174cc662fccc4a38b73ece6880d8c07e)' Cause: java.lang.IllegalArgumentException: argument type mismatch

    三. 异常分析

    上面的异常提示说在BookType类中的books属性设置有问题我们来仔细查看一下代码,发现是因为直接复制了之前的关系配置,在配置文件中使用javaType节点但正确的应该使用ofType如下图所示:

    四. 解析

    那么为什么有的关系配置要使用javaType,而有的地方又要使用ofType呢?

    这我们就不得不说说Mybatis的底层原理了!在关联映射中,如果是单个的JavaBean对象,那么可以使用javaType;而如果是集合类型,则需要写ofType。以下是Mybatis的官方文档原文:

    五. 结尾

    虽然上面的代码中只是因为一个单词的不同,却造成了不小的错误。我们的程序是严格的,小问题就可能会耽误你很久的时间。就比如我们的小张同学,在求助壹哥之前已经找bug找了一个小时......最后壹哥一眼就给他看出了问题所在,他都无语凝噎了.....

    现在你明白javaType和ofType用法上的区别了吗?如果你还有其他什么问题,可以在评论区留言或私信哦!关注Java架构栈,干货天天都不断。

  • 相关阅读:
    Kotlin 协程再探之为什么使用协程反而更慢了?
    【BackEnd--Mongodb】学习笔记(完整详细版)
    LVGL介绍
    猿创征文|瑞吉外卖——移动端_笔记
    SQL 的执行流程是什么样的
    前端-(6)
    SpringBoot 整合RabbitMQ 之延迟队列实验
    业务数据分析-Excel的基础操作(一)
    makkefile文件自动化编译以及基础文件命令(补)
    ansible中常用模块
  • 原文地址:https://blog.csdn.net/syc000666/article/details/127663921