Mybatis中的assocication和collection

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/aodeng110/article/details/84553008

Mybatis中的assocication和collection

第一个是用来关联实体类的,第二个是用来关联集合的,也就是平时的一对一(assocication),一对多(collection)

has one 举例:一个人一张身份证


这是一个人,加一张身份证

public class User {
    private String userName;
    private Card card;
}

这是 一张身份证

public class Card {
    private String cardNum;//身份证号
}

sql

<select id="findOneUserById" parameterType="int" resultMap="userMap">
SELECT 
            u.*,
            c.*
            FROM user u
            left join card c on c.id=u.id
            where 1=1  and u.Id=#{id}
</select>

map

<resultMap type="User" id="userMap">
   <result property="userName"  column="user_name"/>
   <association property="card"  column="card_id"  javaType="Card">
       <id property="cardId"  column="card_id"/>
       <result property="cardNum"  column="card_num"/>
   </association>
</resultMap>

has many

见我上一篇文章即可
https://aodeng.cc/archives/mybatisgaoji

理解最重要

我的博客:https://aodeng.cc 我的公众号:低调小熊猫 我的QQ群:756796932

猜你喜欢

转载自blog.csdn.net/aodeng110/article/details/84553008
今日推荐