V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
77yf77yf77yf
V2EX  ›  Java

大佬们帮忙看个问题

  •  
  •   77yf77yf77yf · 2023-01-03 19:52:49 +08:00 · 1169 次点击
    这是一个创建于 688 天前的主题,其中的信息可能已经有所发展或是发生改变。

    这当中 episode 的 id 获取为 null ,请问是哪里出了问题

    @Select("select * from Anime t where t.id = #{id}")
    @Results({
            @Result(column = "id", property = "id", id = true),
            @Result(column = "local_name", property = "localName"),
            @Result(column = "origin_name", property = "originName"),
            @Result(column = "image_path", property = "imagePath"),
            @Result(column = "type", property = "type", typeHandler = BaseEnumTypeHandler.class),
            @Result(column = "broadcast_time", property = "broadcastTime"),
            @Result(column = "state", property = "state", typeHandler = BaseEnumTypeHandler.class),
            @Result(column = "introduction_path", property = "introductionPath"),
            @Result(column = "update_time", property = "updateTime"),
            @Result(many = @Many(select = "org.watp.anifree.anime.dao.AnimeMapper.getSynthesizeEpisodesInfoByAnimeId"), property = "episodes", column = "id", javaType = ArrayList.class)
    })
    @CacheEnable(type = CacheType.PRIVATE, desc = "SynthesizeAnimeInfo", dataType = DataType.DATA, privateId = "${anime.id}")
    AnimePO getSynthesizeAnimeInfoByPO(AnimePO anime);
    
    @Select("select * from Episode t where t.anime_id = #{anime_id} order by t.ordr")
    @Results({
            @Result(column = "anime_id", property = "animeId"),
            @Result(column = "image_path", property = "imagePath"),
            @Result(many = @Many(select = "org.watp.anifree.anime.dao.AnimeMapper.getSynthesizePlaysInfoByEpisodeId"), property = "plays", column = "id", javaType = ArrayList.class)
    })
    List<EpisodePO> getSynthesizeEpisodesInfoByAnimeId(@Param("anime_id") String anime_id);
    
    @Select("select * from Play t where t.episode_id = #{episode_id} order by t.ordr")
    @Results({
            @Result(column = "episode_id", property = "episodeId"),
            @Result(column = "type", property = "type", typeHandler = BaseEnumTypeHandler.class)
    })
    List<PlayPO> getSynthesizePlaysInfoByEpisodeId(@Param("episode_id") String episode_id);
    
    77yf77yf77yf
        1
    77yf77yf77yf  
    OP
       2023-01-03 19:54:21 +08:00
    create table Anime
    (
    id varchar(16),
    local_name varchar(500) not null,
    origin_name varchar(500) not null,
    image_path varchar(500) not null,
    type char(1) not null,
    broadcast_time date not null,
    author varchar(50),
    producer varchar(50),
    state char(1) not null,
    introduction_path varchar(1000),
    update_time datetime not null,
    constraint ANIME_ID_PRIMARY primary key (id)
    )
    create unique index ANIME_LOCALNAME_UNIQUE on Anime(local_name);
    create unique index ANIME_ORIGINNAME_UNIQUE on Anime(origin_name);

    create table Episode
    (
    anime_id varchar(16),
    id varchar(16),
    name varchar(200) not null,
    image_path varchar(500),
    ordr int not null,
    constraint EPISODE_ID_PRIMARY primary key (id),
    constraint EPISODE_ANIMEID_FOREIGN foreign key (anime_id) references Anime(id)
    )
    create unique index EPISODE_NAME_UNIQUE on Episode(name);
    create unique index EPISODE_ORDR_UNIQUE on Episode(anime_id,ordr);

    create table Play
    (
    id varchar(16),
    episode_id varchar(16),
    name varchar(200) not null,
    type char(1) not null,
    location varchar(500),
    ordr int not null,
    constraint PLAY_ID_PRIMARY primary key (id),
    constraint PLAY_EPISODEID_FOREIGN foreign key (episode_id) references Episode(id)
    )
    create unique index PLAY_ORDR_UNIQUE on Play(episode_id,ordr);

    表结构是这样的
    Red998
        2
    Red998  
       2023-01-03 20:53:34 +08:00
    看到就头疼、为什么要用注解 Results 呢 直接写 xml 写 sql 不是更简单 。简单直观
    77yf77yf77yf
        3
    77yf77yf77yf  
    OP
       2023-01-03 21:05:41 +08:00
    @redorblacck886 我用注解更加习惯一点吧
    这个获取到的 episode 对象中 id 为 null,但是后面的 play 对象是根据传过去的 episode id 查出来的,照理来说肯定有,但是就是不知道为什么没有放进对象里
    L0L
        4
    L0L  
       2023-01-03 21:37:19 +08:00
    ```java
    @Select("select * from Episode t where t.anime_id = #{anime_id} order by t.ordr")
    @Results({
    @Result(column = "anime_id", property = "animeId"),
    @Result(column = "image_path", property = "imagePath"),
    @Result(many = @Many(select = "org.watp.anifree.anime.dao.AnimeMapper.getSynthesizePlaysInfoByEpisodeId"), property = "plays", column = "id", javaType = ArrayList.class)
    })
    List<EpisodePO> getSynthesizeEpisodesInfoByAnimeId(@Param("anime_id") String anime_id);
    ```
    是不是第二条关联语句中的 `column = "anime_id"` 这样呢?
    实际是三个表的依次 JOIN 操作,猜测是你第二个语句中实际是没有 column 为 id 的这一列的原因
    77yf77yf77yf
        5
    77yf77yf77yf  
    OP
       2023-01-03 21:46:50 +08:00
    @L0L 加了个 id 的 Result 还真的有了,感谢大佬
    不是应该实体类和列的名称相同可以自动映射吗,为什么这里需要手写一下,而下面那个 Play 的 id 就不用手写就有,向大佬请教一下
    L0L
        6
    L0L  
       2023-01-03 21:54:07 +08:00
    @77yf77yf77yf 这块可以 debug 看一下,我记得自动映射在 xml 里可以使用双引号实现;注解的方式平常用的比较少,如果是对比 xml 中 resultMap 的标签形式的话,其实你第二个语句实际相当于只使用了你定义的几个 Result 注解的字段,而其他的框架并没有使用,也就没有映射了;
    L0L
        7
    L0L  
       2023-01-03 21:54:21 +08:00
    V 友多说一嘴,一般如果是应用工程中的代码的话,` select * ` 的这种写法尽量还是避免,用到什么写什么,对于后续扩展表的字段结构甚至是查询速度来说,都会有一些提升。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   963 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 20:49 · PVG 04:49 · LAX 12:49 · JFK 15:49
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.