5.mybatis pair of one-table association queries

Source: Internet
Author: User

Approach one: Nested results: Use nested result mappings to process subsets of duplicate federated results, encapsulate data from a table query (removing duplicate data)
SELECT * from class C,teacher t WHERE c.tid = T.tid and C.cid=#{id}

Method Two: Nested query, by executing another SQL mapping statement to return the expected complex type
SELECT * from class C,teacher t WHERE c.tid = T.tid and C.cid=#{id}
SELECT t.tid ID, t.tname name from teacher t WHERE T.tid = #{id}//Use the TID found above

2 species are used in Resultmap and Association (association), The difference:

In the association in Mode 1, use the Javatype to represent the associated entity type

Association in Mode 2, without Javatype, but with column (the foreign key representing the result of the first SQL query as the query condition for the second SQL) and select (Second SQL statement)

Instance:

There are class Class[id, name, teacher], the corresponding teacher Teacher[id, name], their relationship is one-on.

Insert Table Data:

-- Teacher Table INSERT  into VALUES NULL,' teacher a'); INSERT  into VALUES NULL,' teacher b');
--Class TableINSERT  into' MyBatis '. ' Class ' (' CID ', ' cname ', ' tid ')VALUES(NULL,'First Grade','1'); INSERT  into' MyBatis '. ' Class ' (' CID ', ' cname ', ' tid ')VALUES(NULL,'Second Grade','2');

Java Entity classes:

 Public classTeacher {Private intID; PrivateString name;  PublicTeacher () {Super(); }     PublicTeacher (intID, String name) {        Super();  This. ID =ID;  This. Name =name; }     Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; } @Override PublicString toString () {return"Teacher [id=" + ID + ", name=" + name + "]"; }    }
 Public classClazz {Private intID; PrivateString name; PrivateTeacher Teacher;  PublicClazz (intID, String name, Teacher Teacher) {        Super();  This. ID =ID;  This. Name =name;  This. Teacher =teacher; }     PublicClazz () {Super(); }     Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicTeacher Getteacher () {returnteacher; }     Public voidSetteacher (Teacher Teacher) { This. Teacher =teacher; } @Override PublicString toString () {return"Clazz [id=" + ID + ", name=" + name + ", teacher=" +Teacher+ "]"; }    }

The first way:nested results, using nested result mappings to process subsets of duplicate federated results, encapsulating data from a table query (removing duplicate data)

SELECT * from class C,teacher t WHERE c.tid = T.tid and C.cid=#{id}

Teachermapper.xml:

<!--Query class and teacher information -    <SelectID= "Getclazz"ParameterType= "int"Resultmap= "getclazzmap">SELECT * from class C,teacher t WHERE c.tid = T.tid and C.cid=#{id}</Select>        <Resultmaptype= "Clazz"ID= "getclazzmap">        <ID Property= "id"column= "CID"/>        <result Property= "Name"column= "CNAME"/>        <!--corresponding teacher of related classes -        <Association Property= "Teacher"Javatype= "Teacher">            <ID Property= "id"column= "Tid"/>            <result Property= "Name"column= "Tname"/>        </Association>    </Resultmap>

Java Test class:

/*** Way One: nested results *@authorCharm _ Niche **/@Test Public voidgetClazz1 () {//Create session, set transaction to Truesqlsession session = Mybatisutil.getsessionfactory (). Opensession (true); String Statement= "Com.mlxs.mybatis.test4.clazzMapper.getClazz"; Clazz Clazz= Session.selectone (statement, "2"); System.out.println ("Clazz--->" +clazz);    Session.close (); }

Results:

Clazz--->clazz [id=2, Name= sophomore, Teacher=teacher [id=2, name= teacher B]]

The second way: nested queries that return the expected complex type by executing another SQL mapping statement

SELECT * from class C,teacher t WHERE c.tid = T.tid and C.cid=#{id}
SELECT t.tid ID, t.tname name from teacher t WHERE T.tid = #{id}//Use the TID found above

Teachermapper.xml:

<!--single-to-one correlation query: Method Two: Nested query, by executing another SQL mapping statement to return the expected complex type SELECT * from class C,teacher t WHERE c.tid = T.tid and C.cid=#{id} SELECT t.tid ID, t.tname name from teacher t WHERE T.tid = #{id}//Use the TID found above -    <!--Query class and teacher information -    <SelectID= "GETCLAZZ2"ParameterType= "int"Resultmap= "getClazzMap2">SELECT * from class C,teacher t WHERE c.tid = T.tid and C.cid=#{id}</Select>    <SelectID= "getteacher"ParameterType= "int"Resulttype= "Teacher">SELECT t.tid ID, t.tname name from teacher t WHERE T.tid = #{id}</Select>        <Resultmaptype= "Clazz"ID= "getClazzMap2">        <ID Property= "id"column= "CID"/>        <result Property= "Name"column= "CNAME"/>        <!--associated class corresponding to the teacher, will query the class table return the TID parameter to Getteacher, that is, getteacher in the #{id} -        <Association Property= "Teacher"column= "tid"Select= "getteacher"/>    </Resultmap>

Java Test class:

/*** Method Two: Nested query, by executing another SQL mapping statement to return the expected complex type SELECT * from class C,teacher t WHERE c.tid = T.tid and C.cid=#{id} SELECT t.tid ID, t.tname name from teacher t WHERE T.tid = #{id}//Use the TID found above *@authorCharm _ Niche **/@Test Public voidgetClazz2 () {//Create session, set transaction to Truesqlsession session = Mybatisutil.getsessionfactory (). Opensession (true); String Statement= "COM.MLXS.MYBATIS.TEST4.CLAZZMAPPER.GETCLAZZ2"; Clazz Clazz= Session.selectone (statement, "2"); System.out.println ("Clazz--->" +clazz);    Session.close (); }

Results:

Clazz--->clazz [id=1, name= first grade, Teacher=teacher [id=1, name= teacher A]]

5.mybatis pair of one-table association 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.