0

Suppose we have the following mapping in MyBatis:

 <resultMap id="map_X" type="Wrapper">   
    <result column="....." property="totalCount" />     

    <collection property="someArray" ofType="hashmap">
        <result column="A" property="first" />
        <result column="B" property="second" />
    </collection>
</resultMap>

<select id="mySelect" resultMap="myMap">
    SELECT A, B FROM myTable
</select>

How to set the count of rows in the query into the property totalCount of resulting Wrapper object.

2 Answers2

0

Your issue can be solved with SQL alone. MyBatis is not the issue here.

<select id="mySelect" resultMap="myMap">
   SELECT A, B, (SELECT COUNT(*) FROM myTable) AS totalCount
   FROM myTable
</select>

Not sure if your resultMap is correct since the id's in your select don't match.

Here is a thread with more detailed answers.

raupach
  • 3,092
  • 22
  • 30
-2

What you are trying to do is possible yet a bad match for MyBatis and SQL alike. I would solve this Java-side. e.g.:

public int getTotalCount() {
  return someArray.size()
}
Fritz Duchardt
  • 11,026
  • 4
  • 41
  • 60