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="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,这个属性,下面是返回值结构截图







  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis 中可以通过 `collection` 元素来进行关联表的条件查询。 `collection` 元素用于处理一对多的关联关系,它可以用来映射一个 List 或 Array 对象。下面是一个示例的 mapper.xml 文件: ```xml <select id="findUserWithOrders" parameterType="Integer" resultMap="userResultMap"> SELECT u.*, o.id AS order_id, o.order_no AS order_no, o.status AS order_status FROM users u LEFT JOIN orders o ON u.id = o.user_id WHERE u.id = #{id} ORDER BY o.id </select> <resultMap id="userResultMap" type="User"> <id property="id" column="id"/> <result property="username" column="username"/> <result property="password" column="password"/> <collection property="orders" ofType="Order"> <id property="id" column="order_id"/> <result property="orderNo" column="order_no"/> <result property="status" column="order_status"/> <!-- other result mappings for Order --> </collection> </resultMap> ``` 这个例子中,我们定义了一个名为 `findUserWithOrders` 的查询语句,它接受一个参数类型为 `Integer` 的对象。查询语句中使用了 LEFT JOIN 连接了 `users` 和 `orders` 表,并在 WHERE 子句中使用了传入的参数 `id` 来限制查询结果只返回指定用户的信息。在结果映射中,我们使用了 `collection` 元素来将一个 `User` 对象中的 `orders` 属性映射为一个 `List<Order>` 对象。 在实际使用中,你需要根据自己的数据表结构和业务逻辑来编写对应的 mapper.xml 文件。你可以参考这个示例,根据自己的情况进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值