java,mybatis 一对多级联查询,怎么给多的一方添加条件啊???
比如Teacher和Student,查询teacher级联查询出学生,但是又不是查询所有的学生,比如查询teacher:name=张三,student:age>18,这个...
比如 Teacher和 Student, 查询teacher 级联查询出学生,但是又不是查询所有的学生,
比如 查询 teacher: name= 张三 , student: age>18 , 这个怎么做到啊 ,是级联查询 展开
比如 查询 teacher: name= 张三 , student: age>18 , 这个怎么做到啊 ,是级联查询 展开
2个回答
展开全部
把你的条件添加到select语句后面,然后传下去,例如:
<!-- 旅行社详情 -->
<resultMap type="com.demo.teacher" id="teacherMap">
<id property="teacherId" column="teacher_id"/>
<result property="teacherName" column="teacher_name"/>
<!--注意下面这个声明,只有column-->
<result column="age"/>
<collection property="student" column="{teacherId=teacher_id,age=age}" ofType="java.util.Map" select="studentMap">
<id property="studentId" column="student_id" />
<result property="studentName" column="student_name"/>
<result property="studentAge" column="student_age"/>
</collection>
</resultMap>
<!--主-->
<select id="getTeacher" parameterType="java.util.Map" resultMap="teacherMap">
select
teacher_id,
teacher_name,
#{age} as age <!--把你的参数这样写-->
from
teachers
where
teacher_name = '大西瓜'
</select>
<!--从-->
<select id="studentMap" parameterType="java.util.Map" resultType="java.util.Map">
select
student_id,
student_name,
student_age
from
students
where
teacher_id = #{teacherId}
and
age > #{age} <!--这两个参数是resultMap中column指定的key-->
</select>
<!--mybatis的一对多级联查询多方的参数只能是一方的column,所以你要想办法把你的参数做成column-->
展开全部
<resultMap type="com.xx.xx.Teacher" id="ResultMap">
<id column="teacherId" property="teacher_id" jdbcType="BIGINT" />
<result column="teacherName" property="teacher_name" jdbcType="VARCHAR" />
<association property="studentList" column="teacher_id"
select="com.xxxx.selectStudentByTeacherId"></association>
</resultMap>
<resultMap type="com.xx.xx.Student" id="StudentResultMap">
<id column="teacherId" property="teacher_id" jdbcType="BIGINT" />
<result column="teacherName" property="teacher_name" jdbcType="VARCHAR" />
<association property="studentList" column="teacher_id"
select="com.xxxx.selectStudentByTeacherId"></association>
</resultMap>
<select id="selectTeacherList" parameterMap="java.util.Map" resultMap="ResultMap">
select * from teacher where teacher_name like CONCAT(CONCAT('%',
#{teacherName}),'%')
</select>
<select id="selectTeacherList" parameterType="java.lang.Long" resultMap="StudentResultMap">
select * from student where teacher_id = #{teacherId}
</select>
public class Teacher{
private Long teacherId;
private String teacherName;
private List<Student> 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学生"}
]
}]
一般这样集联查询的多
追问
谢谢啊,你的这个写法,还是没有过滤掉部分的学生啊 , 假如 Teacher 张三 关联30个学生, select * from student where teacher_id = #{teacherId} 这个不就查询出30个学生了吗,但是我只想要 查询出 age>18的学生 , 我不知道 怎么把这个 age带到sql去,
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询