mybatis 实体嵌套查询

11 篇文章 0 订阅
原文链接: https://my.oschina.net/u/1462828/blog/1595220

本例子只是一个实体类嵌套另外一个或者多个实体类。在实体类嵌套一个实体列表的我还没研究出来。

首先上实体类:

public class AppPost {
    private String postId;
    private String postTitle;
    private int upTime;
    private int postType;
    private int pointsNum;
    private int collectionNum;
    private int replyNum;
    private int transferNum;
    private int isHost;
    private int contentType;
    private AppUserMini user;
}
 
 
public class AppUserMini {
    private String userId;
    private String userName;
    private Integer userSex;
    private Integer userAge;
    private String userImg;
    private String userAddress;
}

然后其他乱七八糟的就不说了,直接上xml的代码:

<resultMap id="Base_app_post" type="<路径省略>.AppPost">
    <result column="post_id" property="postId"/>
    <result column="post_title" property="postTitle"/>
    <result column="up_time" property="upTime"/>
    <result column="post_type" property="postType"/>
    <result column="points_num" property="pointsNum"/>
    <result column="collection_num" property="collectionNum"/>
    <result column="reply_num" property="replyNum"/>
    <result column="transfer_num" property="transferNum"/>
    <result column="is_host" property="isHost"/>
    <result column="content_type" property="contentType"/>
 
 
    <association property="user" javaType="<路径省略>.AppUserMini" resultMap="Base_app_user" />
  </resultMap>
 
  <resultMap id="Base_app_user" type="<路径省略>.AppUserMini" >
    <result column="user_id" property="userId"/>
    <result column="user_name" property="userName"/>
    <result column="user_sex" property="userSex"/>
    <result column="user_age" property="userAge"/>
    <result column="user_img" property="userImg"/>
    <result column="user_address" property="userAddress"/>
  </resultMap>
 
  <select id="selectAppByPostId" resultMap="Base_app_post" parameterType="java.lang.String" >
    select a.post_id,a.post_title,a.up_time,a.post_type,a.points_num,a.collection_num,a.reply_num,a.transfer_num,a.is_hot,a.content_type,b.user_id,b.user_name,b.user_sex,b.user_age,b.user_img,b.user_address
    from service_post a,sys_user b
    where a.post_id = #{postId,jdbcType=INTEGER} AND a.user_id=b.user_id
  </select>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值