Multi-table collection nested queries

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.