1: Requirement: query the student numbers and names of students who have studied all the courses taught by teacher "Ye Ping";
1 List<Map<String,Object>> selectYepingAllCourse(@Param("name") String name);
use
List<Map<String,Object>> to receive the data queried from the database
1 <!--Student number and name of all the students taught by Mr. Yeping--> 2 <select id="selectYepingAllCourse" resultMap="Base_ResultMap"> 3 SELECT 4 a.s_id, 5 a.sname 6 FROM 7 a_student a 8 WHERE 9 a.s_id IN ( 10 SELECT 11 a.s_id 12 FROM 13 a_sc a 14 INNER JOIN a_course b ON a.c_id = b.c_id 15 INNER JOIN a_teacher c ON c.t_id = b.t_id 16 WHERE 17 c.tname = #{name} 18 GROUP BY 19 a.s_id 20 HAVING 21 count(a.c_id) = ( 22 SELECT 23 count(a_course.c_id) 24 FROM 25 a_course 26 INNER JOIN a_teacher ON a_teacher.t_id = a_course.t_id 27 WHERE 28 a_teacher.tname = #{name} 29 ) 30 ); 31 </select>
Analyze this sql.
1 : Which students have studied Teacher Ye Ping's class? SELECT a.s_id,count(a.c_id) FROM a_sc a INNER JOIN a_course b ON a.c_id = b.c_id INNER JOIN a_teacher c ON c.t_id = b.t_id WHERE c.tname = 'Yeping' GROUP BY a.s_id 2 : Mr. Ye Ping taught several classes. SELECT count(a_course.c_id) FROM a_course INNER JOIN a_teacher ON a_teacher.t_id = a_course.t_id WHERE a_teacher.tname = 'Yeping' ) 3 : Learn the information of the students who have the same class as Mr. Ye Ping's class. SELECT a.s_id FROM a_sc a INNER JOIN a_course b ON a.c_id = b.c_id INNER JOIN a_teacher c ON c.t_id = b.t_id WHERE c.tname = 'Yeping' GROUP BY a.s_id HAVING count(a.c_id) = ( SELECT count(a_course.c_id) FROM a_course INNER JOIN a_teacher ON a_teacher.t_id = a_course.t_id WHERE a_teacher.tname = 'Yeping' )
test:
/** * Query the student numbers and names of students who have studied all the courses taught by teacher "Ye Ping"; */ @Test public void testSelectYepingAllCourse(){ List<Map<String, Object>> list = ascMapper.selectYepingAllCourse("李一"); for (Map<String,Object> i:list){ System.out.println(i); } }
The result of the output:
Get the value of the Map collection in the List collection.
1 for ( int i=0;i<list.size();i++ ){ 2 Map map= list.get(i); 3 // It is received by set. cannot be repeated. No order 4 Set set = map.keySet(); 5 System.out.println("set value" + set); 6 Object sId = map.get("sId" ); 7 System.out.println("Get sId value "+ sId); 8 }
Output result: This one has only one result.
See below for several results.