mybatis collection 多条件查询

转自:https://blog.csdn.net/di315362886/article/details/60144410

业务需要通过mybatis 查询返回嵌套集合,嫌多次查询太麻烦,用自带的高级查询解决问题,下边是代码,已测试通过

说下自己的理解,就是一个主查询结果集里面嵌套了子查询的结果集,可以是多个子查询,每个子查询的条件从主查询结果集中获取,返回值各自定义。

collection 标签的property是主查询里面集合的名字,如果有多个就再写个collection,column是子查询参数,单参数直接写主查询结合返回结果,例如直接写上user_id,要是数据库的字段,多条件就封装下,例如{userId=user_id,theme=theme},然后子查询的parameterType写"java.util.Map",多条件查询好像只有mybatis3.0以后才有,看网上资料说的,没验证过,ofType是集合里的对象,select是对应下面的语句

补充:javaType ="java.util.ArrayList" column中的 参数需要collection 上一级sql查询出来(BaseResultMap中需查询user_id,theme

[java]  view plain  copy
  1. <resultMap id="BaseResultMap" type="web.model.UserMessage" >  
  2.     <id column="id" property="id" jdbcType="INTEGER" />  
  3.     <result column="user_id" property="userId" jdbcType="INTEGER" />  
  4.     <result column="theme" property="theme" jdbcType="VARCHAR" />  
  5.     <result column="status" property="status" jdbcType="INTEGER" />  
  6.     <result column="theme_time" property="themeTime" jdbcType="TIMESTAMP" />  
  7.     <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />  
  8.     <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />  
  9.     <result column="yn" property="yn" jdbcType="INTEGER" />  
  10.   </resultMap>  
  11.   
  12. <resultMap id="BaseVoResultMap" type="web.model.vo.UserMessageVo" extends="BaseResultMap">  
  13.     <collection property="userMessageDetailList" column="{userId=user_id,theme=theme}" javaType="java.util.ArrayList" ofType="web.model.UserMessageDetail" select="selectUserMessageDetailById"/>  
  14.   </resultMap>  
  15.   
  16. <resultMap id="BaseDetailResultMap" type="web.model.UserMessageDetail" >  
  17.     <id column="id" property="id" jdbcType="INTEGER" />  
  18.     <result column="user_message_id" property="userMessageId" jdbcType="INTEGER" />  
  19.     <result column="sponsor_id" property="sponsorId" jdbcType="INTEGER" />  
  20.     <result column="user_id" property="userId" jdbcType="INTEGER" />  
  21.     <result column="user_type" property="userType" jdbcType="INTEGER" />  
  22.     <result column="provider_id" property="providerId" jdbcType="INTEGER" />  
  23.     <result column="message" property="message" jdbcType="VARCHAR" />  
  24.     <result column="status" property="status" jdbcType="INTEGER" />  
  25.     <result column="message_time" property="messageTime" jdbcType="TIMESTAMP" />  
  26.     <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />  
  27.     <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />  
  28.     <result column="yn" property="yn" jdbcType="INTEGER" />  
  29.   </resultMap>  
  30.   
  31.   <select id="selectUserMessageById" resultMap="BaseVoResultMap" parameterType="java.lang.Integer">  
  32.     select * from user_message where user_id = #{userId,jdbcType=INTEGER}  
  33.   </select>  
  34.   
  35.   <select id="selectUserMessageDetailById" resultMap="BaseDetailResultMap" parameterType="java.util.Map">  
  36.     select * from user_message_detail where user_id = #{userId,jdbcType=INTEGER} and message = #{theme,jdbcType=VARCHAR}  
  37.   </select>  

猜你喜欢

转载自blog.csdn.net/qq_35804654/article/details/80774985