V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
wwlearnbc
V2EX  ›  问与答

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

  •  
  •   wwlearnbc · 2021-12-07 16:53:28 +08:00 · 585 次点击
    这是一个创建于 1081 天前的主题,其中的信息可能已经有所发展或是发生改变。
    问题遇到的现象和发生背景

    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 中但是报错了。现在不知道怎么解决这个问题。

    我想要达到的结果

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

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2744 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 05:43 · PVG 13:43 · LAX 21:43 · JFK 00:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.