溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

mybatis collection 多條件查詢的實(shí)現(xiàn)方法

發(fā)布時(shí)間:2020-08-21 01:28:04 來(lái)源:腳本之家 閱讀:388 作者:焦點(diǎn) 欄目:數(shù)據(jù)庫(kù)

mybatis collection 多條件查詢的實(shí)現(xiàn)方法

前言:

業(yè)務(wù)需要通過(guò)mybatis 查詢返回嵌套集合,嫌多次查詢太麻煩,用自帶的高級(jí)查詢解決問題,下邊是代碼,已測(cè)試通過(guò)。

說(shuō)下自己的理解,就是一個(gè)主查詢結(jié)果集里面嵌套了子查詢的結(jié)果集,可以是多個(gè)子查詢,每個(gè)子查詢的條件從主查詢結(jié)果集中獲取,返回值各自定義。collection 標(biāo)簽的property是主查詢里面集合的名字,如果有多個(gè)就再寫個(gè)collection,column是子查詢參數(shù),單參數(shù)直接寫主查詢結(jié)合返回結(jié)果,例如直接寫上user_id,要是數(shù)據(jù)庫(kù)的字段,多條件就封裝下,例如{userId=user_id,theme=theme},然后子查詢的parameterType寫"java.util.Map",多條件查詢好像只有mybatis3.0以后才有,看網(wǎng)上資料說(shuō)的,沒驗(yàn)證過(guò),ofType是集合里的對(duì)象,select是對(duì)應(yīng)下面的語(yǔ)句

實(shí)現(xiàn)代碼:

<resultMap id="BaseResultMap" type="web.model.UserMessage" > 
 <id column="id" property="id" jdbcType="INTEGER" /> 
 <result column="user_id" property="userId" jdbcType="INTEGER" /> 
 <result column="theme" property="theme" jdbcType="VARCHAR" /> 
 <result column="status" property="status" jdbcType="INTEGER" /> 
 <result column="theme_time" property="themeTime" jdbcType="TIMESTAMP" /> 
 <result column="create_time" property="createTime" jdbcType="TIMESTAMP" /> 
 <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" /> 
 <result column="yn" property="yn" jdbcType="INTEGER" /> 
 </resultMap> 
 
<resultMap id="BaseVoResultMap" type="web.model.vo.UserMessageVo" extends="BaseResultMap"> 
 <collection property="userMessageDetailList" column="{userId=user_id,theme=theme}" javaType="java.util.ArrayList" ofType="web.model.UserMessageDetail" select="selectUserMessageDetailById"/> 
 </resultMap> 
 
<resultMap id="BaseDetailResultMap" type="web.model.UserMessageDetail" > 
 <id column="id" property="id" jdbcType="INTEGER" /> 
 <result column="user_message_id" property="userMessageId" jdbcType="INTEGER" /> 
 <result column="sponsor_id" property="sponsorId" jdbcType="INTEGER" /> 
 <result column="user_id" property="userId" jdbcType="INTEGER" /> 
 <result column="user_type" property="userType" jdbcType="INTEGER" /> 
 <result column="provider_id" property="providerId" jdbcType="INTEGER" /> 
 <result column="message" property="message" jdbcType="VARCHAR" /> 
 <result column="status" property="status" jdbcType="INTEGER" /> 
 <result column="message_time" property="messageTime" jdbcType="TIMESTAMP" /> 
 <result column="create_time" property="createTime" jdbcType="TIMESTAMP" /> 
 <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" /> 
 <result column="yn" property="yn" jdbcType="INTEGER" /> 
 </resultMap> 
 
 <select id="selectUserMessageById" resultMap="BaseVoResultMap" parameterType="java.lang.Integer"> 
 select * from user_message where user_id = #{userId,jdbcType=INTEGER} 
 </select> 
 
 <select id="selectUserMessageDetailById" resultMap="BaseDetailResultMap" parameterType="java.util.Map"> 
 select * from user_message_detail where user_id = #{userId,jdbcType=INTEGER} and message = #{theme,jdbcType=VARCHAR} 
 </select> 

selectUserMessageById,這個(gè)是主查詢,VO只多了個(gè) private List<UserMessageDetail> userMessageDetailList,

這個(gè)屬性,下面是返回值結(jié)構(gòu)截圖

mybatis collection 多條件查詢的實(shí)現(xiàn)方法

如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI