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关联查询,对象嵌套
0
投稿
猜你喜欢
解决SpringBoot项目使用多线程处理任务时无法通过@Autowired注入bean问题
2022-06-25 06:48:17
SpringBoot使用Redisson实现分布式锁(秒杀系统)
2022-07-17 05:15:41
Android中WebView用法实例分析
2023-03-20 17:02:51
Android实现webview实例代码
2022-05-28 13:32:32
Java如何实现树的同构?
2023-11-28 09:55:19
C#制作二维柱状图方法
2023-12-22 22:06:47
XListView实现下拉刷新和上拉加载原理解析
2022-02-16 06:47:52
Android 中的危险权限详细整理
2023-12-03 09:07:25
Spring Boot如何使用Spring Security进行安全控制
2022-03-26 03:59:41
C#中图片、二进制与字符串的相互转换方法
2023-05-16 13:07:50
使用Prometheus+Grafana的方法监控Springboot应用教程详解
2023-10-31 13:08:12
android 获取视频第一帧作为缩略图的方法
2022-11-22 19:30:25
解决@RequestBody部分属性丢失的问题
2023-08-01 15:00:21
Spring+SpringMVC+MyBatis深入学习及搭建(三)之MyBatis全局配置文件解析
2022-03-21 05:25:23
详解Java并发包中线程池ThreadPoolExecutor
2022-03-23 19:57:20
基于java的opencv开发过程详解
2022-03-31 20:02:59
java自动装箱拆箱深入剖析
2023-09-03 08:45:19
SpringBoot登录用户权限拦截器
2022-07-15 04:18:04
mybatis一直加载xml,找到错误的解决方案
2022-08-12 14:55:26
简单探索 Java 中的惰性计算
2023-11-16 20:25:28