Mybatis中ResultMap映射

这篇博客详细介绍了Mybatis中的ResultMap映射,如何通过ResultMap将数据库查询结果映射到对应的实体类属性上,包括id和result的用法,以及如何处理一对多关联关系,如User和BlogType的映射。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Mapper中的代码
<resultMap id="BlogMap" type="com.zy.entity.Blog">
    <!--              id代表resultMap的主键,而result代表其属性-->
    <id property="id" column="id"></id>
    <result property="insertTime" column="insertTime"></result>
    <result property="insertUser" column="insertUser"></result>
    <result property="updateTime" column="updateTime"></result>
    <result property="title" column="title1"></result>
    <result property="content" column="content"></result>
    <association property="user"  javaType="com.zy.entity.User">
        <result property="avatar" column="avatar"></result>
    </association>
    <association property="blogType"  javaType="com.zy.entity.BlogType">
        <result property="title" column="title2"></result>
    </association>

</resultMap>

实体类中的属性:

public class Blog {
    public int id;
    public User user;
    public String title;
    public String content;
    public BlogType blogType;
    public Date insertTime;
    public String insertUser;
    public Date updateTime;
    public int views;

}

 

User属性:

 

public class User {
    private int id;
    private String email;
    private String pwd;
    private String userName;
    private String nickName;
    private  String avatar;
    private int age;
    private  String introduction;
    Date registTime;
    Date updateTime;}

BlogType属性:

 

public class BlogType {
    public int id;
    User user;
    public String title;
    public Date insertTime;
    public Date updateTime;
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值