MyBatis关联对订单的依赖

时间:2019-04-29 09:47:02

标签: mybatis spring-mybatis

我正在尝试使用MyBatis关联来获取数据。获取的数据包括人员列表,这些人员中的每一个都有与之关联的公司列表。 (请参阅POJO)当前,DB中只有两个人,即P1和P0。 P1与公司C1和C2相关联,而P0仅与C1相关联。 (请参见sql结果)

我的结果图包含一个人图,它本身包含一个公司图的关联。 (请参见结果图)

当我在sql中获取数据时,将按以下顺序显示三行:(请参见sql结果)

  1. 带有C1的P1
  2. 带有C1的P0
  3. P1和C2

当我使用“按人,公司订购”时,我得到的是正确的对象,即具有两个值的人员列表,一个为P1,另一个为P0,其中P1具有C1和C2列表,而P0具有列表仅C1。 (请参阅JSON#1)

但是,当我不使用“ order by”时,我得到的对象列表不正确,其中包含两个对象P1(具有C1和C2的列表)。 P0没有对象。 (请参阅JSON#2)

我尝试将ID(作为唯一标识符)用于公司和人员结果地图。我也尝试过嵌套映射。

Java端:

POJO:


public class Person {
    private String personId;
    private String firstName;
    private String lastName;
    private List<CompanyWiseDetails> companyWiseDetails;

    // setters and getters
}

public class CompanyWiseDetails {
    private String companyId;
    private String companyName;
    private String payrollCycle;
    private Double januaryCatch;


    // setters and getters
}

DAO:

final Map<Object, Object> paramMap = new HashMap<Object, Object>();
paramMap.put("universeEndDate", this.getUniverseEndDate());
paramMap.put("projectionDate", projectionDate);
SqlSession.selectList("fetchData")

结果图:

<resultMap id="person" type="Person">
    <id property="personId" column="person_id" />
    <result property="firstName" column="first_name" />
    <result property="lastName" column="last_name" />
    <!-- few other fields which can be null -->
    <collection
            property="companyWiseDetailsList"
            resultMap="companyEntityWiseDetails" />
</resultMap>

<resultMap id="companyEntityWiseDetails"
            type="CompanyWiseDetails">
    <id property="companyId" column="company_id" />
    <result property="companyName" column="company_name" />
    <result property="payrollCycle" column="payroll_cycle" />
    <!-- few other fields which can be null -->
</resultMap>

SQL:

<select id="fetchData" resultMap="person" >
    <include refid="toCreateTemporary_table"/>
    select
        table_one.person_id as person_id,
        company_table.hr_company_code as company_id,
        tCompany.PayrollCycle as payroll_cycle
    from table_one table_one
        join company_table_strategy company_table
            on company_table.person_id =  table_one.person_id
            and company_table.as_on_date = #{projectionDate}
            and company_table.tt_end= #{universeEndDate}
        join tCompany
            on company_table.hr_company_code = tCompany.HRCompanyCode
        left join
            #temporary_table
            on #temporary_table.company_id = company_table.hr_company_code
            and #temporary_table.person_id = table_one.person_id
    where
        table_one.as_on_date = #{projectionDate}
        and table_one.tt_end= #{universeEndDate}
    order by
        person_id, entity_id
    drop table #temporary_table
     </select>

SQL输出


person_id    company_id    payroll_cycle    company_name    cash_compensation    random_percentage    income             comp           field1             bonus             field2
P1           C1            bi_weekly        C1_name         5000.00000000        50.00000000          200112.10000000    60.10000000    NULL               NULL              NULL
P0           C1            bi_weekly        C1_name         5000.00000000        50.00000000          100112.10000000    30.10000000    NULL               NULL              NULL
P1           C2            bi_weekly        C2_name         5000.00000000        50.00000000          200112.10000000    30.10000000    362906.82000000    25000.00000000    1000.00000000

使用order by时响应JSON:

[
    {
        "personId": "P0",
        "firstName": null,
        "lastName": null,
        "companyEntityWiseDetailsList": [
            {
                "companyId": "C1",
                "companyName": null,
                "payrollCycle": "bi_weekly"
            }
        ]
    },
    {
        "personId": "P1",
        "firstName": null,
        "lastName": null,
        "companyEntityWiseDetailsList": [
            {
                "companyId": "C1",
                "companyName": null,
                "payrollCycle": "bi_weekly",
            },
            {
                "companyId": "C2",
                "companyName": null,
                "payrollCycle": "bi_weekly",
            }
        ]
    }
]

未使用order by时响应JSON:

[
    {
        "personId": "P1",
        "firstName": null,
        "lastName": null,
        "companyEntityWiseDetailsList": [
            {
                "companyId": "C1",
                "companyName": null,
                "payrollCycle": "bi_weekly",
            },
            {
                "companyId": "C2",
                "companyName": null,
                "payrollCycle": "bi_weekly",
            }
        ]
    },
    {
        "personId": "P1",
        "firstName": null,
        "lastName": null,
        "companyEntityWiseDetailsList": [
            {
                "companyId": "C1",
                "companyName": null,
                "payrollCycle": "bi_weekly",
            },
            {
                "companyId": "C2",
                "companyName": null,
                "payrollCycle": "bi_weekly",
            }
        ]
    }
]

“排序依据”是否应该对结果有影响

0 个答案:

没有答案