发布网友 发布时间:2022-05-01 00:36
共1个回答
热心网友 时间:2022-06-21 10:40
<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学生"} ] }] 一般这样集联查询的多