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

2023-01-21 0 3,988

目录

对象嵌套关联查询一对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

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

学生student

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

代码实现

<!--        多对一  或者 一对一   -->
<!--        <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);
    } 
}

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

:本文采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可, 转载请附上原文出处链接。
1、本站提供的源码不保证资源的完整性以及安全性,不附带任何技术服务!
2、本站提供的模板、软件工具等其他资源,均不包含技术服务,请大家谅解!
3、本站提供的资源仅供下载者参考学习,请勿用于任何商业用途,请24小时内删除!
4、如需商用,请购买正版,由于未及时购买正版发生的侵权行为,与本站无关。
5、本站部分资源存放于百度网盘或其他网盘中,请提前注册好百度网盘账号,下载安装百度网盘客户端或其他网盘客户端进行下载;
6、本站部分资源文件是经压缩后的,请下载后安装解压软件,推荐使用WinRAR和7-Zip解压软件。
7、如果本站提供的资源侵犯到了您的权益,请邮件联系: 442469558@qq.com 进行处理!

猪小侠源码-最新源码下载平台 Java教程 MybatisPlus实现对象嵌套关联查询一对多List集合查询 http://www.20zxx.cn/463390/xuexijiaocheng/javajc.html

猪小侠源码,优质资源分享网

常见问题
  • 本站所有资源版权均属于原作者所有,均只能用于参考学习,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担
查看详情
  • 最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,建议提前注册好百度网盘账号,使用百度网盘客户端下载
查看详情

相关文章

官方客服团队

为您解决烦忧 - 24小时在线 专业服务