How does mybatis receive the return result with count?

query the top N users with the largest number of courses, and the number of courses should also be queried

the corresponding sql is as follows:

<select id="getUserWithMostFans" resultMap="">
    SELECT t.userId,t.selectNum
    FROM
    (
        SELECT
            userId,
            count(course) AS selectNum
        FROM
            user_course
        GROUP BY
            userId
        ORDER BY
            selectNum
    ) t  LIMIT 10

question:
1. How do I receive the return value?
of course you can use map to receive the return value, but map is generally not recommended.
in addition, if you define a resultMap, you also need to specify a type

<resultMap id="" type="?????">
    <result   />
    <result   />
</resultMap>

if so, you also need to define another object .
2. If you define another object, does that object count as DO? Or should it be treated as VO?
VO is obviously not appropriate either. And DO corresponds to the database field one by one, so the return result of the database should not belong to the category of DO, right?

I hope you can give me a lot of help. Thank you!

May.01,2021

suggest scenario 2, which defines a new object. Key in Map needs to be maintained in other ways (usually annotations or doc), are much more expensive to maintain later.

DO corresponds to the table structure and encapsulates business logic, which is an inappropriate name.
VO is related to presentation, which is more appropriate. Of course, it can also be called DTO

.
Menu