1. Create a database table
CREATE TABLE Course ( c_id INT (5) PRIMARY KEY, c_name VARCHAR (20)); CREATE TABLE Students ( s_id INT (5) PRIMARY KEY, s_name VARCHAR (), s_sal DOUBLE (8,2)); CREATE TABLE students_course ( s_id Int (5), c_id Int (5));
2. Create JavaBean
public class Students { private Integer sId; Private String sName; Private Double ssal; One student selected multiple courses private list<course> courselist;} public class Course { private Integer cId; Private String CName;}
3. mapping files
Studentsmapper. Xml<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd" >< Mapper namespace= "Org.zenzzat.toa.core.test.dao.StudentsMapper" > <resultmap id= "courseresultmap" type= " Org.zenzzat.toa.core.test.entity.Course "> <id column=" c_id "property=" CId "jdbctype=" INTEGER "/> <resul T column= "C_name" property= "CName" jdbctype= "VARCHAR"/> </resultMap> <resultmap id= "Studentsresultmap" Type= "Org.zenzzat.toa.core.test.entity.Students" > <id column= "s_id" property= "SId" jdbctype= "INTEGER"/> & Lt;result column= "S_name" property= "SName" jdbctype= "VARCHAR"/> <result column= "s_sal" property= "SSal" JdbcType = "DOUBLE"/> <collection property= "courselist" column= "s_id" javatype= "ArrayList" oftype= " Org.zenzzat.toa.core.test.entity.Course "select=" selesctcourseinstudents "/> </resultMap> <sel EctId= "selectstudents" parametertype= "int" resultmap= "Studentsresultmap" > select * from students WHERE s_id = #{sid} ; </select> <select id= "selesctcourseinstudents" parametertype= "int" resultmap= "Courseresultmap" > select * From course where c_id in (SELECT sc.c_id from Students_course sc WHERE sc.s_id = #{sid}); </select></mapper>
Multi-table collection nested queries