myBatis实现三级嵌套复杂对象的赋值问题
平常我们工作中基本最多两级嵌套,但是有时候难免会遇到三级嵌套的业务场景,笔者最近就碰到了,使用一般的嵌套发现赋值为空,这可难倒了菜逼的我,后来在stackoverflow的帮助下终于搜到了解决办法,完美解决了问题,总结一下,方便有需要的同学,下面直接上栗子:
首先上实体类:三级嵌套如下(电站----->电桩---->电枪)
电站实体类(实体为JPA写法,不影响mybatis的使用)
packagecom.weima.cecapp.entities; importlombok.Data; importlombok.NoArgsConstructor; importorg.hibernate.annotations.BatchSize; importorg.hibernate.annotations.GenericGenerator; importjavax.persistence.*; importjava.util.Collections; importjava.util.List; importjava.util.Set; @NoArgsConstructor @Data @Entity @Table(name="station_info") publicclassStationInfo{ /** *Auto-generatedprimarykey. */ @Id @GeneratedValue(generator="uuid") @GenericGenerator(name="uuid",strategy="org.hibernate.id.UUIDGenerator") @Column(unique=true,nullable=false,updatable=false) privateStringid; @Column(name="station_id") privateStringstationId; @Column(name="operator_id") privateStringoperatorId; @Column(name="equipment_owner_id") privateStringequipmentOwnerId; @Column privateStringstationName; @Column privateStringcountryCode; @Column privateStringareaCode; @Column privateStringaddress; @Column privateStringstationTel; @Column privateStringserviceTel; @Column privateIntegerstationType; @Column privateIntegerstationStatus; @Column privateIntegerparkNums; @Column privateDoublestationLng; @Column privateDoublestationLat; @Column privateStringsiteGuide; @Column privateIntegerconstruction; @OneToMany(fetch=FetchType.LAZY,cascade={CascadeType.ALL},orphanRemoval=true,mappedBy="ownerStationInfo") privateListpictures; @Column privateStringmatchCars; @Column privateStringparkInfo; @Column privateStringbusineHours; @Column(name="busine_hours_in_milliseconds") privateLongbusineHoursInMilliseconds; @Column privateStringelectricityFee; @Column privateStringserviceFee; @Column privateStringparkFee; @Column privateStringpayment; @Column privateIntegersupportOrder; @Column privateStringremark; @OneToMany(fetch=FetchType.LAZY,cascade={CascadeType.ALL},orphanRemoval=true,mappedBy="ownerStationInfo") @BatchSize(size=20) privateList equipmentInfos; }
电站图片实体
packagecom.weima.cecapp.entities; importlombok.Data; importlombok.EqualsAndHashCode; importlombok.ToString; importorg.hibernate.annotations.GenericGenerator; importjavax.persistence.*; @Data @Entity @EqualsAndHashCode(of={"url"}) @ToString(exclude={"ownerStationInfo"}) @Table(name="station_picture") publicclassStationPicture{ /** *Auto-generatedprimarykey. */ @Id @GeneratedValue(generator="uuid") @GenericGenerator(name="uuid",strategy="org.hibernate.id.UUIDGenerator") @Column(unique=true,nullable=false,insertable=true,updatable=false) privateStringid; @ManyToOne(fetch=FetchType.EAGER,optional=false) @JoinColumn(nullable=false,updatable=false) privateStationInfoownerStationInfo; @Column privateStringurl; }
电桩实体类
packagecom.weima.cecapp.entities; importlombok.Data; importlombok.EqualsAndHashCode; importlombok.ToString; importorg.hibernate.annotations.BatchSize; importorg.hibernate.annotations.GenericGenerator; importjavax.persistence.*; importjava.text.ParseException; importjava.text.SimpleDateFormat; importjava.util.Collections; importjava.util.List; @Data @Entity @EqualsAndHashCode(of={"equipmentId"}) @ToString(exclude={"ownerStationInfo"}) @Table(name="equipment_info") publicclassEquipmentInfo{ /** *Auto-generatedprimarykey. */ @Id @GeneratedValue(generator="uuid") @GenericGenerator(name="uuid",strategy="org.hibernate.id.UUIDGenerator") @Column(unique=true,nullable=false,updatable=false) privateStringid; @Column(name="equipment_id") privateStringequipmentId; @ManyToOne(fetch=FetchType.EAGER,optional=false) @JoinColumn(nullable=false,updatable=false) privateStationInfoownerStationInfo; @Column(name="manufacturer_id") privateStringmanufacturerId; @Column privateStringmanufacturerName; @Column privateStringequipmentModel; @Column privateStringproductionDate; publicStringgetProductionDate(){ Stringformat=null; if(this.productionDate!=null){ SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-dd"); try{ format=sdf.format(sdf.parse(this.productionDate)); }catch(ParseExceptione){ e.printStackTrace(); } returnformat; } returnformat; } @Column privateStringequipmentType; @OneToMany(fetch=FetchType.LAZY,orphanRemoval=true,mappedBy="ownerEquipmentInfo",cascade={CascadeType.PERSIST}) @BatchSize(size=20) privateListconnectorInfos; @Column privateDoubleequipmentLng; @Column privateDoubleequipmentLat; @Column privateDoublepower; @Column privateStringequipmentName; @Column(name="equipment_no") //cpo'scustomequipmentIdmostlyforEvstation privateStringequipmentNo; }
电枪实体类
packagecom.weima.cecapp.entities; importlombok.Data; importlombok.EqualsAndHashCode; importlombok.ToString; importorg.hibernate.annotations.GenericGenerator; importjavax.persistence.*; @Data @Entity @EqualsAndHashCode(of={"connectorId"}) @ToString(exclude={"ownerEquipmentInfo"}) @Table(name="connector_info") publicclassConnectorInfo{ /** *Auto-generatedprimarykey. */ @Id @GeneratedValue(generator="uuid") @GenericGenerator(name="uuid",strategy="org.hibernate.id.UUIDGenerator") @Column(unique=true,nullable=false,updatable=false) privateStringid; @ManyToOne(fetch=FetchType.EAGER,optional=false) @JoinColumn(nullable=false,updatable=false) privateEquipmentInfoownerEquipmentInfo; @Column(name="connector_id") privateStringconnectorId; @Column privateStringconnectorName; @Column privateIntegerconnectorType; @Column privateIntegervoltageUpperLimits; @Column privateIntegervoltageLowerLimits; @Column privateIntegercurrent; @Column privateDoublepower; @Column privateStringparkNo; @Column privateIntegernationalStandard; @Column(name="connector_no") //cpo'scustomconnectorIdmostlyforEvstation privateStringconnectorNo; }
mapper文件的resultMap映射及Sql语句的书写,要特别注意映射关系
--要特别注意的是t_f_的前缀,这个关乎到映射不映射上的根本。 select a.idasid, a.station_idasstation_id, a.operator_idasoperator_id, a.equipment_owner_idasequipment_owner_id, a.station_nameasstation_name, a.country_codeascountry_code, a.area_codeasarea_code, a.addressasaddress, a.station_telasstation_tel, a.service_telasservice_tel, a.station_typeasstation_type, a.station_statusasstation_status, a.park_numsaspark_nums, a.station_lngasstation_lng, a.station_latasstation_lat, a.site_guideassite_guide, a.constructionasconstruction, a.match_carsasmatch_cars, a.park_infoaspark_info, a.busine_hoursasbusine_hours, a.busine_hours_in_millisecondsasbusine_hours_in_milliseconds, a.electricity_feeaselectricity_fee, a.service_feeasservice_fee, a.park_feeaspark_fee, a.paymentaspayment, a.support_orderassupport_order, a.remarkasremark , e.idast_id, e.equipment_idast_equipment_id, e.equipment_latast_equipment_lat, e.equipment_lngast_equipment_lng, e.equipment_modelast_equipment_model, e.equipment_nameast_equipment_name, e.equipment_typeast_equipment_type, e.manufacturer_idast_manufacturer_id, e.manufacturer_nameast_manufacturer_name, e.powerast_power, e.production_dateast_production_date, e.owner_station_info_idast_owner_station_info_id, e.equipment_noast_equipment_no , c.idast_f_id, c.powerast_f_power, c.currentast_f_current, c.park_noast_f_park_no, c.connector_idast_f_connector_id, c.connector_nameast_f_connector_name, c.connector_typeast_f_connector_type, c.connector_noast_f_connector_no, c.national_standardast_f_national_standard, c.voltage_lower_limitsast_f_voltage_lower_limits, c.voltage_upper_limitsast_f_voltage_upper_limits, c.owner_equipment_info_idast_f_owner_equipment_info_id fromstation_infoajoinequipment_infoeona.id=e.owner_station_info_id joinconnector_infocone.id=c.owner_equipment_info_id wherea.operator_id='MA59J8YL8'
希望为遇到同样需求的同学提供到帮助。
到此这篇关于myBatis实现三级嵌套复杂对象的赋值问题的文章就介绍到这了,更多相关myBatis三级嵌套复杂对象的赋值内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。