广告
返回顶部
首页 > 资讯 > 后端开发 > Python >MybatisPlus实现对象嵌套关联查询一对多List集合查询
  • 834
分享到

MybatisPlus实现对象嵌套关联查询一对多List集合查询

2024-04-02 19:04:59 834人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录对象嵌套关联查询一对多List集合查询mybatis嵌套关联查询如下一对多查询(经典案例)条件数据库代码实现对象嵌套关联查询一对多List集合查询 mybatis嵌套关联查询如下

对象嵌套关联查询一对多List集合查询

mybatis嵌套关联查询如下

由于我的是一对集合查询,所以我有两个类。

@Data
@TableName("tb_user")
public class User {
    @TableId(type= IdType.INPUT)
    private String id;
    @TableField("user_name")
    private String username;
    private String passWord;
    private String name;
    private String email;
    private int age;
    private ArrayList<Authority> list;
}

权限类

@Data
@TableName
public class Authority {
    @TableId(type = IdType.INPUT)
    @TableField("aid")
    private int id;
    @TableId("aname")
    private String name;
}

测试

 @Test
    public void ManyToMany(){
        User user = userMapper.selectAuthorityById(1);
        ArrayList <Authority> list = user.getList();
        System.out.println(user);
        for (Authority authority : list) {
            System.out.println("所对应权限为"+authority.getName());
        }
    }

SpringBoot项目的依赖

       <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
        <dependency>
        <groupId>Mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.26</version>
    </dependency>
        <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
        <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
       <!--mybatis plus 起步依赖-->
        <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.4.0</version>
    </dependency>

这下面就是我xml文件里面怎么写的嵌套查询语句

<mapper namespace="com.itheima.mybatisplus.mapper.UserMapper">
    <!--返回的对象为authority-->
    <resultMap id="authority" type="com.itheima.mybatisplus.domain.User">
        <id column="id" property="id"/>
        <id column="password" property="password"/>
        <id column="age" property="age"/>
        <id column="email" property="email"/>
        <id column="name" property="name"/>
        <id column="user_name" property="username"/>
      <collection property="list"
                  ofType="com.itheima.mybatisplus.domain.Authority">
          <id property="id" column="aid"/>
          <id property="name" column="aname"/>
      </collection>
    <select id="selectAuthorityById" parameterType="int" resultMap="authority">
       SELECT * FROM
         authority a,tb_user t,user_authority ua
         WHERE a.aid=ua.authority_id
         AND t.id=ua.user_id
         AND t.id=#{id}
    </select>

数据库的配置我就不放了,直接编写就可以了,看会下面这个xml配置就可以了 

一对多查询(经典案例)

条件

查询班级表 返回所有学生信息  (一对多问题)

数据库

班级class_info

学生student

代码实现

<!--        多对一  或者 一对一   -->
<!--        <association property=""-->
<!--        一对多 返回集合-->
<!- -  <collection  property=""- ->

实体类ClassInfo.java

@Data
public class ClassInfo {
 
    private Long id;
    private String name;
    private String nameTest; 
    private List<Student> studentList;
}

ClassInfoMapper.xml

<?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层某个接口的包的全名称-->
<mapper namespace="com.example.demo.mapper.ClassInfoMapper">
 
    <!--    查询班级 返回所有学生的信息   一对多-->
 
    <!--    自定义映射规则-->
    <resultMap id="OneToMany" type="com.qcby.zsGC.entity.ClassInfo">
        <result column="name" jdbcType="VARCHAR" property="nameTest" />
        <collection  column="{id1=id,name=name}"
                     property="studentList"
                     select="com.example.demo.mapper.StudentMapper.listByClassInfoId">                 </collection>
    </resultMap>
 
    <select id="listAllWithStudent" resultMap="OneToMany">
        select * from class_info
    </select>

关联StudentMapper.xml中的子查询

 <select id="listByClassInfoId" resultType="com.example.demo.entity.Student">
        SELECT
           *
        FROM
            student s
        where class_info_id = #{id1} or name = #{name}
    </select>

ClassInfoMapper.java

public interface ClassInfoMapper extends BaseMapper<ClassInfo> {  
    IPage<ClassInfo> listAllWithStudent(IPage<ClassInfo> page); 
}

ClassInfoService.java

public interface ClassInfoService extends IService<ClassInfo> { 
    IPage<ClassInfo> listAllWithStudent(IPage<ClassInfo> page); 
}

ClassInfoServiceImpl.java

@Service
public class ClassInfoServiceImpl extends ServiceImpl<ClassInfoMapper, ClassInfo> implements ClassInfoService {
    @Autowired
    private StudentService studentService;
    @Override
    public IPage<ClassInfo> listAllWithStudent(IPage<ClassInfo> page) {
        return this.baseMapper.listAllWithStudent(page);
    }
}

ClassInfoController.java

@Controller
@RequestMapping("classInfo")
public class ClassInfoController {
 
    @Autowired
    private ClassInfoService classInfoService;
 
    @RequestMapping("listAllWithStudent")
    @ResponseBody
    public IPage<ClassInfo> listAllWithStudent(Integer pageNo,Integer pageSize){
        Page<ClassInfo> page = new Page<>(pageNo,pageSize);
        return classInfoService.listAllWithStudent(page);
    } 
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: MybatisPlus实现对象嵌套关联查询一对多List集合查询

本文链接: https://www.lsjlt.com/news/149365.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作