MyBatis one-to-one correlation table query

Source: Internet
Author: User

Create a table first

CREATE TABLETeacher (t_idINT PRIMARY KEYAuto_increment,t_nameVARCHAR( -));CREATE TABLEClass (c_idINT PRIMARY KEYAuto_increment,c_nameVARCHAR( -), teacher_idINT);ALTER TABLEClassADD CONSTRAINTfk_teacher_idFOREIGN KEY(teacher_id)REFERENCESteacher (t_id);INSERT  intoTeacher (T_name)VALUES('LS1');INSERT  intoTeacher (T_name)VALUES('LS2');INSERT  intoClass (C_name, teacher_id)VALUES('bj_a',1);INSERT  intoClass (C_name, teacher_id)VALUES('Bj_b',2);

To define an entity class:

 Public class Teacher {privateint  ID; Private String name;}
 Public class Classes {    privateint  ID;     Private String name;     Private Teacher Teacher;}

There are two ways to query for what you want to see in two tables:

 select  *   class C, teacher T where  c.teacher_id =  t.t_id and  c.c_id=  #{id}//Union Table query /pre>
SELECT *  from WHERE c_id=1; SELECT *  from WHERE t_id=1//1 is the value of the teacher_id from the previous query              //multi-table query

mapping files for the first way:

<SelectID= "GetClass"ParameterType= "int"Resultmap= "Getclassmap">SELECT * from class C, teacher T where c.teacher_id=t.t_id and C.c_id=#{id}</Select>    <Resultmaptype= "Classes"ID= "Getclassmap">        <ID Property= "id"column= "c_id"/>        <result Property= "Name"column= "C_name"/>        <Association Property= "Teacher"Javatype= "Teacher">            <ID Property= "id"column= "t_id"/>            <result Property= "Name"column= "T_name"/>        </Association>    </Resultmap>

Mapping file for the second way

<SelectID= "GetClass2"Resultmap= "Getclass2map">SELECT * from class where C_id=#{id}</Select>    <SelectID= "Getteacher"Resulttype= "Teacher">Select t_id ID, t_name name from teacher where T_id=#{id}</Select>    <Resultmaptype= "Classes"ID= "Getclass2map">        <ID Property= "id"column= "c_id"/>        <result Property= "Name"column= "C_name"/>        <Association Property= "Teacher"column= "teacher_id"Select= "Getteacher">        </Association>    </Resultmap>

MyBatis one-to-one correlation table query

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.