Spring Data JPA 查询:MYSQL TO @QUERY
Posted
技术标签:
【中文标题】Spring Data JPA 查询:MYSQL TO @QUERY【英文标题】:Spring Data JPA Query: MYSQL TO @QUERY 【发布时间】:2021-08-27 22:53:12 【问题描述】:谁能帮我把下面的mysql查询翻译成@Query?
Mysql-查询:
SELECT *
FROM game JOIN game_teams ON game.id = game_teams.game_id
JOIN team_members ON game_teams.teams_id = team_members.team_id
WHERE start_date IS NOT NULL AND end_date IS NOT NULL AND members_username = 'user2'
此查询检索的列多于游戏表中的列。也许我必须只选择游戏表中的列?
我需要这样的东西:
@Query("???")
Collection<Game> findUserPlayedGames(User user);
【问题讨论】:
【参考方案1】:如果您将用户名作为变量传递,请按照以下方式进行操作,并且您正在查询中加入一些列,因此使用返回类型作为 List
@Query(value ="SELECT *
FROM game JOIN game_teams ON game.id =
game_teams.game_id
JOIN team_members ON game_teams.teams_id =
team_members.team_id
WHERE start_date IS NOT NULL AND end_date IS NOT NULL
ANDD members_username = :username", nativeQuery = true)
List<Object[]>
findUserPlayedGamess(@Param("username)String userName);
【讨论】:
【参考方案2】:@Query(value = "SELECT * "
+ " FROM game g JOIN game_teams gt ON g.id = gt.game_id "
+ " JOIN team_members tm ON gt.teams_id = tm.team_id "
+ " WHERE g.start_date IS NOT NULL AND g.end_date IS NOT NULL AND tm.members_username = 'user2'",
nativeQuery = true)
Collection<Game> findUserPlayedGames(User user);
【讨论】:
以上是关于Spring Data JPA 查询:MYSQL TO @QUERY的主要内容,如果未能解决你的问题,请参考以下文章
Spring Data JPA 查询:MYSQL TO @QUERY