The classes of the two tables are shown in the previous article.<2Table join queries>
Here we will discuss how to use our habits.Inner joinOfSQLStatement to associate our data tables
The only difference is that
Book. xml
< Resultmap ID = "Get-book-Note" Class = "Com. It. Book" Groupby = "Book. oid" >
< Result Property = "Oid" Column = "Oid" />
< Result Property = "Name" Column = "Name" />
< Result Property = "Notes" Resultmap = "Get-Note" />
</ Resultmap >
< Resultmap ID = "Get-Note" Class = "NOTE" >
< Result Property = "Sid" Column = "Sid" />
< Result Property = "Sname" Column = "Sname" />
</ Resultmap >
<SelectID= "Getproduct"Resultmap= "Get-book-Note">
<! [CDATA [
Select
Book. OID,
Book. Name,
Note. Sid,
Note. sname
From
Book, note where note. book_oid = book. OID
]>
</Select>
Notes: 1. SQLAll fields in the statement,All must be inResultmapEmbodied in,Otherwise, the queried fields areNULL;
2.Parent table associated with child table,Add
<Result Property="Notes" Resultmap="Get-Note" />
property Notes attributes and methods to obtain subclasses , the result must be a subclass resultmap id
3. SelectStatementResultmapCorresponds toResultmapName
Test statement'Main. Java
// Test the association between two tables. Author: meetweb Try {
Book book1 = New Book ();
Book1.setoid (1 );
List <book> books = sqlmapclinet. queryforlist ("getproduct ");
System. Out. println (books. Size ());
For (Iterator = books. iterator (); iterator. hasnext ();){
Book bookk = (book) iterator. Next ();
System. Out. println (bookk. getname ());
List <Note> L = bookk. getnotes ();
System. Out. println (Note) L. Get (0). getsname ());
}
} Catch (Sqlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Original Works. For reprinted works, please indicate the author.