Mybatis关联查询结果集对象嵌套的具体使用
作者:FANQIBU 时间:2021-07-12 22:09:18
在查询时经常出现一对多”的关系,所有会出现嵌套对象的情况,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
标签:Mybatis关联查询,对象嵌套
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
高斯混合模型与EM算法图文详解
2022-10-02 12:05:02
![](https://img.aspxhome.com/file/2023/5/61855_0s.jpg)
JSON.toJSONString()空字段不忽略修改的问题
2023-06-16 03:12:37
Java中接收键盘输入的三种方法
2023-11-13 16:11:29
JAVA抽象类,接口,内部类详解
2023-11-09 16:37:25
花样使用Handler与源码分析
2023-07-30 08:36:31
Maven插件的安装及使用
2022-04-05 04:38:30
![](https://img.aspxhome.com/file/2023/5/64155_0s.jpg)
JAVA8 Stream流中的reduce()方法详解
2023-09-01 21:10:33
Flutter瀑布流仿写原生的复用机制详解
2023-06-20 17:02:08
![](https://img.aspxhome.com/file/2023/0/98700_0s.jpg)
Java字符串技巧之删除标点或最后字符的方法
2023-02-06 21:42:50
浅谈SpringMVC的执行流程
2023-09-30 17:59:59
![](https://img.aspxhome.com/file/2023/8/62358_0s.png)
搞懂Java线程池
2021-08-04 10:01:06
![](https://img.aspxhome.com/file/2023/5/62345_0s.jpg)
Flutter使用sqflite处理数据表变更的方法详解
2023-10-21 11:05:49
![](https://img.aspxhome.com/file/2023/3/139083_0s.webp)
Java设计通用的返回数据格式过程讲解
2023-11-09 00:16:40
SpringMVC拦截器创建配置及执行顺序
2023-06-06 20:41:16
![](https://img.aspxhome.com/file/2023/1/62351_0s.png)
C语言中求字符串长度的函数的几种实现方法
2023-07-04 23:29:05
java集合与数组的相同点和不同点
2022-07-19 03:13:53
java中Class.forName的作用浅谈
2023-11-11 12:30:26
SpringBoot绿叶显示yml和端口问题及解决方法
2023-12-09 00:29:13
![](https://img.aspxhome.com/file/2023/5/62775_0s.png)
Java的Struts框架简介与环境配置教程
2023-10-29 05:23:03
![](https://img.aspxhome.com/file/2023/8/58728_0s.jpg)
快速理解Java垃圾回收和jvm中的stw
2021-09-06 20:27:17