mybatis 里面的 collection 嵌套两层后,如果里面那层有两条数据,最后返回的是两个 json 字符串,目标是想得到 1 个 json 字符串。把类型改为 List 也不行,有没有大佬教一教我呀。

2021-12-07 16:53:28 +08:00
 wwlearnbc
问题遇到的现象和发生背景

mybatis 里面的 collection 嵌套两层后,如果里面那层有两条数据,最后返回的是两个 json 字符串,目标是想得到 1 个 json 字符串。把类型改为 List 也不行,有没有大佬教一教我呀。

问题相关代码,

domain 层:

public class Alltest extends BaseEntity {

    private static final long serialVersionUID = 1L;

    private List<Treatmentinfo> treatmentinfoList;
    private List<Treatmentpciinfo> treatmentpciinfoList;
    private List<Outcomeinfo> outcomeinfoList;
    private List<Registerinfo> registerinfoList;
    private List<Firstaidinfo> firstaidinfoList;

其他的几个嵌套就是在另外的 class 里面再嵌套一个 List ,代码太长就不展示了

因为全部数据太长了,我删除了中间的一些字段,保留前后数据和 collection 。
        <resultMap id="AlltestResult" type="com.ruoyi.alltest.domain.Alltest">

                <collection property="registerinfoList" javaType="java.util.List" resultMap="RegisterinfoResult"/>
                <collection property="firstaidinfoList" javaType="java.util.List" resultMap="FirstaidinfoResult"/>
                <collection property="outcomeinfoList" javaType="java.util.List" resultMap="OutcomeinfoResult"/>
                <collection property="treatmentinfoList" javaType="java.util.List" resultMap="TreatmentinfoResult"/>
                <collection property="treatmentpciinfoList" javaType="java.util.List" resultMap="TreatmentpciinfoResult"/>
                
        </resultMap>

        <!--人口信息表-->
        <resultMap type="com.ruoyi.patientme.domain.Registerinfo" id="RegisterinfoResult">
                <result property="patientId"    column="patient_id"    />
                <result property="register_id"    column="REGISTER_ID"    />
                <result property="hospital_id"    column="HOSPITAL_ID"    />
                <result property="name"    column="NAME"    />
                <result property="gender"    column="GENDER"    />
                <result property="age"    column="AGE"    />
                <result property="birthday"    column="BIRTHDAY"    />
                <result property="nation"    column="NATION"    />
                <result property="fillin_person"    column="FILLIN_PERSON"    />
                <result property="submit_audit_person"    column="SUBMIT_AUDIT_PERSON"    />
                <result property="auditor"    column="AUDITOR"    />
                <result property="archiving_person"    column="ARCHIVING_PERSON"    />
        </resultMap>
        <!--急救信息表-->
        <resultMap type="com.ruoyi.emergencyaid.domain.Firstaidinfo" id="FirstaidinfoResult">
                <result property="patientId"    column="patient_id"    />
                <result property="inpatient_id"    column="INPATIENT_ID"    />
                <result property="outpatient_id"    column="OUTPATIENT_ID"    />
                <result property="attack_time"    column="ATTACK_TIME"    />
                <result property="is_null_attack_detail_time"    column="IS_NULL_ATTACK_DETAIL_TIME"    />
                <result property="attack_zone"    column="ATTACK_ZONE"    />
                <result property="province"    column="PROVINCE"    />
                <result property="city"    column="CITY"    />
                <result property="area"    column="AREA"    />
                <result property="attack_address"    column="ATTACK_ADDRESS"    />
                <result property="medical_insurance_type"    column="MEDICAL_INSURANCE_TYPE"    />
                <result property="medical_insurance_no"    column="MEDICAL_INSURANCE_NO"    />
                <result property="serious_illness_insurance"    column="SERIOUS_ILLNESS_INSURANCE"    />
                <result property="consciousness_type"    column="CONSCIOUSNESS_TYPE"    />
                <result property="respiration_rate"    column="RESPIRATION_RATE"    />
                <result property="pulse_rate"    column="PULSE_RATE"    />
                <result property="heart_rate"    column="HEART_RATE"    />
                <result property="throm_end_time"    column="THROM_END_TIME"    />
                <result property="throm_drug_type"    column="THROM_DRUG_TYPE"    />
                <result property="throm_drug_code"    column="THROM_DRUG_CODE"    />
                <result property="is_repatency"    column="IS_REPATENCY"    />
                <result property="start_radiography_time"    column="START_RADIOGRAPHY_TIME"    />
                <result property="is_repci"    column="IS_REPCI"    />
                <result property="operation_time"    column="OPERATION_TIME"    />
                <result property="hospital_position"    column="HOSPITAL_POSITION"    />
                <result property="patient_remark"    column="PATIENT_REMARK"    />
        </resultMap>
        <resultMap type="com.ruoyi.outcomeme.domain.Outcomeinfo" id="OutcomeinfoResult">
                <result property="patientId"    column="patient_id"    />
                <result property="cp_diagnosis_code"    column="CP_DIAGNOSIS_CODE"    />
                <result property="diagnosis_time"    column="DIAGNOSIS_TIME"    />
                <result property="is_heart_failure"    column="IS_HEART_FAILURE"    />
                <result property="pcsk9_name"    column="PCSK9_NAME"    />
                <result property="pcsk9_dose"    column="PCSK9_DOSE"    />

                <collection property="outdrugdetailsList" javaType="java.util.List" resultMap="OutdrugdetailsResult" />
        </resultMap>
                <resultMap type="Outdrugdetails" id="OutdrugdetailsResult">
                        <id property="myDrugId"    column="my_drug_id"    />
                        <result property="drug_id"    column="DRUG_ID"    />
                        <result property="patientId"    column="patient_id"    />
                        <result property="type"    column="TYPE"    />
                        <result property="name"    column="sub_name"    />
                        <result property="dose"    column="DOSE"    />
                        <result property="rete_count"    column="RETE_COUNT"    />
                        <result property="rete_time"    column="RETE_TIME"    />
                        <result property="other_drug_name"    column="OTHER_DRUG_NAME"    />
                </resultMap>
        <!--胸痛诊疗-->
        <resultMap type="Treatmentinfo" id="TreatmentinfoResult">
                <id property="patientId"    column="patient_id"    />
                <result property="has_ecg_image"    column="HAS_ECG_IMAGE"    />
                <result property="no_ecg_image_reason"    column="NO_ECG_IMAGE_REASON"    />
             
                <result property="await_doctor_name"    column="AWAIT_DOCTOR_NAME"    />
                <result property="await_treatment_type"    column="AWAIT_TREATMENT_TYPE"    />
                <result property="await_patient_remark"    column="AWAIT_PATIENT_REMARK"    />
        
                <collection property="ctntsList" javaType="java.util.List" resultMap="CtntsRrsult" />
                <collection property="ecgsList" javaType="java.util.List" resultMap="EcgsResult" />
        </resultMap>
                <resultMap id="CtntsRrsult" type="Ctnts">
                        <id property="ctntsId"    column="CTNTS_ID"    />
                        <result property="ctntsPatientId"    column="ctnts_patient_id"    />
                        <result property="type"    column="TYPE"    />
                        <result property="value"    column="VALUE"    />
                        <result property="unit"    column="UNIT"    />
                        <result property="status"    column="STATUS"    />
                        <result property="blood_time"    column="BLOOD_TIME"    />
                        <result property="report_time"    column="REPORT_TIME"    />
                </resultMap>
                <resultMap id="EcgsResult" type="Ecgs">
                        <id property="ecgId"    column="ECG_ID"    />
                        <result property="patientId"    column="patient_id"    />
                        <result property="ecg_time"    column="ECG_TIME"    />
                        <result property="ecg_diagnose_time"    column="ECG_DIAGNOSE_TIME"    />
                        <result property="ecgimagefiles"    column="ECGImageFiles"    />
                </resultMap>
        <!--导管室-->
        <resultMap type="com.ruoyi.cathlab.domain.Treatmentpciinfo" id="TreatmentpciinfoResult">
                <result property="patientId"    column="patient_id"    />
                <result property="activate_conduit_time"    column="ACTIVATE_CONDUIT_TIME"    />
                <result property="patient_arrived_conduit_time"    column="PATIENT_ARRIVED_CONDUIT_TIME"    />
                <result property="auxiliary_device"    column="AUXILIARY_DEVICE"    />
                <result property="complication"    column="COMPLICATION"    />

                <collection property="coronaryAngiographieList" javaType="java.util.List" resultMap="CoronaryAngiographieResult" />

        </resultMap>
                <resultMap id="CoronaryAngiographieResult" type="CoronaryAngiographie">
                        <id property="caId"    column="CA_ID"    />
                        <result property="caPatientId"    column="ca_patient_id"    />
                        <result property="position"    column="POSITION"    />
                        <result property="narrow_level"    column="NARROW_LEVEL"    />
                        <result property="preoperative_timi_leve"    column="PREOPERATIVE_TIMI_LEVE"    />
                        <result property="bracket_thrombus"    column="BRACKET_THROMBUS"    />
                        <result property="branching_sick"    column="BRANCHING_SICK"    />
                        <result property="cto"    column="CTO"    />
                        <result property="ca_sick"    column="CA_SICK"    />
                        <result property="criminal_sick"    column="CRIMINAL_SICK"    />
                        <result property="pci"    column="PCI"    />
                        <result property="intraoperative_handle"    column="INTRAOPERATIVE_HANDLE"    />
                        <result property="balloon_expansion_time"    column="BALLOON_EXPANSION_TIME"    />
                        <result property="timi"    column="TIMI"    />
                        <result property="bracket_num"    column="BRACKET_NUM"    />
                        <result property="bracket_type"    column="BRACKET_TYPE"    />
                </resultMap>

        <select id="selectAlltestByPatientId" parameterType="String" resultMap="AlltestResult">
                SELECT *                    
                FROM
                        registerinfo a,
                        firstaidinfo b,
                        outcomeinfo c
                                LEFT JOIN outdrugdetails ON outdrugdetails.patient_id = c.patient_id,
                        treatmentinfo d
                                LEFT JOIN ctnts ON ctnts.ctnts_patient_id = d.patient_id
                                LEFT JOIN ecgs ON ecgs.patient_id = d.patient_id,
                        treatmentpciinfo e
                                        LEFT JOIN coronary_angiographie ON coronary_angiographie.ca_patient_id = e.patient_id
                        WHERE
                            a.patient_id = #{paientId} and
                            a.patient_id = b.patient_id
                            AND a.patient_id = c.patient_id
                            AND a.patient_id = d.patient_id
                            AND a.patient_id = e.patient_id

        </select>


运行结果及报错内容

如果这个 Outdrugdetails 表里面对应的 patientId 有两条数据,目标最后的返回的数据是 1 条,但是返回了两条。

我的解答思路和尝试过的方法

我改了数据类型为 list 还是不行,sql 语句我目前只知道这么写,我试过将 select 语句嵌套在 collection 中但是报错了。现在不知道怎么解决这个问题。

我想要达到的结果

最后返回的结果可以正确的嵌套。

493 次点击
所在节点    问与答
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/820665

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX