赞
踩
关键在于练习 resultMap、association、collection 标签的使用
student 包含个人基本信息和内嵌对象 classes。
在查询student时,需要联合 student表和classes表进行查询。
CREATE TABLE classes( id int not null primary key, name varchar(20) ); insert into classes (id,name) values (1,"一班"),(2,"二班"),(3,"三班"); CREATE TABLE student( id int not null primary key auto_increment, name varchar(10), cid int, foreign key (cid) references classes(id) ); insert into student (id, name, cid) values (1,"KawYang",1),(2,"KawYang",2),(3,"张三",2),(4,"李四",3),(5,"Tom",3),(6,"Jary",3);
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Classes{
private int id;
private String name;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student{
private int id;
private String name;
private Classes classes;
}
编写数据库操作的接口
public interface IStudentRepository {
/**
* find by id
* @param id
* @return student
*/
Student findById(int id);
/**
* find all
* @return student list
*/
List<Student> findAll();
}
mapper
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。