mybatis怎么一对多查询语句
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis怎么一对多查询语句相关的知识,希望对你有一定的参考价值。
参考技术A 这个sql有问题的吧,如果已经配置了关联关系了,就不用写left join了,直接写select * from students,不过新手我还是建议不要写关联,直接把resultmap写一个既有students的属性又有class的属性的map,然后再select s.*,c.* from student s left join classInfo c on s.classInfo_id = c.id,这样返回的resultmap就是两个对象都有的map了,自己再创建一个对象,里面和resultmap相对应就可以了java,mybatis 一对多级联查询,怎么给多的一方添加条件啊???
比如 Teacher和 Student, 查询teacher 级联查询出学生,但是又不是查询所有的学生,
比如 查询 teacher: name= 张三 , student: age>18 , 这个怎么做到啊 ,是级联查询
把你的条件添加到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--> 参考技术A <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去,
以上是关于mybatis怎么一对多查询语句的主要内容,如果未能解决你的问题,请参考以下文章