mybatis plus一对多查询(经典案例)

一、条件

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

二、数据库

班级class_info

mybatis plus一对多查询(经典案例)_第1张图片

学生student

mybatis plus一对多查询(经典案例)_第2张图片

二、代码实现





实体类ClassInfo.java

@Data
public class ClassInfo {

    private Long id;
    private String name;
    private String nameTest;

    private List studentList;
}

ClassInfoMapper.xml






    

    
    
        
                         
    

    

关联StudentMapper.xml中的子查询

 

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);
    }

}

你可能感兴趣的:(Java基础,java,开发语言)