栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

myBatis实现三级嵌套复杂对象的赋值问题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

myBatis实现三级嵌套复杂对象的赋值问题

平常我们工作中基本最多两级嵌套,但是有时候难免会遇到三级嵌套的业务场景,笔者最近就碰到了,使用一般的嵌套发现赋值为空,这可难倒了菜逼的我,后来在stackoverflow的帮助下终于搜到了解决办法,完美解决了问题 ,总结一下,方便有需要的同学,下面直接上栗子:

首先上实体类:三级嵌套如下 (电站 -----> 电桩 ---->电枪)

电站实体类 (实体为JPA写法,不影响mybatis的使用)

package com.weima.cecapp.entities;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.util.Collections;
import java.util.List;
import java.util.Set;
@NoArgsConstructor
@Data
@Entity
@Table(name = "station_info")
public class StationInfo {
  
  @Id
  @GeneratedValue(generator = "uuid")
  @GenericGenerator(name = "uuid", strategy = "org.hibernate.id.UUIDGenerator")
  @Column(unique = true, nullable = false, updatable = false)
  private String id;

  @Column(name = "station_id")
  private String stationId;

  @Column(name = "operator_id")
  private String operatorId;

  @Column(name = "equipment_owner_id")
  private String equipmentOwnerId;

  @Column
  private String stationName;

  @Column
  private String countryCode;

  @Column
  private String areaCode;

  @Column
  private String address;

  @Column
  private String stationTel;

  @Column
  private String serviceTel;

  @Column
  private Integer stationType;

  @Column
  private Integer stationStatus;

  @Column
  private Integer parkNums;

  @Column
  private Double stationLng;

  @Column
  private Double stationLat;

  @Column
  private String siteGuide;

  @Column
  private Integer construction;

  @oneToMany(fetch = FetchType.LAZY, cascade = {CascadeType.ALL}, orphanRemoval = true, mappedBy = "ownerStationInfo")
  private List pictures;

  @Column
  private String matchCars;

  @Column
  private String parkInfo;

  @Column
  private String busineHours;

  @Column(name = "busine_hours_in_milliseconds")
  private Long busineHoursInMilliseconds;

  @Column
  private String electricityFee;

  @Column
  private String serviceFee;

  @Column
  private String parkFee;

  @Column
  private String payment;

  @Column
  private Integer supportOrder;

  @Column
  private String remark;

  @oneToMany(fetch = FetchType.LAZY, cascade = {CascadeType.ALL}, orphanRemoval = true, mappedBy = "ownerStationInfo")
  @BatchSize(size = 20)
  private List equipmentInfos;

}

电站图片实体

package com.weima.cecapp.entities;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;

@Data
@Entity
@EqualsAndHashCode(of = {"url"})
@ToString(exclude = {"ownerStationInfo"})
@Table(name = "station_picture")
public class StationPicture {
  
  @Id
  @GeneratedValue(generator = "uuid")
  @GenericGenerator(name = "uuid", strategy = "org.hibernate.id.UUIDGenerator")
  @Column(unique = true, nullable = false, insertable = true, updatable = false)
  private String id;

  @ManyToOne(fetch = FetchType.EAGER, optional = false)
  @JoinColumn(nullable = false, updatable = false)
  private StationInfo ownerStationInfo;

  @Column
  private String url;
}

电桩实体类

package com.weima.cecapp.entities;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.List;

@Data
@Entity
@EqualsAndHashCode(of = {"equipmentId"})
@ToString(exclude = {"ownerStationInfo"})
@Table(name = "equipment_info")
public class EquipmentInfo {
  
  @Id
  @GeneratedValue(generator = "uuid")
  @GenericGenerator(name = "uuid", strategy = "org.hibernate.id.UUIDGenerator")
  @Column(unique = true, nullable = false, updatable = false)
  private String id;

  @Column(name = "equipment_id")
  private String equipmentId;

  @ManyToOne(fetch = FetchType.EAGER, optional = false)
  @JoinColumn(nullable = false, updatable = false)
  private StationInfo ownerStationInfo;

  @Column(name = "manufacturer_id")
  private String manufacturerId;

  @Column
  private String manufacturerName;

  @Column
  private String equipmentModel;

  @Column
  private String productionDate;

  public String getProductionDate() {
    String format = null;
    if (this.productionDate != null) {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      try {
 format = sdf.format(sdf.parse(this.productionDate));
      } catch (ParseException e) {
 e.printStackTrace();
      }
      return format;
    }
    return format;
  }

  @Column
  private String equipmentType;

  @oneToMany(fetch = FetchType.LAZY, orphanRemoval = true, mappedBy = "ownerEquipmentInfo", cascade = {CascadeType.PERSIST})
  @BatchSize(size = 20)
  private List connectorInfos;

  @Column
  private Double equipmentLng;

  @Column
  private Double equipmentLat;

  @Column
  private Double power;

  @Column
  private String equipmentName;

  @Column(name = "equipment_no")
  //cpo's custom equipmentId mostly for Evstation
  private String equipmentNo;
}

电枪实体类

package com.weima.cecapp.entities;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;

@Data
@Entity
@EqualsAndHashCode(of = {"connectorId"})
@ToString(exclude = {"ownerEquipmentInfo"})
@Table(name = "connector_info")
public class ConnectorInfo {
  
  @Id
  @GeneratedValue(generator = "uuid")
  @GenericGenerator(name = "uuid", strategy = "org.hibernate.id.UUIDGenerator")
  @Column(unique = true, nullable = false, updatable = false)
  private String id;

  @ManyToOne(fetch = FetchType.EAGER, optional = false)
  @JoinColumn(nullable = false, updatable = false)
  private EquipmentInfo ownerEquipmentInfo;

  @Column(name = "connector_id")
  private String connectorId;

  @Column
  private String connectorName;

  @Column
  private Integer connectorType;

  @Column
  private Integer voltageUpperLimits;

  @Column
  private Integer voltageLowerLimits;

  @Column
  private Integer current;

  @Column
  private Double power;

  @Column
  private String parkNo;

  @Column
  private Integer nationalStandard;

  @Column(name = "connector_no")
  //cpo's custom connectorId mostly for Evstation
  private String connectorNo;
}

mapper 文件的resultMap映射及Sql语句的书写,要特别注意映射关系





  
    
    
    
    
    
    
    
    
    
    
    
    
    
      
    
      
      
      
      
      
      
      
      
      
      
      
    
  

  
  
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
  


-- 要特别注意的是 t_f_ 的前缀,这个关乎到映射不映射上的根本。

    select
a.id     as    id,
a.station_id as    station_id,
a.operator_idas    operator_id,
a.equipment_owner_id    as    equipment_owner_id,
a.station_nameas    station_name,
a.country_codeas    country_code,
a.area_code as    area_code,
a.address  as    address,
a.station_telas    station_tel,
a.service_telas    service_tel,
a.station_typeas    station_type,
a.station_status      as    station_status,
a.park_nums as    park_nums,
a.station_lngas    station_lng,
a.station_latas    station_lat,
a.site_guide as    site_guide,
a. construction      as    construction,
a.match_cars as    match_cars,
a.park_info as    park_info,
a.busine_hoursas    busine_hours,
a.busine_hours_in_milliseconds      as    busine_hours_in_milliseconds,
a. electricity_fee     as    electricity_fee,
a. service_feeas    service_fee,
a. park_fee as    park_fee,
a. payment  as    payment,
a. support_order      as    support_order,
a.remark   as    remark
,
e.id     as    t_id ,
e.equipment_idas    t_equipment_id   ,
e.equipment_lat      as    t_equipment_lat  ,
e.equipment_lng      as    t_equipment_lng  ,
e.equipment_model     as    t_equipment_model ,
e.equipment_name      as    t_equipment_name  ,
e.equipment_type      as    t_equipment_type  ,
e.manufacturer_id     as    t_manufacturer_id ,
e.manufacturer_name    as    t_manufacturer_name,
e.power   as    t_power      ,
e.production_date     as    t_production_date ,
e.owner_station_info_id  as    t_owner_station_info_id     ,
e.equipment_noas    t_equipment_no
,

c.id     as    t_f_id      ,
c.power   as    t_f_power    ,
c.current  as    t_f_current   ,
c.park_no  as    t_f_park_no   ,
c.connector_idas    t_f_connector_id ,
c.connector_name      as    t_f_connector_name,
c.connector_type      as    t_f_connector_type,
c.connector_noas    t_f_connector_no ,
c.national_standard    as    t_f_national_standard     ,
c.voltage_lower_limits   as    t_f_voltage_lower_limits    ,
c.voltage_upper_limits   as    t_f_voltage_upper_limits    ,
c.owner_equipment_info_id as    t_f_owner_equipment_info_id

from station_info a join equipment_info e on a.id = e.owner_station_info_id

  join connector_info c on e.id = c.owner_equipment_info_id

  where a.operator_id='MA59J8YL8'
  
  
  

希望为遇到同样需求的同学提供到帮助。

到此这篇关于myBatis实现三级嵌套复杂对象的赋值问题的文章就介绍到这了,更多相关myBatis三级嵌套复杂对象的赋值内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/130514.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号