MyBatis映射集合的正确方法

时间:2020-07-22 14:09:52

标签: mybatis mybatis-mapper

我有POJO团队,其中包含钓鱼者列表:

@Entity
public class Team {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private int orderNumber;

private String name;

private int rank;

@ManyToOne
private Competition competition;

@OneToMany
private List<Angler> anglers;

//Constructors, getters, setters
}

POJO Angler在团队中具有指针:

@Entity
public class Angler {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String name; 

private Integer trackNumber;

private Integer sector;

private Double points;

private String note;

private Integer rankSector;

private Integer totalRank;

@ManyToOne
private Team team;

//construcotrs, getters, setters

AnglerMapper.xml

<resultMap type="rs.necara.anglercompetitionbackend.model.Angler" id="AnglerResultMap">
        <id property="id" column="id" />
        <result property="name" column="name" />
        <result property="trackNumber" column="trackNumber" />
        <result property="sector" column="sector" />
        <result property="points" column="points" />
        <result property="rankSector" column="rankSector" />
        <result property="totalRank" column="totalRank" />
        <result property="note" column="note" />
        <association property="team" resultMap="TeamResultMap"/> 
</resultMap>

<resultMap type="rs.necara.anglercompetitionbackend.model.Team" id="TeamResultMap">
        <id property="id" column="id" />
        <result property="name" column="teamname" />
        <result property="rank" column="rank" />
        <collection property="anglers" column="{id=teamId}" javaType="ArrayList" ofType="rs.necara.anglercompetitionbackend.model.Angler" 
        select="rs.necara.anglercompetitionbackend.persistence.mapper.AnglerMapper.getAll"/>
</resultMap> 

<select id="getAll" resultMap="AnglerResultMap" >
    select * from angler
</select>

TeamMapper.xml

<resultMap type="rs.necara.anglercompetitionbackend.model.Team" id="TeamResultMap">
        <id property="id" column="id" />
        <result property="title" column="title" />
        <result property="orderNumber" column="orderNumber" />
        <result property="name" column="name" />
        <result property="rank" column="rank" />
        <association property="competition" resultMap="CompetitionResultMap"/>
        <collection property="anglers" javaType="ArrayList" ofType="rs.necara.anglercompetitionbackend.model.Angler">
            <id property="id" column="anglerid" />
            <result property="name" column="name" />
            <result property="trackNumber" column="trackNumber" />
            <result property="sector" column="sector" />
            <result property="points" column="points" />
            <result property="rankSector" column="rankSector" />
            <result property="totalRank" column="totalRank" />
            <result property="note" column="note" />
        </collection>
</resultMap>

<resultMap type="rs.necara.anglercompetitionbackend.model.Competition" id="CompetitionResultMap">
        <id property="id" column="competitionid" />
        <result property="title" column="title" />
        <result property="finished" column="finished" />
        <result property="competitionDate" column="competitionDate" />
        <result property="type" column="type" />
</resultMap> 

当我想要检索所有钓鱼者时,我得到无法编写JSON:无限递归(StackOverflowError)。结果是这样的:

[
    {
        "id": 7,
        "name": "t1",
        "trackNumber": null,
        "sector": null,
        "points": null,
        "note": null,
        "rankSector": null,
        "totalRank": null,
        "team": {
            "id": 7,
            "orderNumber": 0,
            "name": null,
            "rank": 0,
            "competition": null,
            "anglers": [
                {
                    "id": 7,
                    "name": "t1",
                    "trackNumber": null,
                    "sector": null,
                    "points": null,
                    "note": null,
                    "rankSector": null,
                    "totalRank": null,
                    "team": {
                        "id": 7,
                        "orderNumber": 0,
                        "name": null,
                        "rank": 0,
                        "competition": null,
                        "anglers": [
                            {...

因此,钓鱼者指向团队,而不是团队指向钓鱼者,依此类推,我很清楚,但是可以避免这种映射方式吗?什么是解决方案,最佳做法?

0 个答案:

没有答案
相关问题