Mybatis关联查询结果集对象嵌套

在查询时经常出现一对多”的关系,所有会出现嵌套对象的情况,Mybatis在resultMap提供了collection标签,本文适合有一定Mybatis基础的读者查阅

  • 数据模型WeixinActivity2018User.java

public class WeixinActivity2018User  implements Serializable{

    /** serialVersionUID*/
    private static final long serialVersionUID = -2740162776768956231L;

    private int id;
    private String nickname;  //昵称
    private String headurl;   //头像
    private String openid;    //微信用户OpenId
    private String unionid;
    private String phone;     //用户手机号
    private int count;        //积攒数
    private String createtime;//创建时间
    private String uptime;    //更新时间
    private List activity2018UserAssists;//点赞用户信息
  • 数据模型WeixinActivity2018UserAssist.java
public class WeixinActivity2018UserAssist  implements Serializable{

    /** serialVersionUID*/
    private static final long serialVersionUID = -2740162776768956232L;

    private int aid;
    private int uid;
    private String nickname;
    private String headurl;
    private String openid;
    private String unionid;
    private String createtime;
  • WeixinActivity2018UserMapper.xml
  id="BaseResultMap" type="com.lh.wx.model.WeixinActivity2018User">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="openid" jdbcType="VARCHAR" property="openid" />
    <result column="unionid" jdbcType="VARCHAR" property="unionid" />
    <result column="phone" jdbcType="VARCHAR" property="phone" />
    <result column="nickname" jdbcType="VARCHAR" property="nickname" />
    <result column="headurl" jdbcType="VARCHAR" property="headurl" />
    <result column="count" jdbcType="INTEGER" property="count" />
    <result column="createtime" jdbcType="DATE" property="createtime" />
    <result column="uptime" jdbcType="DATE" property="uptime" />
    property="activity2018UserAssists"  ofType="com.lh.wx.model.WeixinActivity2018UserAssist">
            <id property="aid" column="aid" />
             <result column="uid" jdbcType="INTEGER" property="uid" />
            <result column="aopenid" jdbcType="VARCHAR" property="openid" />
            <result column="aunionid" jdbcType="VARCHAR" property="unionid" />
            <result column="anickname" jdbcType="VARCHAR" property="nickname" />
            <result column="aheadurl" jdbcType="VARCHAR" property="headurl" />
            <result column="acreatetime" jdbcType="DATE" property="createtime" />
    
  
  id="Base_Column_List">
    openid,unionid,phone,nickname,headurl,count,createtime,uptime
  
  id="insertActivity2018User"  useGeneratedKeys="true" keyProperty="id" parameterType="com.lh.wx.model.WeixinActivity2018User" >
    insert into t_weixin_activity_2018_user (openid,unionid,phone,nickname,headurl,createtime,uptime)
    values (
        #{openid,jdbcType=VARCHAR},#{unionid,jdbcType=VARCHAR},#{phone,jdbcType=VARCHAR}, #{nickname,jdbcType=VARCHAR}, #{headurl,jdbcType=VARCHAR},now(),now()
    )
  
    
    

你可能感兴趣的:(Mybatis)