html5博客网站源码,公司网址制作,噼里啪啦在线看免费观看视频,嘉兴网站建设电话目录
1、使用VO聚合对象#xff08;可以解决这两种情况#xff09;
多对一#xff1a;
一对多#xff1a;
2、非聚合的多对一做法#xff1a;
3、非聚合的一对多做法#xff1a; 1、使用VO聚合对象#xff08;可以解决这两种情况#xff09; 当我需要多对一、一对…目录
1、使用VO聚合对象可以解决这两种情况
多对一
一对多
2、非聚合的多对一做法
3、非聚合的一对多做法 1、使用VO聚合对象可以解决这两种情况 当我需要多对一、一对多时可以创建VO聚合对象。 例如学生类和老师类会出现多对一、一对多的情况。此时创建一个聚合类里面包含所需要的学生类和老师类的属性。并先查询出前面的内容再根据前面的内容查询出后面的内容。 多对一
数据库设计学生表中有一个tid来匹配老师id 在学生类和老师类后再创建出一个聚合类:
Data
public class Student {private Integer id;private String username;private Integer tid;
}
Data
public class Teacher {private Integer id;private String username;
}Data
public class StudentAndTeacher {private Integer id;private String username;private Teacher teacher;
}写出查询所有学生和根据学生类里的tid查询老师:
Mapper
public interface AllMapper {ListStudent AllStudent();Teacher AllTeacherByTid(Integer tid);
} select idAllStudent resultTypecom.example.demo.entity.Studentselect * from student;/selectselect idAllTeacherByTid resultTypecom.example.demo.entity.Teacherselect * from teacher where id#{tid};/select
通过聚合类把这两个查询到的内容聚合到一起 Testpublic void AllStudent() {ListStudent students allMapper.AllStudent();for (Student student : students) {StudentAndTeacher studentAndTeacher new StudentAndTeacher();studentAndTeacher.setId(student.getId());studentAndTeacher.setUsername(student.getUsername());studentAndTeacher.setTeacher(allMapper.AllTeacherByTid(student.getTid()));System.out.println(studentAndTeacher);}}
结果如下 一对多
学生类和老师类一样但是聚合类不一样因为现在的主体是老师而不是学生。
Data
public class Student {private Integer id;private String username;private Integer tid;
}
Data
public class Teacher {private Integer id;private String username;
}Data
public class TeacherAndStudent {private Integer id;private String username;private ListStudent studentList;
}
写出查询所有老师和根据老师的id去学生表里查对应tid的学生:
Mapper
public interface AllMapper {ListTeacher AllTeacher();ListStudent AllStudentById(Integer id);
} select idAllTeacher resultTypecom.example.demo.entity.Teacherselect * from teacher;/selectselect idAllStudentById resultTypecom.example.demo.entity.Studentselect * from student where tid#{id};/select
通过聚合类把这两个查询到的内容聚合到一起 Testpublic void AllTeacher() {ListTeacher Teachers allMapper.AllTeacher();for (Teacher teacher : Teachers) {ListStudent students allMapper.AllStudentById(teacher.getId());TeacherAndStudent studentAndTeacher new TeacherAndStudent();studentAndTeacher.setId(teacher.getId());studentAndTeacher.setUsername(teacher.getUsername());studentAndTeacher.setStudentList(students);System.out.println(studentAndTeacher);}}
结果如下 2、非聚合的多对一做法
知识点association标签是用在多对一时当一个类中有其他类作为该类的属性时要用到这个标签。
此时的Student类中有一个属性是Teacher类
Data
public class Student {private Integer id;private String username;private Teacher teacher;
}
Data
public class Teacher {private Integer id;private String username;
}
Mapper
public interface AllMapper {ListStudent AllStudent();
} 这里用resultMap来匹配。 要注意两点 1、result 标签里的property对应的是java类的属性名column对应的是select 标签里查询出来的字段名。并且要注意若多个表的字段名相同必须要用别名区分并在column中写上别名而非字段名。 2、association 标签中property对应的是Student类中的teacher属性类型为Student类这里的类型用javaType而不是Type。 select idAllStudent resultMapStudentAndTeacherselect s.id sid, s.username susername, t.id tid, t.username tusernamefrom student s, teacher twhere s.tid t.id;/selectresultMap idStudentAndTeacher typecom.example.demo.entity.Studentresult propertyid columnsid/resultresult propertyusername columnsusername/resultassociation propertyteacher javaTypecom.example.demo.entity.Teacherresult propertyid columntid/resultresult propertyusername columntusername/result/association/resultMap Testpublic void StudentAndTeacher() {ListStudent students allMapper.AllStudent();for (Student student : students) {System.out.println(student);}}
结果如下 3、非聚合的一对多做法
知识点collection标签是用在一对多时当一个类中有其他类集合作为该类的属性时要用到这个标签。
此时的Teacher类中有一个属性是ListStudent
Data
public class Student {private Integer id;private String username;private Integer tid;
}
Data
public class Teacher {private Integer id;private String username;private ListStudent students;
}
Mapper
public interface AllMapper {ListTeacher AllTeacher();
} 这里用resultMap来匹配。 要注意两点 1、result 标签里的property对应的是java类的属性名column对应的是select 标签里查询出来的字段名。并且要注意若多个表的字段名相同必须要用别名区分并在column中写上别名而非字段名。 2、collection标签中property对应的是Teacher类中的students属性类型为List这里的类型用javaType而不是TypeofType指的是List的泛型。 select idAllTeacher resultMapTeacherAndStudentselect s.id sid, s.username susername, s.tid stid, t.id tid, t.username tusernamefrom student s, teacher twhere s.tid t.id;/selectresultMap idTeacherAndStudent typecom.example.demo.entity.Teacherresult propertyid columntid/resultresult propertyusername columntusername/resultcollection propertystudents javaTypeList ofTypecom.example.demo.entity.Studentresult propertyid columnsid/resultresult propertyusername columnsusername/resultresult propertytid columnstid/result/collection/resultMap Testpublic void StudentAndTeacher() {ListTeacher teachers allMapper.AllTeacher();for (Teacher teacher : teachers) {System.out.println(teacher);}}
此时结果