mybatis collection 多條件查詢的實現(xiàn)方法
mybatis collection 多條件查詢的實現(xiàn)方法
前言:
業(yè)務需要通過mybatis 查詢返回嵌套集合,嫌多次查詢太麻煩,用自帶的高級查詢解決問題,下邊是代碼,已測試通過。
說下自己的理解,就是一個主查詢結果集里面嵌套了子查詢的結果集,可以是多個子查詢,每個子查詢的條件從主查詢結果集中獲取,返回值各自定義。collection 標簽的property是主查詢里面集合的名字,如果有多個就再寫個collection,column是子查詢參數,單參數直接寫主查詢結合返回結果,例如直接寫上user_id,要是數據庫的字段,多條件就封裝下,例如{userId=user_id,theme=theme},然后子查詢的parameterType寫"java.util.Map",多條件查詢好像只有mybatis3.0以后才有,看網上資料說的,沒驗證過,ofType是集合里的對象,select是對應下面的語句
實現(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,這個是主查詢,VO只多了個 private List<UserMessageDetail> userMessageDetailList,
這個屬性,下面是返回值結構截圖

如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- Mybatis中collection和association的使用區(qū)別詳解
- Mybatis中Collection集合標簽的使用詳解
- 詳解mybatis foreach collection示例
- mybatis 中 foreach collection的用法小結(三種)
- 解決mybatis 中collection嵌套collection引發(fā)的bug
- 詳解mybatis collection標簽一對多的使用
- mybatis使用collection嵌套查詢的實現(xiàn)
- Mybatis之association和collection用法
- MyBatis中的collection兩種使用方法及效率比較
相關文章
使用mongovue把sqlserver數據導入mongodb的步驟
MongoVUE免費版支持MySQL導入Mongo,所以思路是SQLServer導入MySQL,再從MySQL導入Mongo。2014-01-01
SqlServer 2022通過臨時表和游標遍歷方式邏輯處理獲取目標數據
在SQL的存儲過程,函數中,經常需要使用遍歷(遍歷table),其中游標、臨時表等遍歷方法很常用,本文就來介紹一下SqlServer 2022通過臨時表和游標遍歷方式邏輯處理獲取目標數據,感興趣的可以了解一下2024-04-04
Java 實現(xiàn)連接sql server 2000
JDBC技術事實上是一種能通過JAVA語言訪問任何結構化數據庫的應用程序接口(API)(Sun這樣說的,我也不知道是不是真的),而且現(xiàn)在的JDBC 3.0據Sun說也能訪問Execel等電子表格程序!2009-06-06

