欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-24 21:58:14
平常我们工作中基本最多两级嵌套,但是有时候难免会遇到三级嵌套的业务场景,笔者最近就碰到了,使用一般的嵌套发现赋值为空,这可难倒了菜逼的我,后来在*的帮助下终于搜到了解决办法,完...

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

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

电站实体类 (实体为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 {
  /**
   * auto-generated primary key.
   */
  @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<stationpicture> 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<equipmentinfo> 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 {
  /**
   * auto-generated primary key.
   */
  @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 {
  /**
   * auto-generated primary key.
   */
  @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<connectorinfo> 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 {
  /**
   * auto-generated primary key.
   */
  @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语句的书写,要特别注意映射关系

<?xml version="1.0" encoding="utf-8"?>
<!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hubject.cecapp.mapper.stationinfoanyuemapper">

  <resultmap id="templatelistresp" type="com.hubject.cecapp.entities.equipmentinfo">
    <!-- 充电桩信息-->
    <id column  ="id          " property="id"/>
    <result column="equipment_id     " property="equipmentid"/>
    <result column="equipment_lat     " property="manufacturername"/>
    <result column="equipment_lng     " property="equipmentmodel"/>
    <result column="equipment_model    " property="productiondate"/>
    <result column="equipment_name    " property="manufacturerid"/>
    <result column="equipment_type    " property="equipmenttype"/>
    <result column="manufacturer_id    " property="equipmentlng"/>
    <result column="manufacturer_name   " property="equipmentlat"/>
    <result column="power         " property="power"/>
    <result column="production_date    " property="equipmentname"/>
    <result column="owner_station_info_id " property="equipmentno"/>
      <!-- 充电枪 信息列表 -->
    <collection property="connectorinfos" columnprefix="f_" oftype="com.hubject.cecapp.entities.connectorinfo">
      <id column="id" property="id"/>
      <result column="connector_id" property="connectorid"/>
      <result column="connector_name" property="connectorname"/>
      <result column="connector_type" property="connectortype"/>
      <result column="voltage_upper_limits" property="voltageupperlimits"/>
      <result column="voltage_lower_limits" property="voltagelowerlimits"/>
      <result column="current" property="current"/>
      <result column="power" property="power"/>
      <result column="park_no" property="parkno"/>
      <result column="national_standard" property="nationalstandard"/>
      <result column="connector_no" property="connectorno"/>
    </collection>
  </resultmap>

  <!-- 通用查询映射结果 -->
  <resultmap id="base3resultmap" type="com.hubject.cecapp.entities.stationinfo">
    <result column="id" property="id"/>
    <result column="address" property="address"/>
    <result column="area_code" property="areacode"/>
    <result column="busine_hours" property="businehours"/>
    <result column="construction" property="construction"/>
    <result column="country_code" property="countrycode"/>
    <result column="electricity_fee" property="electricityfee"/>
    <result column="equipment_owner_id" property="equipmentownerid"/>
    <result column="station_id" property="stationid"/>
    <result column="operator_id" property="operatorid"/>
    <result column="station_name" property="stationname"/>
    <result column="station_tel" property="stationtel"/>
    <result column="service_tel" property="servicetel"/>
    <result column="station_type" property="stationtype"/>
    <result column="station_status" property="stationstatus"/>
    <result column="park_nums" property="parknums"/>
    <result column="station_lng" property="stationlng"/>
    <result column="station_lat" property="stationlat"/>
    <result column="site_guide" property="siteguide"/>
    <result column="match_cars" property="matchcars"/>
    <result column="park_info" property="parkinfo"/>
    <result column="busine_hoursin_milliseconds" property="businehoursinmilliseconds"/>
    <result column="service_fee" property="servicefee"/>
    <result column="park_fee" property="parkfee"/>
    <result column="payment" property="payment"/>
    <result column="support_order" property="supportorder"/>
    <result column="remark" property="remark"/>
    <result column="templatelistresp" property="remark"/>
    <collection property="equipmentinfos" columnprefix="t_" resultmap="templatelistresp"/>
  </resultmap>


-- 要特别注意的是 t_f_ 的前缀,这个关乎到映射不映射上的根本。
<select id="queryareaforanyocharging" resultmap="base3resultmap">
    select
       a.id                   as    id,
       a.station_id               as    station_id,
       a.operator_id              as    operator_id,
       a.equipment_owner_id           as    equipment_owner_id,
       a.station_name              as    station_name,
       a.country_code              as    country_code,
       a.area_code               as    area_code,
       a.address                as    address,
       a.station_tel              as    station_tel,
       a.service_tel              as    service_tel,
       a.station_type              as    station_type,
       a.station_status             as    station_status,
       a.park_nums               as    park_nums,
       a.station_lng              as    station_lng,
       a.station_lat              as    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_hours              as    busine_hours,
       a.busine_hours_in_milliseconds      as    busine_hours_in_milliseconds,
       a. electricity_fee            as    electricity_fee,
       a. service_fee              as    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_id              as    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_no              as    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_id              as    t_f_connector_id        ,
       c.connector_name             as    t_f_connector_name       ,
       c.connector_type             as    t_f_connector_type       ,
       c.connector_no              as    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'
  </select>
  
  </mapper>

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

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