java MyBatis 中的一对多关系

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33151873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:15:18  来源:igfitidea点击:

One to many relationship in MyBatis

javamysqlmybatis

提问by spacitron

I'm trying to use MyBatis to map a one to many relationship in my data model. This is based on the following classes:

我正在尝试使用 MyBatis 在我的数据模型中映射一对多关系。这是基于以下类:

class Team{
    String mId;
    String mName;
    List<Player> mPlayers;
}

class Player{
    String mId;
    String mName;
}

I would like to write a query that returns a list of matches, each populated with the tags that correspond to that match.

我想编写一个返回匹配列表的查询,每个匹配都填充了与该匹配对应的标签。

<select id="getTeams" resultType="Team" resultMap="TeamMap">
    SELECT id, name, players.id as player_id, players.name as player_name
    FROM teams
    JOIN players ON teams.id = players.team_id
</select>


<resultMap type="Team" id="TeamMap">
    <id property="mId" column="id"/>
    <result property="mName" column="name"/>

    <collection property="mTags" javaType="List" ofType="Player">
        <id property="player_id" column="mId"/>
        <result property="player_name" column="mName"/>
    </collection>

</resultMap>

But the problem I'm having with this is that each Team object is only populated with a single Player. How can I change this to ensure that each team contains all players that belong to it?

但是我遇到的问题是每个 Team 对象只填充了一个 Player。如何更改此设置以确保每个团队都包含属于它的所有玩家?

回答by Lucky

I've tried to follow this question and I've created a One-to-Many relationship in Mybatis using Annotations. Following is my code,

我试图遵循这个问题,并使用注释在 Mybatis 中创建了一对多关系。以下是我的代码,

UserMapper.java

用户映射器

@Select("SELECT teamId, name FROM TEAM")
    @Results(value = {
        @Result(property="teamId", column = "teamId"),
        @Result(property="name", column = "name"),
        @Result(property="players", column="teamId", javaType= List.class, many=@Many(select="selectPlayers"))
    })   
public List<Team> getAllTeams();

@Select("SELECT * FROM PLAYER WHERE teamId = #{teamId}")
    @Results(value={
        @Result(property="playerId", column ="playerId" ),
        @Result(property="name", column = "name")
    })
List<Player> selectPlayers(String teamId);

My Team.java:

我的Team.java

public class Team {

    private Long teamId;
    private String name;
    private List<Player> players;

    //...getters and setters

}

Player.java:

播放器.java:

public class Player {

    private Long playerId;
    private String name;
    private Team team;

    //...getter and setters

}

team.sql

团队.sql

CREATE TABLE `team` (
  `teamId` bigint(10) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`teamId`)
)

player.sql

播放器.sql

CREATE TABLE `player` (
  `playerId` bigint(10) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  `teamId` bigint(10) DEFAULT NULL,
  PRIMARY KEY (`playerId`),
  KEY `FK_TEAM_ID` (`teamId`),
  CONSTRAINT `FK_TEAM_ID` FOREIGN KEY (`teamId`) REFERENCES `team` (`teamId`)
)

UserServiceImpl.java

UserServiceImpl.java

@Autowired
private UserMapper userMapper;

...
/* Get the list of teams with players data */
List<Team> teams = userMapper.getAllTeams();
...

Hope this serves useful for future readers.

希望这对未来的读者有用。

回答by spacitron

Found the solution. All I had to do was change the player_id mapping in the collection from "id" to "result".

找到了解决办法。我所要做的就是将集合中的 player_id 映射从“id”更改为“result”。