前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Mybatis【18】-- Mybatis自关联多对一查询方式

Mybatis【18】-- Mybatis自关联多对一查询方式

原创
作者头像
秦怀杂货店
修改2021-02-04 10:05:19
3720
修改2021-02-04 10:05:19
举报
文章被收录于专栏:技术杂货店技术杂货店

注:代码已托管在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,希望查出父辈栏目,父辈的父辈栏目等等信息。

数据表设计如下:

实体类设计:

代码语言:txt
复制
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查询接口定义:

代码语言:txt
复制
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 属性执行的语句
代码语言:txt
复制
<?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>

测试类:

代码语言:txt
复制
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();
    }
  }

}

查询出来结果:

代码语言:txt
复制
[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]]]

【作者简介】:

秦怀,公众号【秦怀杂货店】作者,技术之路不在一时,山高水长,纵使缓慢,驰而不息。求个 赞 和 在看 ,对我,是莫大的鼓励和认可,让我更有动力持续写出好文章。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档