Mybatis關(guān)聯(lián)查詢(xún)結(jié)果集對(duì)象嵌套的具體使用
在查詢(xún)時(shí)經(jīng)常出現(xiàn)一對(duì)多”的關(guān)系,所有會(huì)出現(xiàn)嵌套對(duì)象的情況,Mybatis在resultMap提供了collection標(biāo)簽,本文適合有一定Mybatis基礎(chǔ)的讀者查閱
數(shù)據(jù)模型WeixinActivity2018User.java
public class WeixinActivity2018User ?implements Serializable{
? ? /** serialVersionUID*/
? ? private static final long serialVersionUID = -2740162776768956231L;
? ? private int id;
? ? private String nickname; ?//昵稱(chēng)
? ? private String headurl; ? //頭像
? ? private String openid; ? ?//微信用戶(hù)OpenId
? ? private String unionid;
? ? private String phone; ? ? //用戶(hù)手機(jī)號(hào)
? ? private int count; ? ? ? ?//積攢數(shù)
? ? private String createtime;//創(chuàng)建時(shí)間
? ? private String uptime; ? ?//更新時(shí)間
? ? private List<WeixinActivity2018UserAssist> activity2018UserAssists;//點(diǎn)贊用戶(hù)信息數(shù)據(jù)模型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>到此這篇關(guān)于Mybatis關(guān)聯(lián)查詢(xún)結(jié)果集對(duì)象嵌套的具體使用的文章就介紹到這了,更多相關(guān)Mybatis關(guān)聯(lián)查詢(xún)對(duì)象嵌套 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Springboot2.3集成Spring security 框架(原生集成)
這篇文章主要介紹了詳解Springboot2.3集成Spring security 框架(原生集成),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
詳解springboot shiro jwt實(shí)現(xiàn)權(quán)限管理
為什么使用jwt呢,因?yàn)榭梢酝ㄟ^(guò)URL,POST參數(shù)或者在HTTP header發(fā)送,因?yàn)閿?shù)據(jù)量小,傳輸速度也很快。本篇通過(guò)具體代碼來(lái)進(jìn)行詳情解析,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值2021-09-09
SpringBoot整合Hibernate Validator實(shí)現(xiàn)參數(shù)驗(yàn)證功能
這篇文章主要介紹了SpringBoot整合Hibernate Validator實(shí)現(xiàn)參數(shù)驗(yàn)證功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
SpringCloudAlibaba整合Feign實(shí)現(xiàn)遠(yuǎn)程HTTP調(diào)用的簡(jiǎn)單示例
這篇文章主要介紹了SpringCloudAlibaba 整合 Feign 實(shí)現(xiàn)遠(yuǎn)程 HTTP 調(diào)用,文章中使用的是OpenFeign,是Spring社區(qū)開(kāi)發(fā)的組件,需要的朋友可以參考下2021-09-09
Java常用的時(shí)間類(lèi)以及其轉(zhuǎn)化方式
這篇文章主要介紹了Java常用的時(shí)間類(lèi)以及其轉(zhuǎn)化方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
Java中ThreadLocal線(xiàn)程變量的實(shí)現(xiàn)原理
本文主要介紹了Java中ThreadLocal線(xiàn)程變量的實(shí)現(xiàn)原理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06

