Java基础|mybatis plus一对多查询(经典案例)

一、条件

  • 查询班级表 返回所有学生信息(一对多问题)
二、数据库 班级class_info
Java基础|mybatis plus一对多查询(经典案例)
文章图片

【Java基础|mybatis plus一对多查询(经典案例)】学生student
Java基础|mybatis plus一对多查询(经典案例)
文章图片


二、代码实现

实体类ClassInfo.java
@Data public class ClassInfo {private Long id; private String name; private String nameTest; private List studentList; }

ClassInfoMapper.xml
select * from class_info

关联StudentMapper.xml中的子查询
SELECT * FROM student s where class_info_id = #{id1} or name = #{name}

ClassInfoMapper.java
public interface ClassInfoMapper extends BaseMapper {IPage listAllWithStudent(IPage page); }

ClassInfoService.java
public interface ClassInfoService extends IService {IPage listAllWithStudent(IPage page); }


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


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


    推荐阅读