版权声明:本文为博主原创文章,转载请标明地址。 https://blog.csdn.net/u013034413/article/details/79696213
参考:https://www.jianshu.com/p/bc68e763c7a2
一对一
一个boy对应一个girl,相应地一个girl对应一个boy
Boy类:
public class Boy extends DataSupport {
private int id;
private Girl girl;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Girl getGirl() {
return girl;
}
public void setGirl(Girl girl) {
this.girl = girl;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Boy{" +
"id=" + id +
", girl=" + girl +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
Girl类
public class Girl extends DataSupport {
private int id;
private Boy boy;
private int age;
private String name;
public Boy getBoy() {
return boy;
}
public void setBoy(Boy boy) {
this.boy = boy;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "Girl{" +
"id=" + id +
", boy=" + boy +
", age=" + age +
", name='" + name + '\'' +
'}';
}
}
测试代码:
Girl girl = new Girl();
girl.setName("lucy");
girl.setAge(17);
girl.save();
Boy boy = new Boy();
boy.setAge(17);
boy.setName("jack");
boy.setGirl(girl);
boy.save();
Boy b = DataSupport.findLast(Boy.class, true);
Log.e("tag", b.toString());
Girl g = DataSupport.findLast(Girl.class, true);
Log.e("tag", g.toString());
int id = g.getId();
g.setBoy(b);
g.update(id);
测试结果:
E/tag: Boy{id=2, girl=Girl{id=2, boy=null, age=17, name='lucy'}, name='jack', age=17}
E/tag: Girl{id=2, boy=Boy{id=2, girl=null, name='jack', age=17}, age=17, name='lucy'}
可以看到,一个Boy对应一个Girl,一个Girl也对应一个Boy
多对一
一张专辑有多首歌,一首歌对应一张专辑
Song:
public class Song extends DataSupport {
private int id;
private Album album;
private String name;
private int duration;
public Album getAlbum() {
return album;
}
public void setAlbum(Album album) {
this.album = album;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "Song{" +
"id=" + id +
", album=" + album +
", name='" + name + '\'' +
", duration=" + duration +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDuration() {
return duration;
}
public void setDuration(int duration) {
this.duration = duration;
}
}
Album:
public class Album extends DataSupport {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
private String name;
private int age;
private List<Song> song = new ArrayList<>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<Song> getSong() {
return song;
}
public void setSong(List<Song> song) {
this.song = song;
}
@Override
public String toString() {
return "Album{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", song=" + song.toString() +
'}';
}
}
测试代码:
Song song1 = new Song();
song1.setName("因为爱情");
song1.setDuration(240);
Song song2 = new Song();
song2.setName("爱情转移");
song2.setDuration(340);
song1.save();
song2.save();
Album album = new Album();
album.getSong().add(song1);
album.getSong().add(song2);
album.setName("陈奕迅");
album.setAge(10);
album.save();
Album album1 = DataSupport.findLast(Album.class, true);
Song song3 = DataSupport.findLast(Song.class, true);
Log.e("tag", song3.toString());
Log.e("tag", album1.toString());
测试结果:
E/tag: Song{id=2, album=Album{id=1, name='陈奕迅', age=10, song=[]}, name='爱情转移', duration=340}
E/tag: Album{id=1, name='陈奕迅', age=10, song=[Song{id=1, album=null, name='因为爱情', duration=240}, Song{id=2, album=null, name='爱情转移', duration=340}]}
可以看到Song对应一个Album,一个Album对应多个Song
多对多
一个老师对应多个学生,一个学生对应多个老师
Teacher类
public class Teacher extends DataSupport {
private int id;
private String name ;
private int age;
private List<Student> student = new ArrayList<>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<Student> getStudent() {
return student;
}
public void setStudent(List<Student> student) {
this.student = student;
}
@Override
public String toString() {
return "Teacher{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", student=" + student +
'}';
}
}
Student类
public class Student extends DataSupport {
private int id;
private String name;
private int age;
private List<Teacher> teacherList = new ArrayList<>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<Teacher> getTeacherList() {
return teacherList;
}
public void setTeacherList(List<Teacher> teacherList) {
this.teacherList = teacherList;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", teacherList=" + teacherList +
'}';
}
}
测试代码:
Student s1 = new Student();
s1.setName("小名");
s1.setAge(12);
s1.save();
Student s2 = new Student();
s2.setName("小红");
s2.setAge(13);
s2.save();
Teacher t1 = new Teacher();
t1.setAge(45);
t1.setName("张老师");
t1.getStudent().add(s1);
t1.getStudent().add(s2);
t1.save();
Teacher t2 = new Teacher();
t2.setAge(46);
t2.setName("刘老师");
t2.getStudent().add(s1);
t2.getStudent().add(s2);
t2.save();
Student s = DataSupport.findLast(Student.class, true);
Log.e("tag", s.toString());
Teacher t = DataSupport.findLast(Teacher.class, true);
Log.e("tag", t.toString());
测试结果:
E/tag: Student{id=2, name='小红', age=13, teacherList=[Teacher{id=1, name='张老师', age=45, student=[]}, Teacher{id=2, name='刘老师', age=46, student=[]}]}
E/tag: Teacher{id=2, name='刘老师', age=46, student=[Student{id=1, name='小名', age=12, teacherList=[]}, Student{id=2, name='小红', age=13, teacherList=[]}]}
可以看到,学生小红对应多个老师,刘老师对应多个学生