I have a certain resultset in MYSQL database using the sakila database on actors and its related tables:
actor_id first_name last_name title
1 PENELOPE GUINESS ANACONDA CONFESSIONS
1 PENELOPE GUINESS ANGELS LIFE
I have successfully map the following data in MyBatis Plus using the following result map:
<resultMap id="actor" type="java.util.TreeMap">
<result property="id" column="id" />
<result property="firstname" column="first_name" />
<result property="lastname" column="last_name" />
<collection property="movies" javaType="ArrayList" resultMap="movies" />
</resultMap>
<resultMap id="movies" type="java.util.TreeMap">
<result property="title" column="title" />
</resultMap>
The SQL query looked like this:
<select id="getActors" parameterType="ActorDetailsDTO" resultMap="actor">
select
a.actor_id,
first_name,
last_name,
title
from actor a left join
film_actor fa on a.actor_id = fa.actor_id left join
film f on fa.film_id = f.film_id
where a.actor_id = #{id}
</select>
This, in turn achieve the following result:
result :{
"id" : 1,
"movies":[
{
title : "ANACONDA CONFESSIONS"
},
{
title : "ANGELS LIFE"
},
],
"firstname" : "PENELOPE",
"lastname" : "GUINESS"
}
But I find it difficult to add a "non-existent" column name "movielist" with a value of "blockbuster X" where X is an incrementing number from 1 to N.
I hope to achieve this result:
result :{
"id" : 1,
"movies":[
{
"movielist" : "blockbuster 1",
"title" : "ANACONDA CONFESSIONS"
},
{
"movielist" : "blockbuster 2",
"title" : "ANGELS LIFE"
},
],
"firstname" : "PENELOPE",
"lastname" : "GUINESS"
}
And, of course, if I have two or more actors, the "blockbuster X" will rest back to 1 to the second actor.
What I've done in the SQL query is I add a variable counter, but the count just continues on counting, not resetting for a new actor.
Thank you for any help you could give.
It should be possible with variables, but here is another approach using a subquery.
<select id="getActors" resultMap="actor">
select
a.actor_id,
first_name,
last_name,
fa.film_id,
concat('blockbuster ', (select count(*) from film_actor fa2
where a.actor_id = fa2.actor_id and fa2.film_id <= fa.film_id)) movielist,
title
from actor a
left join film_actor fa on a.actor_id = fa.actor_id
left join film f on fa.film_id = f.film_id
order by a.actor_id, fa.film_id;
</select>
And here are the result maps.
<id />
elements are added to improve efficiency.
<resultMap id="actor" type="java.util.TreeMap">
<id property="id" column="actor_id" />
<result property="firstname" column="first_name" />
<result property="lastname" column="last_name" />
<collection property="movies" javaType="list"
resultMap="movies" />
</resultMap>
<resultMap id="movies" type="java.util.TreeMap">
<id column="film_id" /><!-- not stored in map -->
<result property="movielist" column="movielist"
javaType="string" /><!-- CONCAT returns VARBINARY -->
<result property="title" column="title" />
</resultMap>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.