Mybatis關(guān)聯(lián)查詢結(jié)果集對象嵌套的具體使用
在查詢時經(jīng)常出現(xiàn)一對多”的關(guān)系,所有會出現(xiàn)嵌套對象的情況,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; ?//昵稱
? ? private String headurl; ? //頭像
? ? private String openid; ? ?//微信用戶OpenId
? ? private String unionid;
? ? private String phone; ? ? //用戶手機號
? ? private int count; ? ? ? ?//積攢數(shù)
? ? private String createtime;//創(chuàng)建時間
? ? private String uptime; ? ?//更新時間
? ? private List<WeixinActivity2018UserAssist> activity2018UserAssists;//點贊用戶信息數(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)查詢結(jié)果集對象嵌套的具體使用的文章就介紹到這了,更多相關(guān)Mybatis關(guān)聯(lián)查詢對象嵌套 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java基礎(chǔ)學(xué)習(xí)之字符串知識總結(jié)
今天帶著大家復(fù)習(xí)一下Java基礎(chǔ)知識-字符串,文中介紹的非常詳細,對初步學(xué)習(xí)Java或者復(fù)習(xí)Java的小伙伴們都很有幫助喲,需要的朋友可以參考下2021-05-05
Spring Boot數(shù)據(jù)庫鏈接池配置方法
這篇文章主要介紹了Spring Boot數(shù)據(jù)庫鏈接池配置方法,需要的朋友可以參考下2017-04-04
Java線程池的幾種實現(xiàn)方法和區(qū)別介紹實例詳解
本篇文章主要介紹了Java線程池的幾種實現(xiàn)方法和區(qū)別,需要的朋友可以參考2017-04-04
對Mybatis?Plus中@TableField的使用正解
這篇文章主要介紹了對Mybatis?Plus中@TableField的使用正解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
RestTemplate對HttpClient的適配源碼解讀
這篇文章主要為大家介紹了RestTemplate對HttpClient的適配源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10
解決springboot集成swagger碰到的坑(報404)
這篇文章主要介紹了解決springboot集成swagger碰到的坑(報404),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06

