使用resultmap时如何计数?

时间:2019-05-07 11:02:11

标签: mysql mybatis

我想使用Mybatis结果图进行页面查询,因为where子句中有条件,所以我必须加入Post,但是由于它是一对多关系,因此如何获得正确数量的Blog ?

<select id="selectBlog" resultMap="blogResult">
  select
  B.id as blog_id,
  B.title as blog_title,
  B.author_id as blog_author_id,
  P.id as post_id,
  P.subject as post_subject,
  P.body as post_body
  from Blog B
  left join Post P on B.id = P.blog_id
  where B.id = #{id}
  and P.subject=#{postSubject}
</select>

<resultMap id="blogResult" type="Blog">
  <id property="id" column="blog_id" />
  <result property="title" column="blog_title"/>
  <collection property="posts" ofType="Post">
    <id property="id" column="post_id"/>
    <result property="subject" column="post_subject"/>
    <result property="body" column="post_body"/>
  </collection>
</resultMap>

修改 对不起,我不清楚。这似乎很容易,我找到了解决方法:

<select id="count" resultType="java.lang.Integer">
  select
  count(distinct B.id)
  from Blog B
  left join Post P on B.id = P.blog_id
  where B.id = #{id}
  and P.subject=#{postSubject}
</select>

0 个答案:

没有答案