一个class中有许多student,属于一对多关系。本文利用mybatis注解,嵌套查询方式来实现一对多查询,在根据classId查询class时,顺便查询并展示出class中的student
student实体类
package com.itheima.dao;
public class StudentDao {
private Integer id;
private String name;
private Integer age;
private Integer cid;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
@Override
public String toString() {
return "StudentDao{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", cid=" + cid +
'}';
}
}
class实体类
package com.itheima.dao;
import java.util.List;
public class ClassDao {
private Integer id;
private String className;
private List<StudentDao> studentsList;
public Integer getId() {
return id;
}
public void

本文介绍了如何使用MyBatis注解处理一对多关系的嵌套查询,通过在Class类中存储Student的主键作为外键,然后在Class的Mapper接口中定义查询方法,实现在查询Class时同时获取到其对应的Student信息,从而展示2班的班级及学生详情。
最低0.47元/天 解锁文章
1821

被折叠的 条评论
为什么被折叠?



