java,mybatis 一对多级联查询,怎么给多的一方添加条件啊???

2025-02-27 00:05:06
推荐回答(2个)
回答1:

把你的条件添加到select语句后面,然后传下去,例如:



    
    
    
    
    
        
        
        
    




    select 
        teacher_id,
        teacher_name,
        #{age} as age 
    from 
        teachers
    where
        teacher_name = '大西瓜'


    select 
        student_id,
        student_name,
        student_age
    from 
        students
    where
        teacher_id = #{teacherId}
    and
        age > #{age} 

回答2:


    
    
       select="com.xxxx.selectStudentByTeacherId">



    
    
       select="com.xxxx.selectStudentByTeacherId">



 select * from teacher where teacher_name like  CONCAT(CONCAT('%',
#{teacherName}),'%')



 select * from student where teacher_id = #{teacherId}


public class Teacher{
    private Long teacherId;
    private String teacherName;
    private List studentList;
    //get and set method
}

public class Student{
    private Long studentId;
    private String studentName;
    //get and set method
}
这样 会查出来 这样的结果JSON

[{
"teacherId":"1",
"teacherName":"张三",
"studentList":[
               {"studentName":"a学生"},
               {"studentName":"b学生"}
               ]
 },{
"teacherId":"2",
"teacherName":"张四",
"studentList":[
               {"studentName":"c学生"},
               {"studentName":"d学生"}
               ]
 }]
 
 一般这样集联查询的多