Mybatis關聯(lián)查詢結果集對象嵌套的具體使用
在查詢時經常出現一對多”的關系,所有會出現嵌套對象的情況,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;//創(chuàng)建時間 ? ? 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>
到此這篇關于Mybatis關聯(lián)查詢結果集對象嵌套的具體使用的文章就介紹到這了,更多相關Mybatis關聯(lián)查詢對象嵌套 內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解Springboot2.3集成Spring security 框架(原生集成)
這篇文章主要介紹了詳解Springboot2.3集成Spring security 框架(原生集成),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08SpringBoot整合Hibernate Validator實現參數驗證功能
這篇文章主要介紹了SpringBoot整合Hibernate Validator實現參數驗證功能,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06SpringCloudAlibaba整合Feign實現遠程HTTP調用的簡單示例
這篇文章主要介紹了SpringCloudAlibaba 整合 Feign 實現遠程 HTTP 調用,文章中使用的是OpenFeign,是Spring社區(qū)開發(fā)的組件,需要的朋友可以參考下2021-09-09