一、多对一查询
根据班级id查询班级信息
①创建表
CREATE TABLE student(
s_id INT PRIMARY KEY AUTO_INCREMENT,
s_name VARCHAR(20),
class_id INT
);
INSERT INTO student(s_name, class_id) VALUES('xs_A', 1);
INSERT INTO student(s_name, class_id) VALUES('xs_B', 1);
INSERT INTO student(s_name, class_id) VALUES('xs_C', 1);
INSERT INTO student(s_name, class_id) VALUES('xs_D', 2);
INSERT INTO student(s_name, class_id) VALUES('xs_E', 2);
INSERT INTO student(s_name, class_id) VALUES('xs_F', 2);
②实体类
@Data
public class Clazz {
private Integer cid;
private String cname;
private Teacher teacher; //在班级类中定义个老师类型的属性 多对一
}
@Data
public class Teacher {
private Integer tid;
private String tname;
}
③Dao
public interface ClazzDao {
public Clazz findById(int cid);
}
④映射文件