Mybatis【18】-- Mybatis自关联多对一查询方式
注:代码已托管在GitHub上,地址是:https://github.com/Damaer/Mybatis-Learning ,项目是mybatis-14-oneself-many2one,需要自取,需要配置maven环境以及mysql环境(sql语句在resource下的test.sql中),觉得有用可以点个小星星。
docsify文档地址在:https://damaer.github.io/Mybatis-Learning/#/
现在有个数据库查询需求,给出当前新闻栏目的id,希望查出父辈栏目,父辈的父辈栏目等等信息。
数据表设计如下:

实体类设计:
package beans;
import java.util.Set;
public class NewsLabel {
private Integer id;
private String name;
private NewsLabel parent;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public NewsLabel getParent() {
return parent;
}
@Override
public String toString() {
return "NewsLabel [id=" + id + ", name=" + name + ", parent=" + parent
+ "]";
}
public void setParent(NewsLabel parent) {
this.parent = parent;
}
}
sql查询接口定义:
public interface INewsLabelDao {
NewsLabel selectParentByParentId(int pid);
}
sql定义在mapper.xml文件中,可以看到,我们查询的时候调用的是id为“selectParentByParentId”的sql,返回结果做了一个映射(resultMap),resultMap的id是“newsLabelMapper”,“newsLabelMapper”中除了id映射和name映射,还有一个<association/>,里面定义的是关联关系定义。
property="parent":表示映射的属性是parentjavaType="NewsLabel":表示映射的类型是NewsLabelcolumn="pid":使用pid作为参数传递进去再次查询。select="selectParentByParentId":查询parent属性执行的语句
<?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="dao.INewsLabelDao">
<resultMap type="NewsLabel" id="newsLabelMapper">
<id column="id" property="id"/>
<result column="name" property="name"/>
<association property="parent"
javaType="NewsLabel"
select="selectParentByParentId"
column="pid"/>
</resultMap>
<select id="selectParentByParentId" resultMap="newsLabelMapper">
select id,name,pid from newslabel where id=#{xxx}
</select>
</mapper>
测试类:
public class MyTest {
private INewsLabelDao dao;
private SqlSession sqlSession;
@Before
public void Before(){
sqlSession=MyBatisUtils.getSqlSession();
dao=sqlSession.getMapper(INewsLabelDao.class);
}
@Test
public void TestselectMinisterById(){
NewsLabel children=dao.selectParentByParentId(7);
System.out.println(children);
}
@After
public void after(){
if(sqlSession!=null){
sqlSession.close();
}
}
}
查询出来结果:
[service] 2018-07-16 11:54:10,123 - dao.INewsLabelDao.selectParentByParentId -683 [main] DEBUG dao.INewsLabelDao.selectParentByParentId - ==> Preparing: select id,name,pid from newslabel where id=?
[service] 2018-07-16 11:54:10,154 - dao.INewsLabelDao.selectParentByParentId -714 [main] DEBUG dao.INewsLabelDao.selectParentByParentId - ==> Parameters: 7(Integer)
[service] 2018-07-16 11:54:10,174 - dao.INewsLabelDao.selectParentByParentId -734 [main] DEBUG dao.INewsLabelDao.selectParentByParentId - ====> Preparing: select id,name,pid from newslabel where id=?
[service] 2018-07-16 11:54:10,174 - dao.INewsLabelDao.selectParentByParentId -734 [main] DEBUG dao.INewsLabelDao.selectParentByParentId - ====> Parameters: 4(Integer)
[service] 2018-07-16 11:54:10,181 - dao.INewsLabelDao.selectParentByParentId -741 [main] DEBUG dao.INewsLabelDao.selectParentByParentId - ======> Preparing: select id,name,pid from newslabel where id=?
[service] 2018-07-16 11:54:10,181 - dao.INewsLabelDao.selectParentByParentId -741 [main] DEBUG dao.INewsLabelDao.selectParentByParentId - ======> Parameters: 2(Integer)
[service] 2018-07-16 11:54:10,183 - dao.INewsLabelDao.selectParentByParentId -743 [main] DEBUG dao.INewsLabelDao.selectParentByParentId - ========> Preparing: select id,name,pid from newslabel where id=?
[service] 2018-07-16 11:54:10,183 - dao.INewsLabelDao.selectParentByParentId -743 [main] DEBUG dao.INewsLabelDao.selectParentByParentId - ========> Parameters: 0(Integer)
[service] 2018-07-16 11:54:10,184 - dao.INewsLabelDao.selectParentByParentId -744 [main] DEBUG dao.INewsLabelDao.selectParentByParentId - <======== Total: 0
[service] 2018-07-16 11:54:10,184 - dao.INewsLabelDao.selectParentByParentId -744 [main] DEBUG dao.INewsLabelDao.selectParentByParentId - <====== Total: 1
[service] 2018-07-16 11:54:10,184 - dao.INewsLabelDao.selectParentByParentId -744 [main] DEBUG dao.INewsLabelDao.selectParentByParentId - <==== Total: 1
[service] 2018-07-16 11:54:10,184 - dao.INewsLabelDao.selectParentByParentId -744 [main] DEBUG dao.INewsLabelDao.selectParentByParentId - <== Total: 1
NewsLabel [id=7, name=北京金瓯, parent=NewsLabel [id=4, name=CBA, parent=NewsLabel [id=2, name=体育新闻, parent=null]]]
【作者简介】:
秦怀,公众号【秦怀杂货店】作者,技术之路不在一时,山高水长,纵使缓慢,驰而不息。求个 赞 和 在看 ,对我,是莫大的鼓励和认可,让我更有动力持续写出好文章。
Mybatis【18】-- Mybatis自关联多对一查询方式的更多相关文章
- myBatis系列之四:关联数据的查询
myBatis系列之三:增删改查是基于单表的查询,如果联表查询,返回的是复合对象,需要用association关键字来处理. 如User发表Article,每个用户可以发表多个Article,他们之间 ...
- mybatis 使用resultMap实现关联数据的查询(association 和collection )
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "- ...
- mybatis实战教程二:多对一关联查询(一对多)
多对一关联查询 一.数据库关系.article表和user表示多对一的关系 CREATE TABLE `article` ( `id` ) NOT NULL AUTO_INCREMENT, `user ...
- MyBatis 高级查询之多对多查询(十一)
高级查询之多对多查询 查询条件:根据玩家名,查询游戏信息 我们在之前创建的映射器接口 GameMapper.java 中添加接口方法,如下: /** * 根据玩家名查询游戏 * @param name ...
- mybatis实战教程(mybatis in action)之四:实现关联数据的查询
有了前面几章的基础,对一些简单的应用是可以处理的,但在实际项目中,经常是关联表的查询,比如最常见到的多对一,一对多等.这些查询是如何处理的呢,这一讲就讲这个问题.我们首先创建一个Article 这个表 ...
- MyBatis 实践 -动态SQL/关联查询
MyBatis 实践 标签: Java与存储 动态SQL 动态SQL提供了对SQL语句的灵活操作,通过表达式进行判断,对SQL进行拼接/组装. if 对查询条件进行判断,如果输入参数不为空才进行查询条 ...
- mybatis(一、原理,一对多,多对一查询)
MyBatis框架及原理分析 MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架,其主要就完成2件事情: 封装JDBC操作 利用反射打通Java类与SQL语句之间的相互转换 ...
- SpringBoot使用Mybatis注解进行一对多和多对多查询(2)
SpringBoot使用Mybatis注解进行一对多和多对多查询 GitHub的完整示例项目地址kingboy-springboot-data 一.模拟的业务查询 系统中的用户user都有唯一对应的地 ...
- Mybatis表关联多对一
在上章的 一对多 中,我们已经学习如何在 Mybatis 中关联多表,但在实际项目中也是经常使用 多对一 的情况,这些查询是如何处理的呢,在这一节中我们来学习它.多表映射的多对一关系要用到 mybit ...
- 4. mybatis实战教程(mybatis in action)之四:实现关联数据的查询
转自:https://www.cnblogs.com/shanheyongmu/p/5653599.html 有了前面几章的基础,对一些简单的应用是可以处理的,但在实际项目中,经常是关联表的查询,比如 ...
随机推荐
- C语言linux系统fork函数
References: c语言fork函数 linux中fork()函数详解 一.fork函数简介 作用 在linux下,C语言创建进程用fork函数.fork就是从父进程拷贝一个新的进程出来,子进程 ...
- JavaScript – 冷知识 (新手)
当 charAt 遇上 Emoji 参考: stackoverflow – How to get first character of string? 我们经常会用 charAt(0) 来获取 fir ...
- ASP.NET Core – Program.cs and Startup.cs 小笔记
前言 ASP.NET Core 6.0 以后, 默认模板去掉了 Program.cs 的 namespace, class 和 Startup.cs, 一开始看会有点懵. 这篇大概记入一下, prog ...
- [TK] CF1526B I Hate 1111
给定一个数,将它表示成若干个形如 \(11,111,1111\cdots\) 之类的数之和,判断有没有可行解 考虑到一种贪心,即从高位开始依次向下减去每位数字,判断还能不能减动,减不动或者没减完就报告 ...
- C4996 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.
错误原因 VS平台认为scanf函数不安全,要求换成scanf_s函数 解决方案 方案一:将scanf换成scanf_s[不建议] 将scanf换成scanf_s 但是,scanf_s函数只能在vs上 ...
- KubeSphere 社区双周报|2024.09.27-10.10
KubeSphere 社区双周报主要整理展示新增的贡献者名单和证书.新增的讲师证书以及两周内提交过 commit 的贡献者,并对近期重要的 PR 进行解析,同时还包含了线上/线下活动和布道推广等一系列 ...
- 在 Azure CNI 中启用 Calico WireGuard
作者:Peter Kelly 译者:Wendi Wang 注:本文已取得作者本人的翻译授权! 去年6月,Tigera 宣布首次在 K8s 上支持用于集群内加密传输的开源 VPN - WireGuard ...
- 玩黑悟空要配什么显卡?ToDesk云电脑一招搞定!
近期国产游戏大作<黑神话·悟空>的预售开启,许多玩家对于如何配置自己的电脑以畅玩这款画质卓越.支持全景光追的3A大作产生了浓厚的兴趣. 尤其是显卡的选择,成为了玩家们关注的焦点.<黑 ...
- ROS入门21讲(2)
四.创建工作空间与功能包 1.工作空间 工作空间(workspace):是一个存放工程开发相关文件的文件夹(相当于在IDE中创建的工程文件). 包含: src:代码空间(Source Space),放 ...
- 使用 vscode 编译+运行 typescropt Mac win同理
一..d.ts文件最好在src/typings 目录下,可在tsconfig.json 文件配置 二.vs 监听文件变化,自动编译ts文件 tsconfig.json { "compiler ...