如何將一個物件中的List物件新增為子集,且父結構不存在重複資料
阿新 • • 發佈:2021-11-11
有這樣一個需求,把一個客戶名下的聯絡人構建成下拉樹結構。如 :
A公司
----聯絡人:張三
----聯絡人:李四
B公司
----聯絡人:王五
客戶資料結構
CREATE TABLE `os_customer` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主鍵',
`name` varchar(20) DEFAULT NULL COMMENT '客戶公司名',
`tenant_id` bigint(20) DEFAULT NULL COMMENT '租戶id',
`create_user` bigint(20) DEFAULT NULL COMMENT '建立人',
`create_time` datetime(3) DEFAULT NULL COMMENT '建立時間',
`update_user` bigint(20) DEFAULT NULL COMMENT '更新人',
`update_time` datetime(3) DEFAULT NULL COMMENT '更新時間',
`del_flag` tinyint(1) DEFAULT NULL COMMENT '刪除標識(0:否 1:是)',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COMMENT='客戶表';
已新增資料
客戶-客戶聯絡人表
CREATE TABLE `os_customer_user` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主鍵',
`customer_id` bigint(20) DEFAULT NULL COMMENT '客戶公司id',
`user_id` bigint(20) DEFAULT NULL COMMENT '客戶聯絡人id',
`del_flag` tinyint(1) DEFAULT NULL COMMENT '是否刪除(0:否 1:否)',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COMMENT='客戶聯絡人-客戶公司聯絡表';
已新增資料
客戶聯絡人表
CREATE TABLE `sys_user` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主鍵ID',
`user_name` varchar(100) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '賬號',
`password` varchar(100) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '密碼',
`salt` varchar(50) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '密碼鹽',
`name` varchar(200) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '姓名',
`avatar` varchar(200) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '頭像地址',
`sex` tinyint(1) DEFAULT NULL COMMENT '性別,1:男;2:女;',
`birthday` date DEFAULT NULL COMMENT '出生日期',
`mobile` varchar(20) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '手機號碼',
`email` varchar(100) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '郵箱',
`qq` varchar(50) COLLATE utf8mb4_bin DEFAULT NULL COMMENT 'QQ',
`create_user` bigint(20) DEFAULT NULL COMMENT '建立者',
`create_time` datetime(3) DEFAULT NULL COMMENT '建立時間',
`update_user` bigint(20) DEFAULT NULL COMMENT '修改者',
`update_time` datetime(3) DEFAULT NULL COMMENT '修改時間',
`type` tinyint(1) DEFAULT NULL COMMENT '使用者型別,0:系統管理員;1:管理員;2:普通使用者;3客戶',
`status` tinyint(1) DEFAULT NULL COMMENT '狀態,0:已啟用;1:已禁用;',
`tenant_id` bigint(20) DEFAULT NULL COMMENT '租戶ID',
`del_flag` tinyint(1) DEFAULT NULL COMMENT '刪除狀態,0:未刪除;1:已刪除;',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=36 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='系統使用者';
已新增資料
剛開始想著直接用mysql聯表查詢出來。
mysql寫法
<select id="getCustomerUserList" resultType="com.hnlrkj.order.vo.order.OrderCustomerVO>
select
oc.id customerId,
ocu.customer_id parentId,
oc.name customerName,
su.id customerUserId,
su.name customerUserName,
su.mobile customerUserPhone
from sys_user su
left join os_customer_user ocu
on ocu.user_id = su.id
left join os_customer oc
on oc.id = ocu.customer_id
where su.type = 3
ORDER BY oc.create_time
</select>
查詢結果
這裡有一個公司有兩個聯絡人,最開始是直接把這個查詢結果往一個最開始的OrderCustomerVO中放,再用java遍歷去重,但是並沒有成功
@Data
@Accessors(chain = true)
public class OrderCustomerVO {
/**
* 客戶id
*/
@JsonSerialize(using = IdSerialize.class)
private Long customerId;
/**
* 客戶名稱
*/
private String customerName;
/**
* 客戶聯絡人id
*/
@JsonSerialize(using = IdSerialize.class)
private Long customerUserId;
/**
* 客戶聯絡人姓名
*/
private String customerUserName;
/**
* 客戶聯絡人電話
*/
private String customerUserPhone;
}
後面使用了mysql的結果集一對多的對映,修改返回檢視OrderCustomerVO物件,把要新增的客戶聯絡人作為子列表List,拆分成一個物件(private List<OrderCustomerUserVO> children;)
/**
* 客戶及客戶聯絡人檢視
*
*/
@Data
@Accessors(chain = true)
public class OrderCustomerVO {
/**
* 客戶id
*/
@JsonSerialize(using = IdSerialize.class)
private Long customerId;
/**
* 客戶名稱
*/
private String customerName;
/**
* 客戶聯絡人子集
*/
private List<OrderCustomerUserVO> children;
}
/**
* 客戶聯絡人VO
*/
@Data
@Accessors(chain = true)
public class OrderCustomerUserVO {
/**
* 客戶聯絡人id
*/
@JsonSerialize(using = IdSerialize.class)
private Long customerUserId;
/**
* 客戶聯絡人姓名
*/
private String customerUserName;
/**
* 客戶聯絡人電話
*/
private String customerUserPhone;
}
mysql寫法,這裡需要注意,要把private List<OrderCustomerUserVO> children;作為對映的collection
<!-- 客戶及客戶聯絡人結果集-->
<resultMap id="OrderCustomerVOMap" type="com.hnlrkj.order.vo.order.OrderCustomerVO">
<id column="customerId" property="customerId"/>
<result column="customerName" property="customerName"/>
<collection property="children" ofType="com.hnlrkj.order.vo.order.OrderCustomerUserVO">
<result column="customerUserId" property="customerUserId"/>
<result column="customerUserName" property="customerUserName"/>
<result column="customerUserPhone" property="customerUserPhone"/>
</collection>
</resultMap>
<select id="getCustomerUserList" resultMap="OrderCustomerVOMap">
select
oc.id customerId,
ocu.customer_id parentId,
oc.name customerName,
su.id customerUserId,
su.name customerUserName,
su.mobile customerUserPhone
from sys_user su
left join os_customer_user ocu
on ocu.user_id = su.id
left join os_customer oc
on oc.id = ocu.customer_id
where su.type = 3
ORDER BY oc.create_time
</select>
這樣寫完後,就能得到樹結構了