53

Mybatis中的assocication和collection

 5 years ago
source link: https://aodeng.cc/archives/associcationcollectio?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK