Mybatis关联查询结果集对象嵌套的具体使用
作者:FANQIBU 发布时间:2021-07-12 22:09:18
标签: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<WeixinActivity2018UserAssist> 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
<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>
来源:https://blog.csdn.net/BuFanQi_Info/article/details/78973671


猜你喜欢
- 废话不多说了,关键代码如下所示:import java.util.*;public class Demo04 {public static
- 哈希表(HashMap)hash查询的时间复杂度是O(1)按值传递Character,Short,Integer,Long, Float,D
- 如果JDBC连接是在自动提交模式下,它在默认情况下,那么每个SQL语句都是在其完成时提交到数据库。这可能是对简单的应用程序,但有三个原因,你
- 一、概述有不少人问我intellij idea的mybatis-plus自动生成插件使用出现各种各样的问题,所以我在这边博客的基础上添加一些
- 一、背景在开发过程中,我们的软件会面对不同的运行环境,比如开发环境、测试环境、生产环境,而我们的软件在不同的环境中,有的配置可能会不一样,比
- 现在,让我们找出“如何学习 Java 编程”的答案。通过承认您是初学者这一事实开始您的学习之旅很重要
- 本文主要介绍的是一个球形水波的百分比控件,市面上有各种形形色色的百分比控件,我一直觉得水波是最炫的,UI给了我这个机会,然而网上搜了一大堆,
- 本文实例为大家分享了Java实现图形化界面日历的具体代码,供大家参考,具体内容如下此程序主要功能实现了可以根据用户选择的年月日来定位日期,日
- Java float和double精度范围大小要想理解float和double的取值范围和计算精度,必须先了解小数是如何在计算机中存储的:举
- 前言:在Java8支持Lambda表达式以后,为了满足Lambda表达式的一些典型使用场景,JDK为我们提供了大量常用的函数式接口。它们主要
- 最近接了个项目其中有需要要实现此功能:seekbar需要显示最左和最右值,进度要跟随进度块移动。下面通过此图给大家展示下效果,可能比文字描述
- 公司有一个需求,实现一个多级的树形菜单,并且支持多选功能,实现这个功能之前,我在网上找了找,树形菜单很好找,但是支持多选功能并没有很合适的,
- 原理简介:zookeeper实现分布式锁的原理就是多个节点同时在一个指定的节点下面创建临时会话顺序节点,谁创建的节点序号最小,谁就获得了锁,
- 最近有很多同学,竟然不知道如何使用Intellij IDEA打开Java项目并启动现在来讲一下,希望不要忘记了 1、打开IDEA开机页面 M
- 在研究性能的时候,完全可以使用Stopwatch计时器计算一项技术的效率。但是有时想知道某想技术的性能的时候,又常常想不起可以运用Stopw
- Kotlin this详解及实例为了表示当前函数的接收者(receiver), 们使用this表达式:在类的成员函数中,this指向这个类的
- 我们知道,Spring可以通过包扫描将使用@Component注解定义的Bean定义到容器中。今天就来探究下他实现的原理。首先,找到@Com
- 简单的理解,MyBatis逆向工程,就是通过相应插件,自动生成MyBatis数据库连接的一些文件。mybatis需要编写sql语句,myba
- 写Java程序时经常碰到要读如txt或写入txt文件的情况,但是由于要定义好多变量,经常记不住,每次都要查,特此整理一下,简单易用,方便好懂
- 一、下载Android Studio百度搜索“Android Studio"点击中文社区进入,选择最新版本下载。下载后双击安装包开