MyBatis中多表查询(业务代码方式)
生活随笔
收集整理的這篇文章主要介紹了
MyBatis中多表查询(业务代码方式)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
業務代碼的方式 (實現的方式,書寫業務邏輯的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
StudentMapper.java
public interface StudentMapper {//查詢所有學生的操作List<Student> selectAll();List<Student> selectMore(int clazzno); }XML
ClazzMapper.xml
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);}總結
以上是生活随笔為你收集整理的MyBatis中多表查询(业务代码方式)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电脑两种升级驱动程序的方法电脑如何更新驱
- 下一篇: 《超级马力欧兄弟:惊奇》游戏 Demo