注:代码已托管在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),resultMapid“newsLabelMapper”,“newsLabelMapper”中除了id映射和name映射,还有一个<association/>,里面定义的是关联关系定义。

  • property="parent":表示映射的属性是parent
  • javaType="NewsLabel":表示映射的类型是NewsLabel
  • column="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自关联多对一查询方式的更多相关文章

  1. myBatis系列之四:关联数据的查询

    myBatis系列之三:增删改查是基于单表的查询,如果联表查询,返回的是复合对象,需要用association关键字来处理. 如User发表Article,每个用户可以发表多个Article,他们之间 ...

  2. mybatis 使用resultMap实现关联数据的查询(association 和collection )

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "- ...

  3. mybatis实战教程二:多对一关联查询(一对多)

    多对一关联查询 一.数据库关系.article表和user表示多对一的关系 CREATE TABLE `article` ( `id` ) NOT NULL AUTO_INCREMENT, `user ...

  4. MyBatis 高级查询之多对多查询(十一)

    高级查询之多对多查询 查询条件:根据玩家名,查询游戏信息 我们在之前创建的映射器接口 GameMapper.java 中添加接口方法,如下: /** * 根据玩家名查询游戏 * @param name ...

  5. mybatis实战教程(mybatis in action)之四:实现关联数据的查询

    有了前面几章的基础,对一些简单的应用是可以处理的,但在实际项目中,经常是关联表的查询,比如最常见到的多对一,一对多等.这些查询是如何处理的呢,这一讲就讲这个问题.我们首先创建一个Article 这个表 ...

  6. MyBatis 实践 -动态SQL/关联查询

    MyBatis 实践 标签: Java与存储 动态SQL 动态SQL提供了对SQL语句的灵活操作,通过表达式进行判断,对SQL进行拼接/组装. if 对查询条件进行判断,如果输入参数不为空才进行查询条 ...

  7. mybatis(一、原理,一对多,多对一查询)

    MyBatis框架及原理分析 MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架,其主要就完成2件事情: 封装JDBC操作 利用反射打通Java类与SQL语句之间的相互转换 ...

  8. SpringBoot使用Mybatis注解进行一对多和多对多查询(2)

    SpringBoot使用Mybatis注解进行一对多和多对多查询 GitHub的完整示例项目地址kingboy-springboot-data 一.模拟的业务查询 系统中的用户user都有唯一对应的地 ...

  9. Mybatis表关联多对一

    在上章的 一对多 中,我们已经学习如何在 Mybatis 中关联多表,但在实际项目中也是经常使用 多对一 的情况,这些查询是如何处理的呢,在这一节中我们来学习它.多表映射的多对一关系要用到 mybit ...

  10. 4. mybatis实战教程(mybatis in action)之四:实现关联数据的查询

    转自:https://www.cnblogs.com/shanheyongmu/p/5653599.html 有了前面几章的基础,对一些简单的应用是可以处理的,但在实际项目中,经常是关联表的查询,比如 ...

随机推荐

  1. Google – Cloud Translation API

    前言 通常网站内容翻译,我们都不推荐使用 Google Translate.但网站中一些不那么重要的内容确实可以用 Google Translate.比如 Customer Reviews. 这篇是续 ...

  2. uni-app v3.0.0-alpha-3090220231010001

    https://uniapp.dcloud.net.cn/tutorial/ #-------------------------------------------------------- 未分类 ...

  3. SpringMVC —— 请求参数(传递json数据)

    接收请求中的json数据             注解       json格式(POJO)    json数组(POJO)    @RequestBody与@RequestParam区别   

  4. JavaScript中if嵌套assert的方法

    在JavaScript中,通常我们不会直接使用assert这个词,因为JavaScript标准库中并没有直接提供assert函数(尽管在一些测试框架如Jest.Mocha中经常看到).但是,我们可以模 ...

  5. SpringBoot 实现文件上传

    参考:Java springboot进阶教程 文件上传功能实现 后端代码编写 常见错误分析与解决 在 Service 业务层接口中增加一个上传文件的方法 因为文件并不是上传至数据库中,所以不需要编写 ...

  6. 高通ADSP USB流程

    在高通平台上,ADSP(Audio Digital Signal Processor,音频数字信号处理器)可以通过 USB 接口与主机进行数据传输,以下是大致的 ADSP USB 流程: 主机发起 U ...

  7. OOP的核心思想

    1. 封装 既是信息封装,把一些信息进行封装成对象,只保留部分接口和方法与外部联系,能有效避免程序间相互依赖,实现代码模块间松藕合 : 2. 继承 子类自动继承父类的属性和方法,继承实现了代码的重用性 ...

  8. 把数字转换RMB形式

    方法1 : var str = '12345679' let strNew = str.replace(/\B(?=(?:\d{3})+\b)/g, ',') // 匹配单词边界替换为逗号 方法2: ...

  9. 凌晨 12 点突发 istio 生产事故!一顿操作猛如虎解决了

    事故起因 业务上新集群,本来以为"洒洒水",11 点切,12 点就能在家睡觉了.流量切过来后,在验证过程中,发现网页能够正常打开,在登录时返回了 502,当场懵逼.在相关的容器日志 ...

  10. 【2022noip多校】异或

    [题目描述] 对于一个元素介于 \([0,2^m)\) 且互不相同的长度为 \(n\) 的序列 \(a_1, a_2 ...,a_n\) ,定义它的特征序列为 \(p_0,p_1,...,p_{2^m ...