关于mybatis一对一查询一对多查询遇到的问题
作者:roydon_Guo 时间:2022-01-26 05:25:12
springboot整合mybatis项目
博客系统
文章,相册,评论,标签,等表
IDEA为最新版2021.3.3,mysql数据库为最新版Navicat
(或许有些字段不支持特定的命名)
也是醉了,以前idea还是19版的,navicat也是老版本的时候mybatis关联查询mapper操作能正常运行,拿到相应字段,并封装,但最近写项目过程中遇到一个离谱的是,过了好久才发现,
当关联查询时,无论一对一还是一对多
除了需要注意javaType和ofType之外,还应该注意各表主键不能同一名称
实体类文章
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Article implements Serializable {
private Integer id;
private Integer authorId;
private String title;
private String content;
private Timestamp date;//private LocalDate date;
private Users user;
private List<Comment> commentList;
private List<ArticleTag> articleTagList;
}
接下来是对文章进行操作,要求查询全部文章,并关联查询作者,文章标签(一个文章多个标签)
先看一下我下面代码块的写法,注意一对一,一对多的各个实体查询时的id,都是拿的数据库的id字段,只有标签被我改为了tag_id
<select id="queryAllArticles" resultMap="ArticleList">
select a.*,u.*,t.*
from article a
join users u on a.authorid = u.id
left outer join article_tag t on a.id = t.article_id
</select>
<resultMap id="ArticleList" type="article">
<id column="id" property="id"/>
<result column="authorid" property="authorId"/>
<result column="title" property="title"/>
<result column="content" property="content"/>
<result column="date" property="date"/>
<association property="user" javaType="users">
<id column="id" property="id"/>
<result column="pet_name" property="petName"/>
<result column="headportrait" property="headPortrait"/>
</association>
<collection property="articleTagList" ofType="articleTag">
<id column="tag_id" property="tagId"/>
<result column="article_id" property="articleId"/>
<result column="tag_content" property="tag"/>
</collection>
</resultMap>
如下图
所以当我测试的时候能正常根据id拿到每张表的同一主键名称id吗
并不能拿到
控制台仅仅正确输出了标签list中的tag_id,没错,细心点,我也是最后才发现,它nn的,上面输出的user(文章作者)他的id封装错了,他的id是这篇文章的id,所以,sql我自此所有表的主键不都全命名为id了,防止此处再烙下病根,而且数据库字段在命名时,我发现字段名字为describe和tag都查不到数据,本来想简简单单命名的数据库表,也得上心了拜拜了~~改bug去了🥲😭🥲😭✍️✍️
///
看控制台输出/
我的用户表里就没有id为56的
来源:https://blog.csdn.net/m0_51390535/article/details/124958067
标签:mybatis,一对一查询,一对多查询
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
教你如何使用Java输出各种形状
2023-08-21 00:46:55
JFinal使用ajaxfileupload实现图片上传及预览
2023-08-05 08:30:48
Java基础之反射详解
2022-06-16 12:25:11
![](https://img.aspxhome.com/file/2023/5/61955_0s.png)
JAVASE系统实现抽卡功能
2023-11-19 19:49:41
![](https://img.aspxhome.com/file/2023/0/58700_0s.jpg)
Java类的定义以及执行顺序学习教程
2023-01-14 10:44:29
Java continue break制作简单聊天室程序
2022-12-20 01:23:01
MyBatis SpringMVC整合实现步骤详解
2023-02-02 20:33:07
Java中的main函数的详细介绍
2021-10-03 16:09:16
Java中JDBC连接数据库详解
2023-08-10 16:57:18
![](https://img.aspxhome.com/file/2023/2/58162_0s.png)
C#难点逐个击破(8):可空类型System.Nullable
2023-07-04 14:49:40
![](https://img.aspxhome.com/file/2023/2/71742_0s.png)
Spring@Value使用获取配置信息为null的操作
2021-08-01 23:46:27
带你了解Java的类和对象
2022-05-08 09:10:21
![](https://img.aspxhome.com/file/2023/8/61308_0s.png)
SpringBoot整合java诊断工具Arthas解读
2023-08-07 10:39:03
![](https://img.aspxhome.com/file/2023/0/69920_0s.png)
Java中避免NullPointerException的方法总结
2021-08-29 08:09:24
Java实现按权重随机数
2023-11-28 23:15:32
springboot bean循环依赖实现以及源码分析
2022-06-05 11:50:15
![](https://img.aspxhome.com/file/2023/9/61509_0s.png)
SpringBoot+Netty+WebSocket实现消息发送的示例代码
2023-08-16 00:02:52
Java 网络编程总结
2023-11-10 22:19:29
![](https://img.aspxhome.com/file/2023/1/59071_0s.png)
Springboot+AOP实现返回数据提示语国际化的示例代码
2021-08-18 19:49:12
![](https://img.aspxhome.com/file/2023/5/73125_0s.png)
Spring Cloud Config实现分布式配置中心
2022-04-25 16:28:07