业务代码的方式 (实现的方式,书写业务逻辑的java代码实现)
------查询学生所在班级的信息 (一对一查询)
1、先把所有学生查询出来(clazzno)
select* from student
2、拿着clazzno去clazz表中查询班级的信息
select * from clazz where cno=?
------查询班级中所有学生的信息(一对多查询)
1、查询所有班级的信息(clazzno)
Select * from clazz
2、查询指定班级中的所有学生—List
Select * from student where clazzno=?
特点:
班级和学生之间的关系全部是靠我们书写java业务逻辑代码的方式实现的
最后执行完成SQL语句都执行了N+1次数据的查询
接口
ClazzMapper.java
public interface ClazzMapper {//查询指定学生所在班级的信息Clazz selectOne(int clazzno);//查询所有班级信息List<Clazz> selectAll();
}
StudentMapper.java
public interface StudentMapper {//查询所有学生的操作List<Student> selectAll();List<Student> selectMore(int clazzno);
}
XML
ClazzMapper.xml
<select id="selectOne" resultType="clazz">SELECT * from clazz where clazzno=#{param1}</select><select id="selectAll" resultType="clazz">SELECT * from clazz</select>
StudentMappe.xml
<select id="selectAll" resultType="student">SELECT * from student</select><select id="selectMore" resultType="student">SELECT * from student where clazzno=#{param1}</select>
测试
//查询所有学生所在的班级的信息/* //[A]查询所有学生-->clazznoList<Student> list = stuMapper.selectAll();//[B]拿着clazzno去班级表中查询班级的信息for(Student stu:list){//每一个学生所在班级的编号Integer clazzno = stu.getClazzno();Clazz clazz = claMapper.selectOne(clazzno);stu.setCla(clazz);System.out.println(stu);}*///查询所有班级中学生的信息//[A]查询所有班级的信息List<Clazz> claList = claMapper.selectAll();//[B]查询班级中对应的学生的信息for(Clazz cla:claList){//班级编号Integer clazzno = cla.getClazzno();//指定班级中所有学生的集合List<Student> list = stuMapper.selectMore(clazzno);cla.setLi(list);System.out.println(cla);}