mybatiscollection多条件查询的实现方法
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
mybatiscollection多条件查询的实现⽅法
mybatis collection 多条件查询的实现⽅法
前⾔:
业务需要通过mybatis 查询返回嵌套集合,嫌多次查询太⿇烦,⽤⾃带的⾼级查询解决问题,下边是代码,已测试通过。
说下⾃⼰的理解,就是⼀个主查询结果集⾥⾯嵌套了⼦查询的结果集,可以是多个⼦查询,每个⼦查询的条件从主查询结果集中获取,返回值各⾃定义。
collection 标签的property是主查询⾥⾯集合的名字,如果有多个就再写个collection,column是⼦查询参数,单参数直接写主查询结合返回结果,例如直接写上user_id,要是数据库的字段,多条件就封装下,例如{userId=user_id,theme=theme},然后⼦查询的parameterType写"java.util.Map",多条件查询好像只有mybatis3.0以后才有,看⽹上资料说的,没验证过,ofType是集合⾥的对象,select是对应下⾯的语句
实现代码:
<resultMap id="BaseResultMap" type="erMessage" >
<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="erMessageVo" extends="BaseResultMap">
<collection property="userMessageDetailList" column="{userId=user_id,theme=theme}" javaType="java.util.ArrayList" ofType="erMessageDetail" select="selectUserMessageDetailById"/> </resultMap>
<resultMap id="BaseDetailResultMap" type="erMessageDetail" >
<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="ng.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,
这个属性,下⾯是返回值结构截图
如有疑问请留⾔或者到本站社区交流讨论,感谢阅读,希望能帮助到⼤家,谢谢⼤家对本站的⽀持!。