《代码实例》MyBatis-Plus一对多查询

实体层
Aclass.java

package com.example.x.domain;

import com.baomidou.mybatisplus.annotation.TableField;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Aclass {

    private Integer cid;

    private String cname;

    @TableField(exist = false)
    private List<Student> studentList;
}

Student.java

package com.example.x.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {

    private Integer sid;

    private String sname;

}

dao层

package com.example.x.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.x.domain.Aclass;

import org.springframework.stereotype.Repository;

@Repository
public interface AclassDao extends BaseMapper<Aclass> {
}
package com.example.homework0322.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.homework0322.domain.Student;
import org.springframework.stereotype.Repository;

@Repository
public interface StudentDao extends BaseMapper<Student> {
}

测试类

package com.example.x;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.x.dao.AclassDao;

import com.example.x.dao.StudentDao;
import com.example.x.dao.UserDao;
import com.example.x.domain.Aclass;
import com.example.x.domain.Student;
import com.example.x.domain.User;
import com.example.x.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;

@SpringBootTest
class Homework0322ApplicationTests {


    @Autowired
    private AclassDao aclassDao;
    @Autowired
    private StudentDao studentDao;



    /**
     * mybatis-plus一对多查询
     */
    @Test
    void test01(){
        List<Aclass> aclasses = aclassDao.selectList(null);

        List<Aclass> list = new ArrayList<Aclass>();
        for (Aclass aClass : aclasses) {
            List<Student> studentList = studentDao.selectList(new QueryWrapper<Student>().eq("cid", aClass.getCid()));
            aClass.setStudentList(studentList);
            list.add(aClass);
        }
        System.out.println(list);
    }

}

数据库设计
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值