设为首页 加入收藏

TOP

关于在 Mybatis 中使用 record 关键字来定义 JavaBean
2023-07-25 21:23:11 】 浏览:35
Tags:关于在 Mybatis record 关键字 JavaBean

经测试,正常情况下使用 record 是没有问题的,但若是使用了 resultMap,将会导致错误:

There is no setter for property named 'xxx' in 'xxx'
argument type mismatch

首先, record 类型没有无参构造函数,所以在反射过程中无法创建对应类型,导致了argument type mismatch错误。

那如果给 record 类型的类加上无参构造函数呢?

会出现以下错误:

There is no setter for property named 'xxx' in 'xxx'

可以看到 Mybatis 可以找到对应的类了,但是仍然会报There is no setter for property named 'xxx' in 'xxx',这是由于 record 类型中所有的变量均为 final 类型,record 也并没有生成 set 方法导致的。

所以总的来说,虽然 record 可以极大的简化 Bean 的编写,并且可以摆脱 Lombok,但是其特性决定了它并不适合用来编写 Mybatis 的 Bean。
除非你的 Bean 的属性只有基本类型(笑


resultMap 部分:

<select id="getTeacher" resultType="Teacher">
    select * from mybatis.teacher where id = #{tid}
</select>

<resultMap id="studentMapper" type="Student">
    <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
</resultMap>

<select id="getStudents" resultMap="studentMapper">
    select * from mybatis.student;
</select>
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇JavaFx 使用字体图标记录 下一篇Java注解(2):实现自己的ORM

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目