yongguangl
yongguangl

Reputation: 87

How to use mybatis annotations to implement relation query

I have the A,B,C three tables, B has the id of A, and C has the id of B. A: B= 1:N, B:C= 1: N, now I query some data in C and also correspond to the data in the AB table. what should I do with mybatis annotations?

Upvotes: 0

Views: 167

Answers (1)

Max Medina
Max Medina

Reputation: 190

if you only need to handle relationship queries it would be easier to use a xml mapper file.

there you could write something like this to get a specific A item with all his B

<resultMap id="aResultMap" type="hello.A">
    <id property="ida" column="id_a"/>
    <collection property="bs" javaType="ArrayList" column="ida" ofType="hello.B" select="selectBs"/>
</resultMap>

<select id="selectOneA" resultMap="aResultMap">
    SELECT * FROM A
    WHERE ida = #{idA}
</select>

<select id="selectBs" resultType="HashMap">
    SELECT * FROM B
    WHERE ida = #{idA}
</select>

You can check the myBatis doc http://www.mybatis.org/mybatis-3/sqlmap-xml.html

Upvotes: 3

Related Questions