广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Mybatis关联查询结果集对象嵌套的具体使用
  • 203
分享到

Mybatis关联查询结果集对象嵌套的具体使用

2024-04-02 19:04:59 203人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

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

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

数据模型WeixinActivity2018User.java

public class WeixinActivity2018User  implements Serializable{

    
    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<WeixinActivity2018UserAssist> activity2018UserAssists;//点赞用户信息

数据模型WeixinActivity2018UserAssist.java

public class WeixinActivity2018UserAssist  implements Serializable{

    
    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

  <resultMap 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" />
    <collection  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" />
    </collection>
  </resultMap>
  <sql id="Base_Column_List">
    openid,unionid,phone,nickname,headurl,count,createtime,uptime
  </sql>
  <insert 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()
    )
  </insert>
    <select id="selectTalCount" resultType="java.lang.Integer"  parameterType="java.util.Map">
        SELECT  count(twau.id)
        from  t_weixin_activity_2018_user twau
        <if test="openid != '' and openid != null">
            and twau.openid = #{openid}
        </if>
        <if test="id != 0 and id != null">
            and twau.id = #{id}
        </if>
        <if test="phone != '' and phone != null">
            and twau.phone = #{phone}
        </if>
    </select>
    <select id="queryActivity2018User" parameterType="java.util.Map" resultMap="BaseResultMap" >
        SELECT 
         twau.id,twau.openid, twau.unionid, twau.phone, twau.nickname, twau.headurl, twau.count,date_fORMat( twau.createtime, '%Y-%m-%d %H:%m:%s') as createtime,date_format( twau.uptime, '%Y-%m-%d %H:%m:%s') as uptime
        ,twaua.aid,twaua.uid,twaua.openid as aopenid,twaua.unionid as aunionid,twaua.nickname as anickname,twaua.headurl as aheadurl,date_format(twaua.createtime, '%Y-%m-%d %H:%m:%s') as acreatetime ,
        twaua.phone as aphone
        from  t_weixin_activity_2018_user twau LEFT  JOIN t_weixin_activity_2018_user_assist twaua on twau.id=twaua.uid  where 1=1
        <if test="openid != '' and openid != null">
            and twau.openid = #{openid}
        </if>
        <if test="id != 0 and id != null">
            and twau.id = #{id}
        </if>
        <if test="phone != '' and phone != null">
            and twau.phone = #{phone}
        </if>
        <if test="start != '' and start != null  and start != 0">
            order by tlb.createtime desc limit ${start}, ${number}
        </if>
  </select>

到此这篇关于Mybatis关联查询结果集对象嵌套的具体使用的文章就介绍到这了,更多相关Mybatis关联查询对象嵌套 内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Mybatis关联查询结果集对象嵌套的具体使用

本文链接: https://www.lsjlt.com/news/140145.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作