How can I map ibatis to myself?
For example, a class is defined as follows:
Public class Address {
Private int id;
Private String homeAddress;
Private String workAddress;
}
Public class Person {
Private int id;
Private String name;
Private Person spouse; // the spouse is also a Person, so the Person type is referenced.
Private Address address;
}
I map like this:
<Select id = "selectById" parameterType = "int" resultMap = "detailedPerson">
Select *
From TPERSON p
Where p. id = # {id}
</Select>
<ResultMap type = "Person" id = "detailedPerson">
<Association property = "spouse" column = "SPOUSE" javaType = "Person" jdbcType = "INTEGER" select = "PersonMapper. selectById"/>
<Association property = "address" column = "ADDRESS" javaType = "Address" jdbcType = "INTEGER" select = "AddressMapper. selectById"/>
</ResultMap>
But an error is reported. Comment out "<association property =" spouse "column =" SPOUSE "javaType =" Person "jdbcType =" INTEGER "select =" PersonMapper. selectById "/>"
There is no problem. The execution is successful ···
There is no problem with Address association, but there is a problem with the spouse associated with itself ??
After a long time of hard work, I could not. I consulted my friends and colleagues in the same industry ···
Some people suggest doing it in the program. Of course, it can solve the problem, but in that case, the efficiency will be halved, because two queries may be performed to get the desired result ···
So I did my research on it myself, and I don't believe this kind of thing. I can certainly solve this kind of thing in MyBatis ing, because the strength of MyBatis is here, and result ing is the core of MyBatis ···
Finally, I finally thought of the solution as follows:
First, modify the query ing of the Person class <select> to the following:
<Select id = "selectById" parameterType = "int" resultMap = "detailedPerson">
Select
P1.id,
P1.name,
P1.spouse,
P1.address,
<! -- The "as spouse_xxx" in the following three rows should be paid special attention to. The following result ing should be used -->
P2.id as spouse_id,
P2.name as spouse_name,
P2.address as spouse_address
From TPERSON p1
Left join TPERSON p2 on p2.id = p1.spouse
Where p1.id = # {id}
</Select>
Next, modify the result ing of the Person class <resultMap> as follows:
<ResultMap type = "Person" id = "detailedPerson">
<Association property = "spouse" column = "SPOUSE" javaType = "Person" jdbcType = "INTEGER">
<Id property = "id" column = "spouse_id"/>
<Result property = "name" column = "spouse_name"/>
<Association property = "address" column = "spouse_address" javaType = "Address" jdbcType = "INTEGER" select = "AddressMapper. selectById"/>
</Association>
<Association property = "address" column = "ADDRESS" javaType = "Address" jdbcType = "INTEGER" select = "AddressMapper. selectById"/>
</ResultMap>
The Address class ing does not need to be modified (this is not provided here, so you can do it yourself )···
Run the Test Program (for readers to do it by themselves, here). The desired result is displayed, and the JUnit test result turns green ···
Finishing work ···