详解Mybatis中javaType和ofType的区别

一. 背景描述

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

二. 情景再现

1. 实体类

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

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Book {
    private Integer id;
    private String name;
    private String author;
    private String bookDesc;
    private String createTime;
    private BookType type;
    private String imgPath;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class BookType {
    private Integer id;
    private String name;
}

2.BookMapper.xml映射文件

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

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qf.day7.dao.BookDAO">
    <resultMap id="booksMap" type="com.qf.day7.entity.Books">
        <id property="id" column="id"></id>
        <result property="name" column="name"></result>
        <result property="author" column="author"></result>
        <result property="bookDesc" column="book_desc"></result>
        <result property="createTime" column="create_time"></result>
        <result property="imgPath" column="img_path"></result>
        <!--        单个对象的关联,javaType是指实体类的类型-->
        <association property="type" javaType="com.qf.day7.entity.BookType">
            <id property="id" column="type_id"></id>
            <result property="name" column="type_name"></result>
        </association>
    </resultMap>
    <select id="findAll" resultMap="booksMap">
        SELECT
        b.id,
        b.`name`,
        b.author,
        b.book_desc,
        b.create_time,
        b.img_path,
        t.id type_id,
        t.`name` type_name
        FROM
        books AS b
        INNER JOIN book_type AS t ON b.type_id = t.id
    </select>
</mapper>

3. 核心配置

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

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <typeAliases>
        <package name="com.qf.day7.entity"/>
    </typeAliases>
    <environments default="development">
        <environment id="development">
<!--            事务管理器-->
            <transactionManager type="JDBC"></transactionManager>
<!--            使用mybatis自带连接池-->
            <dataSource type="POOLED">
<!--                jdbc四要素-->
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url"
                          value="jdbc:mysql://localhost:3306/books?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mapper/BookMapper.xml"></mapper>
        <mapper resource="mapper/BookTypeMapper.xml"></mapper>
    </mappers>
</configuration>

4. 测试代码

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

public class BookDAOTest {
    private SqlSessionFactory factory;
    @Before
    public void setUp() throws Exception {
        final InputStream inputStream = Resources.getResourceAsStream("mybatisCfg.xml");
        factory = new SqlSessionFactoryBuilder().build(inputStream);
    }
    @Test
    public void findAll() {
        final SqlSession session = factory.openSession();
        final BookDAO bookDAO = session.getMapper(BookDAO.class);
        final List<Book> list = bookDAO.findAll();
        list.stream().forEach(System.out::println);
        session.close();
    }

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

5. 修改实体类

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Book {
    private Integer id;
    private String name;
    private String author;
    private String bookDesc;
    private String createTime;
    private String imgPath;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class BookType {
    private Integer id;
    private String name;
    private List<Book> books;
}

6. 添加映射文件BookTypeMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qf.day7.dao.BookTypeDAO">
    <resultMap id="bookTypeMap" type="com.qf.day7.entity.BookType">
        <id column="id" property="id"></id>
        <result column="name" property="name"></result>
        <collection property="books" javaType="com.qf.day7.entity.Book">
            <id property="id" column="book_id"></id>
            <result property="name" column="book_name"></result>
            <result property="author" column="author"></result>
            <result property="bookDesc" column="book_desc"></result>
            <result property="createTime" column="create_time"></result>
            <result property="imgPath" column="img_path"></result>
        </collection>
    </resultMap>
    <select id="findById" resultMap="bookTypeMap">
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 = #{typeId}
    </select>
</mapper>

7. 编写测试类

public class BookTypeDAOTest {
    private SqlSessionFactory factory;
    @Before
    public void setUp() throws Exception {
        final InputStream inputStream = Resources.getResourceAsStream("mybatisCfg.xml");
        factory = new SqlSessionFactoryBuilder().build(inputStream);
    }
    @Test
    public void findById() {
        final SqlSession session = factory.openSession();
        final BookTypeDAO bookTypeDAO = session.getMapper(BookTypeDAO.class);
        BookType bookType = bookTypeDAO.findById(1);
        for (Book book : bookType.getBooks()) {
            System.out.println(book.getName());
        }
        session.close();
    }

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

org.apache.ibatis.exceptions.PersistenceException: 
### 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
### The error may exist in mapper/BookTypeMapper.xml
### The error may involve com.qf.day7.dao.BookTypeDAO.findById
### The error occurred while handling results
### 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 = ?
### 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架构栈,干货天天都不断。

​到此这篇关于详解Mybatis中javaType和ofType的区别的文章就介绍到这了,更多相关Mybatis javaType和ofType内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!

本文标题为:详解Mybatis中javaType和ofType的区别

基础教程推荐