简体   繁体   English

如何动态选择resultMap并将递增的整数值附加到MyBatis中的“不存在”列?

[英]How to dynamically select a resultMap and append an incrementing integer value to a “non-existent” column in MyBatis?

I have a certain resultset in MYSQL database using the sakila database on actors and its related tables: 我在MYSQL数据库中使用了关于actors及其相关表的sakila数据库的某个结果集:

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: 我已使用以下结果映射在MyBatis Plus中成功映射了以下数据:

    <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: SQL查询看起来像这样:

<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. 但我发现很难添加一个“不存在的”列名“movielist”,其值为“blockbuster X”,其中X是从1到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. 而且,当然,如果我有两个或更多的演员,“重磅炸弹X”将重新回到第二个演员的1。

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. 我在SQL查询中所做的是添加一个变量计数器,但计数只是继续计数,而不是为新的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. <id />元素以提高效率。

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM