1.
(附加内容:什么时候复写equals和hashCode,为什么有的人不喜欢用lombock)
在开发中,经常会遇到 修改一张主表的数据后,然后再去修改字表的内容,一般是调用两个sql
下面的例子完成 单条sql 完成 主表和字表的批量修改(如果子表涉及到新增,就需要insert语句,这里暂不讨论)
1. 批量修改的语法
update ( A inner |left | right join on B on A.id = B表.A_id)
set A.字段1 = "新值" ,
A.字段2 = "新值" ,
B.字段1 = "新值" ,
B.字段2 = "新值"
where 过滤条件;
说明: 是把红色标出来的结果集当成一张表处理的
单表修改语句语法:
UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值
举例说明:
两张表一对多 :
commodity_order , commodity_order_detail
表里数据如下:
select * from commodity_order;
+--+----+---------+---------+
|id|name|custom_id|is_delete|
+--+----+---------+---------+
|1 |置办年货|1 |0 |
|2 |买衣服 |1 |0 |
+--+----+---------+---------+
select * from commodity_order_detail;
+--+--------+--------------+------------+---------------+---------+
|id|order_id|commodity_name|commodity_id|commodity_count|is_delete|
+--+--------+--------------+------------+---------------+---------+
|1 |1 |鞭炮 |1 |1 |0 |
|2 |1 |春联 |2 |1 |0 |
|3 |1 |牛丸 |3 |1 |0 |
|4 |2 |冬装 |4 |1 |0 |
|5 |2 |夏装 |5 |1 |0 |
+--+--------+--------------+------------+---------------+---------+
以下为练习:
select * from commodity_order;
select * from commodity_order_detail;
#
select 1 as A,2 as B;
# +-+-+
# |A|B|
# +-+-+
# |1|2|
# +-+-+
# 列转行(一行成多行)SQL:
select 1
union select 2;
# 先建表
# create table sql_test.commodity_order
# (
# id int auto_increment
# primary key,
# name varchar(20) null,
# custom_id varchar(20) null,
# is_delete int null
# )
# comment '订单主表';
# create table sql_test.commodity_order_detail
# (
# id int auto_increment
# primary key,
# order_id int null,
# commodity_name varchar(20) null,
# commodity_id int null,
# commodity_count int null,
# is_delete int null
# )
# comment '订单明细表';
select * from commodity_order;
# +--+----+---------+
# |id|name|custom_id|
# +--+----+---------+
# |1 |置办年货|1 |
# +--+----+---------+
select * from commodity_order_detail;
# +--+--------+--------------+------------+---------------+
# |id|order_id|commodity_name|commodity_id|commodity_count|
# +--+--------+--------------+------------+---------------+
# |1 |1 |鞭炮 |1 |1 |
# |2 |1 |春联 |2 |2 |
# |3 |1 |牛丸 |3 |3 |
# +--+--------+--------------+------------+---------------+
# 表名不可以是关键字 order
select o.* ,d.*
from commodity_order o
inner join commodity_order_detail d on o.id = d.order_id and d.is_delete = 0
where o.id = 1 and o.is_delete = 0;
# +--+----+---------+---------+--+--------+--------------+------------+---------------+---------+
# |id|name|custom_id|is_delete|id|order_id|commodity_name|commodity_id|commodity_count|is_delete|
# +--+----+---------+---------+--+--------+--------------+------------+---------------+---------+
# |1 |置办年货|1 |0 |1 |1 |鞭炮 |1 |1 |0 |
# |1 |置办年货|1 |0 |2 |1 |春联 |2 |2 |0 |
# |1 |置办年货|1 |0 |3 |1 |牛丸 |3 |3 |0 |
# +--+----+---------+---------+--+--------+--------------+------------+---------------+---------+
#(修改的批量操作) 需求1:用户做编辑操作,删掉了鞭炮和牛丸 (前段会传 订单明细id 1和 2)
# 先获取要处理的数据集(交集),此处是对订单明细 1和 2 的数据做处理
# update commodity_order_detail set is_delete = 1 where id in (1,2) 这个sql可以解决,但为了引出批量操作的sql 这里引出另一种思路
# 下面的sql写的有点麻烦了,优化后的sql如下
select *
from (
select 1 As detail_id
union
select 2 as detail_id
) tmp_detail
inner join commodity_order_detail detail on tmp_detail.detail_id = detail.id and detail.is_delete = 0 ;
# +---------+--+--------+--------------+------------+---------------+---------+
# |detail_id|id|order_id|commodity_name|commodity_id|commodity_count|is_delete|
# +---------+--+--------+--------------+------------+---------------+---------+
# |1 |1 |1 |鞭炮 |1 |1 |0 |
# |2 |2 |1 |春联 |2 |2 |0 |
# +---------+--+--------+--------------+------------+---------------+---------+
# 优化后的sql
select * from commodity_order_detail detail detail.id in (1,2) and detail.is_delete = 0 ;
# 获取到要操作的数据集后 ,将结果看成一张表
# update 虚拟表 set 字段 = 值 where 过滤条件
update (
# 上面获取结果集只要from 后面的
(
select 1 As detail_id
union
select 2 as detail_id
) tmp_detail
inner join commodity_order_detail detail on tmp_detail.detail_id = detail.id and detail.is_delete = 0
) set detail.is_delete = 1 where detail.order_id = 1;
优化后的sql如下:
update (
# 上面获取结果集只要from 后面的
commodity_order_detail detail detail.id in (1,2) and detail.is_delete = 0
) set detail.is_delete = 1 where detail.order_id = 1;
# 查看结果 发现数据修改成功
select * from commodity_order_detail;
# +--+--------+--------------+------------+---------------+---------+
# |id|order_id|commodity_name|commodity_id|commodity_count|is_delete|
# +--+--------+--------------+------------+---------------+---------+
# |1 |1 |鞭炮 |1 |1 |1 |
# |2 |1 |春联 |2 |2 |1 |
# |3 |1 |牛丸 |3 |3 |0 |
# +--+--------+--------------+------------+---------------+---------+
# 修改主表的name和逻辑删除 一个sql处理
# 先获取要操作的数据集
select * from
(
(
select 1 As detail_id
union
select 2 as detail_id
) tmp_detail
inner join commodity_order_detail detail on tmp_detail.detail_id = detail.id and detail.is_delete = 0
inner join commodity_order co on detail.is_delete = co.is_delete and co.id = detail.order_id
) ;
# +---------+--+--------+--------------+------------+---------------+---------+--+----+---------+---------+
# |detail_id|id|order_id|commodity_name|commodity_id|commodity_count|is_delete|id|name|custom_id|is_delete|
# +---------+--+--------+--------------+------------+---------------+---------+--+----+---------+---------+
# |1 |1 |1 |鞭炮 |1 |1 |0 |1 |置办年货|1 |0 |
# |2 |2 |1 |春联 |2 |2 |0 |1 |置办年货|1 |0 |
# +---------+--+--------+--------------+------------+---------------+---------+--+----+---------+---------+
优化后的sql:
select * from commodity_order corder inner join commodity_order_detail detail on corder .id =detail.order_id and detail.is_delete = 0 and corder.is_delete = 0
# 执行sql进行多表的修改
①
update (
# 上面获取结果集只要from 后面的
(
select 1 As detail_id
union
select 2 as detail_id
) tmp_detail
inner join commodity_order_detail detail on tmp_detail.detail_id = detail.id and detail.is_delete = 0
inner join commodity_order co on detail.is_delete = co.is_delete and co.id = detail.order_id
)
# 修改主表数据
set co.name = '第二次置办年货',
# 修改字表数据
detail.is_delete = 1
where detail.order_id = 1;
② 优化后sql
update (
# 上面获取结果集只要from 后面的
commodity_order corder inner join commodity_order_detail detail on corder .id =detail.order_id and detail.is_delete = 0 and corder.is_delete = 0
)
# 修改主表数据
set co.name = '第二次置办年货',
# 修改明细字段数据
detail.is_delete = 1
where detail.order_id = 1;
# 查看修改后的数据 ,操作成功
select o.* ,d.*
from commodity_order o
inner join commodity_order_detail d on o.id = d.order_id and d.is_delete = 0
where o.id = 1 and o.is_delete = 0;
# +--+-------+---------+---------+--+--------+--------------+------------+---------------+---------+
# |id|name |custom_id|is_delete|id|order_id|commodity_name|commodity_id|commodity_count|is_delete|
# +--+-------+---------+---------+--+--------+--------------+------------+---------------+---------+
# |1 |第二次置办年货|1 |0 |3 |1 |牛丸 |3 |3 |0 |
# +--+-------+---------+---------+--+--------+--------------+------------+---------------+---------+
# 需求二:将主订单名字改为 第二次置办年货 ,
# 将订单明细名字鞭炮和牛丸的数量分别改成50,100,名字分别改成 第二次鞭炮,第二次牛丸
# 订单明细中 春联数据不动
# (在将原来数据还原的基础上进行的)
# 前段传 订单明细表id ,修改后的名称和数量 ,转成sql如下
select '1' detail_id ,'第二次鞭炮' as commodity_name ,'50' as commodity_count
union select '3' detail_id ,'第二次牛丸' as commodity_name ,'100' as commodity_count;
# +---------+--------------+---------------+
# |detail_id|commodity_name|commodity_count|
# +---------+--------------+---------------+
# |1 |第二次鞭炮 |50 |
# |3 |第二次牛丸 |100 |
# +---------+--------------+---------------+
# 上述表和主订单,订单明细关联后获取的数据
select *
from (
(select '1' detail_id, '第二次鞭炮' as tem_commodity_name, '50' as commodity_count
union
select '3' detail_id, '第二次牛丸' as tmp_commodity_name, '100' as commodity_count) as tmp
inner join commodity_order_detail d on d.id = tmp.detail_id and d.is_delete = 0
inner join commodity_order o on o.id = d.order_id and o.is_delete = d.is_delete
# 这是后台传进来的 主订单id ,不能写死 ,这儿加 o.id = 1 是为了获取更少的数据集
and o.id = 1
);
# +---------+------------------+---------------+--+--------+--------------+------------+---------------+---------+--+----+---------+---------+
# |detail_id|tem_commodity_name|commodity_count|id|order_id|commodity_name|commodity_id|commodity_count|is_delete|id|name|custom_id|is_delete|
# +---------+------------------+---------------+--+--------+--------------+------------+---------------+---------+--+----+---------+---------+
# |1 |第二次鞭炮 |50 |1 |1 |鞭炮 |1 |1 |0 |1 |置办年货|1 |0 |
# |3 |第二次牛丸 |100 |3 |1 |牛丸 |3 |3 |0 |1 |置办年货|1 |0 |
# +---------+------------------+---------------+--+--------+--------------+------------+---------------+---------+--+----+---------+---------+
# 加个套形成update语句,执行
update (
# 操作的数据集start
(select '1' detail_id, '第二次鞭炮' as tmp_commodity_name, '50' as tmp_commodity_count
union
select '3' detail_id, '第二次牛丸' as tmp_commodity_name, '100' as tmp_commodity_count) as tmp
inner join commodity_order_detail d on d.id = tmp.detail_id and d.is_delete = 0
inner join commodity_order o on o.id = d.order_id and o.is_delete = d.is_delete
# 这是后台传进来的 主订单id ,不能写死 ,这儿加 o.id = 1 是为了获取更少的数据集
and o.id = 1
# 操作的数据集end
)
# 将 前段传过来的 tem 里的 字段值 赋值给 物理表
set o.name = '第二次置办年货',
d.commodity_name = tmp.tmp_commodity_name,
d.commodity_count = tmp.tmp_commodity_count
# o.id = 1 加到这里也行
where o.id = 1 ;
# 修改后查询 主表和字表数据
# +--+-------+---------+---------+--+--------+--------------+------------+---------------+---------+
# |id|name |custom_id|is_delete|id|order_id|commodity_name|commodity_id|commodity_count|is_delete|
# +--+-------+---------+---------+--+--------+--------------+------------+---------------+---------+
# |1 |第二次置办年货|1 |0 |1 |1 |第二次鞭炮 |1 |50 |0 |
# |1 |第二次置办年货|1 |0 |2 |1 |春联 |2 |2 |0 |
# |1 |第二次置办年货|1 |0 |3 |1 |第二次牛丸 |3 |100 |0 |
# +--+-------+---------+---------+--+--------+--------------+------------+---------------+---------+
# 需求三:将主订单名字改为 第二次置办年货 , (还是一个sql完成)
# 将订单明细名字鞭炮和牛丸的数量分别改成50,100,名字分别改成 第二次鞭炮,第二次牛丸
# 订单明细中 春联数据逻辑删除
# (在将原来数据还原的基础上进行的)
# 需求分析:获取操作的数据集(取两张表的交集),对交集处理,
# -------> 需求转换:因为 前段传输给的 名字鞭炮和牛丸 这是个对象,是集合List A 的元素,后台我拿出主订单下id 为 1 的所有子订单 B,取出B的差集removeAll or filter 筛选出要
# 要删除的元素,形成一个新的要删除订单明细的集合C,A和C 这两个集合合并,就是要逻辑处理的 记录,这样删除的时候也不会,将要删除记录的其他字段设置为 ‘’
# java代码实现
# 需求四:将主订单名字改为 第二次置办年货 ,(两条sql = 批量新增的sql+批量修改的sql)
# 将订单明细名字 新增一条数据 需要用insert语句
# 将订单明细名字鞭炮和牛丸的数量分别改成50,100,名字分别改成 第二次鞭炮,第二次牛丸 update 语句批量修改
# 订单明细中 春联数据逻辑删除 update 语句批量修改
# (在将原来数据还原的基础上进行的)
2.java实现(一个简单的springboot项目)
这篇文章写的也挺不错的
https://blog.youkuaiyun.com/lan_qinger/article/details/84138216
先展示效果,然后代码
根据订单id 查出的json效果:
{
"id": 1,
"name": "置办年货",
"isDelete": 0,
"detailsList": [
{
"id": 1,
"orderId": "1",
"commodityName": "鞭炮",
"commodityId": "1",
"commodityCount": 1,
"isDelete": 0
},
{
"id": 2,
"orderId": "1",
"commodityName": "春联",
"commodityId": "2",
"commodityCount": 1,
"isDelete": 0
},
{
"id": 3,
"orderId": "1",
"commodityName": "牛",
"commodityId": "3",
"commodityCount": 1,
"isDelete": 0
}
]
}
① 批量增加两条数据
入参:
{
"id": 1,
"name": "置办年货",
"isDelete": 0,
"detailsList": [
{
"id": 1,
"orderId": "1",
"commodityName": "鞭炮(edit name)",
"commodityId": "1",
"commodityCount": 1,
"isDelete": 0
},
{
"id": 2,
"orderId": "1",
"commodityName": "春联(edit count)",
"commodityId": "2",
"commodityCount": 100,
"isDelete": 0
},
{
"id": 3,
"orderId": "1",
"commodityName": "牛",
"commodityId": "3",
"commodityCount": 1,
"isDelete": 0
},
{
"orderId": "1",
"commodityName": "新增1",
"commodityId": "4",
"commodityCount": 1,
"isDelete": 0
},
{
"orderId": "1",
"commodityName": "新增2",
"commodityId": "5",
"commodityCount": 1,
"isDelete": 0
}
]
}
出参:
{
"id": 1,
"name": "置办年货",
"isDelete": 0,
"detailsList": [
{
"id": 1,
"orderId": 1,
"commodityName": "鞭炮(edit name)",
"commodityId": "1",
"commodityCount": 1,
"isDelete": 0
},
{
"id": 2,
"orderId": 1,
"commodityName": "春联(edit count)",
"commodityId": "2",
"commodityCount": 100,
"isDelete": 0
},
{
"id": 3,
"orderId": 1,
"commodityName": "牛",
"commodityId": "3",
"commodityCount": 1,
"isDelete": 0
},
{
"id": 14,
"orderId": 1,
"commodityName": "新增1",
"commodityId": "4",
"commodityCount": 1,
"isDelete": 0
},
{
"id": 15,
"orderId": 1,
"commodityName": "新增2",
"commodityId": "5",
"commodityCount": 1,
"isDelete": 0
}
]
}
② 批量删除两条
入参:
{
"id": 1,
"name": "置办年货",
"isDelete": 0,
"detailsList": [
{
"id": 1,
"orderId": "1",
"commodityName": "鞭炮(edit name)",
"commodityId": "1",
"commodityCount": 1,
"isDelete": 0
}
]
}
出参:
{
"id": 1,
"name": "置办年货",
"isDelete": 0,
"detailsList": [
{
"id": 1,
"orderId": 1,
"commodityName": "鞭炮(edit name)",
"commodityId": "1",
"commodityCount": 1,
"isDelete": 0
}
]
}
③ 逻辑编辑两条
入参:
{
"id": 1,
"name": "置办年货",
"isDelete": 0,
"detailsList": [
{
"id": 1,
"orderId": "1",
"commodityName": "鞭炮(edit name)",
"commodityId": "1",
"commodityCount": 1,
"isDelete": 0
},
{
"id": 2,
"orderId": "1",
"commodityName": "春联(edit count)",
"commodityId": "2",
"commodityCount": 100,
"isDelete": 0
},
{
"id": 3,
"orderId": "1",
"commodityName": "牛",
"commodityId": "3",
"commodityCount": 1,
"isDelete": 0
}
]
}
出参:
{
"id": 1,
"name": "置办年货",
"isDelete": 0,
"detailsList": [
{
"id": 1,
"orderId": 1,
"commodityName": "鞭炮(edit name)",
"commodityId": "1",
"commodityCount": 1,
"isDelete": 0
},
{
"id": 2,
"orderId": 1,
"commodityName": "春联(edit count)",
"commodityId": "2",
"commodityCount": 100,
"isDelete": 0
},
{
"id": 3,
"orderId": 1,
"commodityName": "牛",
"commodityId": "3",
"commodityCount": 1,
"isDelete": 0
}
]
}
④ 批量增加两条,批量编辑两条
元数据:
{
"id": 1,
"name": "置办年货",
"isDelete": 0,
"detailsList": [
{
"id": 1,
"orderId": 1,
"commodityName": "鞭炮",
"commodityId": "1",
"commodityCount": 1,
"isDelete": 0
},
{
"id": 2,
"orderId": 1,
"commodityName": "春联",
"commodityId": "2",
"commodityCount": 111,
"isDelete": 0
},
{
"id": 3,
"orderId": 1,
"commodityName": "牛",
"commodityId": "3",
"commodityCount": 1,
"isDelete": 0
},
{
"id": 14,
"orderId": 1,
"commodityName": "新增1",
"commodityId": "4",
"commodityCount": 1,
"isDelete": 0
},
{
"id": 15,
"orderId": 1,
"commodityName": "新增2",
"commodityId": "5",
"commodityCount": 1,
"isDelete": 0
}
]
}
入参:
{
"id": 1,
"name": "置办年货",
"isDelete": 0,
"detailsList": [
{
"id": 1,
"orderId": 1,
"commodityName": "鞭炮(批量编辑)",
"commodityId": "1",
"commodityCount": 1,
"isDelete": 0
},
{
"id": 2,
"orderId": 1,
"commodityName": "春联(批量删除)",
"commodityId": "2",
"commodityCount": 111,
"isDelete": 0
},
{
"id": 3,
"orderId": 1,
"commodityName": "牛",
"commodityId": "3",
"commodityCount": 1,
"isDelete": 0
}
]
}
出参:
{
"id": 1,
"name": "置办年货",
"isDelete": 0,
"detailsList": [
{
"id": 1,
"orderId": 1,
"commodityName": "鞭炮(批量编辑)",
"commodityId": "1",
"commodityCount": 1,
"isDelete": 0
},
{
"id": 2,
"orderId": 1,
"commodityName": "春联(批量删除)",
"commodityId": "2",
"commodityCount": 111,
"isDelete": 0
},
{
"id": 3,
"orderId": 1,
"commodityName": "牛",
"commodityId": "3",
"commodityCount": 1,
"isDelete": 0
}
]
}
⑤ 批量增加两条,批量删除两条
原数据:
{
"id": 1,
"name": "置办年货",
"isDelete": 0,
"detailsList": [
{
"id": 1,
"orderId": 1,
"commodityName": "鞭炮",
"commodityId": "1",
"commodityCount": 1,
"isDelete": 0
},
{
"id": 2,
"orderId": 1,
"commodityName": "春联",
"commodityId": "2",
"commodityCount": 1,
"isDelete": 0
},
{
"id": 3,
"orderId": 1,
"commodityName": "牛",
"commodityId": "3",
"commodityCount": 1,
"isDelete": 0
}
]
}
入参:
{
"id": 1,
"name": "置办年货",
"isDelete": 0,
"detailsList": [
{
"id": 1,
"orderId": 1,
"commodityName": "鞭炮",
"commodityId": "1",
"commodityCount": 1,
"isDelete": 0
},
{
"orderId": 1,
"commodityName": "新增1",
"commodityId": "4",
"commodityCount": 1,
"isDelete": 0
},
{
"orderId": 1,
"commodityName": "新增2",
"commodityId": "5",
"commodityCount": 1,
"isDelete": 0
}
]
}
出参:
{
"id": 1,
"name": "置办年货",
"isDelete": 0,
"detailsList": [
{
"id": 1,
"orderId": 1,
"commodityName": "鞭炮",
"commodityId": "1",
"commodityCount": 1,
"isDelete": 0
},
{
"orderId": 1,
"commodityName": "新增1",
"commodityId": "4",
"commodityCount": 1,
"isDelete": 0
},
{
"orderId": 1,
"commodityName": "新增2",
"commodityId": "5",
"commodityCount": 1,
"isDelete": 0
}
]
}
⑥批量编辑两条,批量删除两条
元数据:
{
"id": 1,
"name": "置办年货",
"isDelete": 0,
"detailsList": [
{
"id": 1,
"orderId": 1,
"commodityName": "鞭炮",
"commodityId": "1",
"commodityCount": 1,
"isDelete": 0
},
{
"id": 2,
"orderId": 1,
"commodityName": "春联",
"commodityId": "2",
"commodityCount": 1,
"isDelete": 0
},
{
"id": 3,
"orderId": 1,
"commodityName": "牛",
"commodityId": "3",
"commodityCount": 1,
"isDelete": 0
},
{
"id": 14,
"orderId": 1,
"commodityName": "删除1",
"commodityId": "4",
"commodityCount": 1,
"isDelete": 0
},
{
"id": 15,
"orderId": 1,
"commodityName": "删除2",
"commodityId": "5",
"commodityCount": 1,
"isDelete": 0
}
]
}
入参:
{
"id": 1,
"name": "置办年货",
"isDelete": 0,
"detailsList": [
{
"id": 1,
"orderId": 1,
"commodityName": "鞭炮(edit)",
"commodityId": "1",
"commodityCount": 1,
"isDelete": 0
},
{
"id": 2,
"orderId": 1,
"commodityName": "春联(edit)",
"commodityId": "2",
"commodityCount": 1,
"isDelete": 0
},
{
"id": 3,
"orderId": 1,
"commodityName": "牛",
"commodityId": "3",
"commodityCount": 1,
"isDelete": 0
}
]
}
出参:
{
"id": 1,
"name": "置办年货",
"isDelete": 0,
"detailsList": [
{
"id": 1,
"orderId": 1,
"commodityName": "鞭炮(edit)",
"commodityId": "1",
"commodityCount": 1,
"isDelete": 0
},
{
"id": 2,
"orderId": 1,
"commodityName": "春联(edit)",
"commodityId": "2",
"commodityCount": 1,
"isDelete": 0
},
{
"id": 3,
"orderId": 1,
"commodityName": "牛",
"commodityId": "3",
"commodityCount": 1,
"isDelete": 0
}
]
}
⑦ 批量新增两条,批量编辑两条,批量删除一条
元数据:
{
"id": 1,
"name": "置办年货",
"isDelete": 0,
"detailsList": [
{
"id": 1,
"orderId": 1,
"commodityName": "鞭炮",
"commodityId": "1",
"commodityCount": 1,
"isDelete": 0
},
{
"id": 2,
"orderId": 1,
"commodityName": "春联",
"commodityId": "2",
"commodityCount": 1,
"isDelete": 0
},
{
"id": 3,
"orderId": 1,
"commodityName": "牛",
"commodityId": "3",
"commodityCount": 1,
"isDelete": 0
}
]
}
入参:
{
"id": 1,
"name": "置办年货",
"isDelete": 0,
"detailsList": [
{
"id": 1,
"orderId": 1,
"commodityName": "鞭炮(edit)",
"commodityId": "1",
"commodityCount": 111111,
"isDelete": 0
},
{
"id": 2,
"orderId": 1,
"commodityName": "春联(edit)",
"commodityId": "2",
"commodityCount": 2222,
"isDelete": 0
},
{
"orderId": 1,
"commodityName": "新增1",
"commodityId": "4",
"commodityCount": 4444,
"isDelete": 0
},
{
"orderId": 1,
"commodityName": "新增2",
"commodityId": "5",
"commodityCount": 555,
"isDelete": 0
}
]
}
出参:
{
"id": 1,
"name": "置办年货",
"isDelete": 0,
"detailsList": [
{
"id": 1,
"orderId": 1,
"commodityName": "鞭炮(edit)",
"commodityId": "1",
"commodityCount": 111111,
"isDelete": 0
},
{
"id": 2,
"orderId": 1,
"commodityName": "春联(edit)",
"commodityId": "2",
"commodityCount": 2222,
"isDelete": 0
},
{
"id": 20,
"orderId": 1,
"commodityName": "新增1",
"commodityId": "4",
"commodityCount": 4444,
"isDelete": 0
},
{
"id": 21,
"orderId": 1,
"commodityName": "新增2",
"commodityId": "5",
"commodityCount": 555,
"isDelete": 0
}
]
}
代码如下:
entity:
package com.example.demo.entity;
import java.util.List;
/**
* @program: springboot_01
* @description:
* @author: guoyiguang
* @create: 2021-01-16 12:37
**/
public class CommodityOrder {
private Integer id ;
private String name ;
private Integer isDelete;
private List<CommodityOrderDetail> detailsList;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getIsDelete() {
return isDelete;
}
public void setIsDelete(Integer isDelete) {
this.isDelete = isDelete;
}
public List<CommodityOrderDetail> getDetailsList() {
return detailsList;
}
public void setDetailsList(List<CommodityOrderDetail> detailsList) {
this.detailsList = detailsList;
}
@Override
public String toString() {
return "CommodityOrder{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", isDelete=" + isDelete +
", detailsList=" + detailsList +
'}';
}
}
注意:重写equals和hashCode 只需要将id和orderId参与运算即可,因为其他字段值是可以变的,前段传的和后台查出来的不一样,但是他们是同一个对象
package com.example.demo.entity;
import java.util.Objects;
/**
* @program: springboot_01
* @description:
* @author: guoyiguang
* @create: 2021-01-16 12:39
**/
public class CommodityOrderDetail {
private Integer id ;
private Integer orderId ;
private String commodityName ;
private String commodityId ;
private Integer commodityCount ;
private Integer isDelete;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getOrderId() {
return orderId;
}
public void setOrderId(Integer orderId) {
this.orderId = orderId;
}
public String getCommodityName() {
return commodityName;
}
public void setCommodityName(String commodityName) {
this.commodityName = commodityName;
}
public String getCommodityId() {
return commodityId;
}
public void setCommodityId(String commodityId) {
this.commodityId = commodityId;
}
public Integer getCommodityCount() {
return commodityCount;
}
public void setCommodityCount(Integer commodityCount) {
this.commodityCount = commodityCount;
}
public Integer getIsDelete() {
return isDelete;
}
public void setIsDelete(Integer isDelete) {
this.isDelete = isDelete;
}
@Override
public String toString() {
return "CommodityOrderDetail{" +
"id='" + id + '\'' +
", orderId='" + orderId + '\'' +
", commodityName='" + commodityName + '\'' +
", commodityId='" + commodityId + '\'' +
", commodityCount=" + commodityCount +
", isDelete=" + isDelete +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CommodityOrderDetail that = (CommodityOrderDetail) o;
return Objects.equals(id, that.id) &&
Objects.equals(orderId, that.orderId);
}
@Override
public int hashCode() {
return Objects.hash(id, orderId);
}
}
package com.example.demo.entity.Vo;
import com.example.demo.entity.CommodityOrderDetail;
import java.util.List;
/**
* @program: springboot_01
* @description:
* @author: guoyiguang
* @create: 2021-01-16 12:37
**/
public class CommodityOrderVo {
private Integer id ;
private String name ;
private Integer isDelete;
private List<CommodityOrderDetail> toEditDetailsList;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getIsDelete() {
return isDelete;
}
public void setIsDelete(Integer isDelete) {
this.isDelete = isDelete;
}
public List<CommodityOrderDetail> getDetailsList() {
return toEditDetailsList;
}
public void setDetailsList(List<CommodityOrderDetail> detailsList) {
this.toEditDetailsList = detailsList;
}
@Override
public String toString() {
return "CommodityOrder{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", isDelete=" + isDelete +
", toEditDetailsList=" + toEditDetailsList +
'}';
}
}
Mapper :
package com.example.demo.mapper;
import com.example.demo.entity.CommodityOrder;
import com.example.demo.entity.Vo.CommodityOrderVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
@Repository
public interface CommodityOrderMapper {
CommodityOrder getByOrderId(@Param("id") Integer id);
int updateOrderAndDetails(CommodityOrderVo commodityOrderVo);
}
package com.example.demo.mapper;
import com.example.demo.entity.CommodityOrder;
import com.example.demo.entity.CommodityOrderDetail;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface CommodityOrderDetailMapper {
int batchInsertDetails(List<CommodityOrderDetail> list);
List<CommodityOrderDetail> selectListByOrderId(@Param("orderId") String orderId);
}
<?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.example.demo.mapper.CommodityOrderMapper" >
<resultMap id="BaseResultMap" type="com.example.demo.entity.CommodityOrder">
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="is_delete" property="isDelete" jdbcType="INTEGER" />
<collection property="detailsList" javaType="java.util.ArrayList" ofType="com.example.demo.entity.CommodityOrderDetail"
select="com.example.demo.mapper.CommodityOrderDetailMapper.selectListByOrderId" column="id" >
<!--column="id" : 将主表id 列上的值作为参数传进来 给collection了里 com.example.demo.mapper.CommodityOrderDetailMapper.selectList 作为实参
先查出主表的结果, 然后主表记录数是几 就执行几次 collection 的select,
javaType和ofType 写不写都行,
select的值: 对应xml的namespace + 对应xml中的代码片段的id,
column作为select语句的参数传入,
如果只传一个参数id可以简写: column="id";
传多个参数: column="{orderId=id,isDelete=is_delete}" ;orderId/isDelete 是定义的变量名, id/is_delete 是主表的字段id/isDelete,将字段值传给collection里的sql
-->
<id column="id" property="id" jdbcType="INTEGER" />
<result column="orderId" property="orderId" jdbcType="INTEGER" />
<result column="commodity_name" property="commodityName" jdbcType="VARCHAR" />
<result column="commodity_id" property="commodityId" jdbcType="VARCHAR" />
<result column="commodity_count" property="commodityCount" jdbcType="INTEGER" />
<result column="is_delete" property="isDelete" jdbcType="INTEGER" />
</collection>
</resultMap>
<select id="getByOrderId" resultMap="BaseResultMap">
select id, name, custom_id, is_delete from commodity_order where id = #{id} and is_delete = 0
</select>
<!--修改订单主字段 同时批量修改订单明细子数据-->
<update id="updateOrderAndDetails" parameterType="com.example.demo.entity.Vo.CommodityOrderVo">
update (
<foreach collection="toEditDetailsList" item="item" open="(" separator=" union " close=")" index="index">
select #{item.id} as detail_id, #{item.commodityName} as tmp_commodity_name, #{item.commodityCount} as tmp_commodity_count , #{item.isDelete} as isDelete
</foreach> as tmp
inner join commodity_order_detail d on d.id = tmp.detail_id and d.is_delete = 0
inner join commodity_order o on o.id = d.order_id and o.is_delete = d.is_delete
and o.id = 1
) set o.name = #{name},
d.commodity_name = tmp.tmp_commodity_name,
d.commodity_count = tmp.tmp_commodity_count,
d.is_delete = tmp.isDelete
where o.id = #{id}
</update>
</mapper>
<?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.example.demo.mapper.CommodityOrderDetailMapper" >
<resultMap id="BaseResultMap" type="com.example.demo.entity.CommodityOrderDetail">
<id column="id" property="id" jdbcType="INTEGER" />
<result column="order_id" property="orderId" jdbcType="INTEGER" />
<result column="commodity_name" property="commodityName" jdbcType="VARCHAR" />
<result column="commodity_id" property="commodityId" jdbcType="VARCHAR" />
<result column="commodity_count" property="commodityCount" jdbcType="INTEGER" />
<result column="is_delete" property="isDelete" jdbcType="INTEGER" />
</resultMap>
<!-- 批量新增-->
<insert id="batchInsertDetails" parameterType="java.util.List">
INSERT INTO commodity_order_detail(order_id,commodity_name ,commodity_id,commodity_count,is_delete)
VALUES
<foreach collection="list" item="item" separator=",">
(#{item.orderId,jdbcType=VARCHAR},#{item.commodityName,jdbcType=VARCHAR},#{item.commodityId,jdbcType=VARCHAR},#{item.commodityCount,jdbcType=VARCHAR},0)
</foreach>
</insert>
<!--根据主订单id获取对应子订单信息-->
<select id="selectListByOrderId" resultMap="BaseResultMap">
select id, order_id, commodity_name, commodity_id, commodity_count, is_delete
from commodity_order_detail
where order_id = #{orderId}
and is_delete = 0
</select>
</mapper>
service:
package com.example.demo.service;
import com.example.demo.entity.CommodityOrder;
import com.example.demo.entity.Vo.CommodityOrderVo;
public interface CommodityOrderService {
CommodityOrder getByOrderId(Integer id);
int updateOrderAndDetails(CommodityOrderVo commodityOrderVo);
}
package com.example.demo.service;
import com.example.demo.entity.CommodityOrderDetail;
import java.util.List;
public interface CommodityOrderDetailService {
int batchInsertDetails(List<CommodityOrderDetail> list);
List<CommodityOrderDetail> selectListByOrderId(String orderId);
}
serviceImpl:
package com.example.demo.service.Impl;
import com.example.demo.entity.CommodityOrder;
import com.example.demo.entity.CommodityOrderDetail;
import com.example.demo.entity.Vo.CommodityOrderVo;
import com.example.demo.mapper.CommodityOrderMapper;
import com.example.demo.service.CommodityOrderDetailService;
import com.example.demo.service.CommodityOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* @program: springboot_01
* @description:
* @author: guoyiguang
* @create: 2021-01-16 13:09
**/
@Service
public class CommodityOrderServiceImpl implements CommodityOrderService {
@Autowired
CommodityOrderMapper commodityOrderMapper;
@Autowired
CommodityOrderDetailService commodityOrderDetailService;
@Override
public CommodityOrder getByOrderId(Integer id){
return commodityOrderMapper.getByOrderId(id);
}
@Override
public int updateOrderAndDetails(CommodityOrderVo commodityOrderVo) {
// 新增数据的容器
List<CommodityOrderDetail> addList = new ArrayList();
// 获取表里数据
CommodityOrder orderFromDb = commodityOrderMapper.getByOrderId(commodityOrderVo.getId());
if (!CollectionUtils.isEmpty(orderFromDb.getDetailsList())) {
List<CommodityOrderDetail> detailsFromDb = orderFromDb.getDetailsList();
// 前段返回的订单明细 集合 中有数据
if (!CollectionUtils.isEmpty(commodityOrderVo.getDetailsList())) {
// 过滤出 detailsFromDb 没有的数据为新增 (也可以考虑 removeAll 不过原来的集合会改变)
addList = commodityOrderVo.getDetailsList()
.stream()
// contains 涉及到自定义对象的 是否一样,需要重写 equals 和 hasCode 方法;此处只需要 id,和orderId一样就认为是同一个对象
// 如果把CommodityOrderDetail 里的所有属性 都参与 equals 和 hasCode,数据库里的一条数据 和前端 传过来根据id ,name 等其他字段判断的时候
// 发现不一样,就认为这是两条不同的数据,所以重写 equals和hashCode 的时候只需要让id 和 orderId 参与运算即可
.filter(detail -> !detailsFromDb.contains(detail))
.collect(Collectors.toList());
}
}
// 新增
if (!CollectionUtils.isEmpty(addList)) {
// 批量入库
int count = commodityOrderDetailService.batchInsertDetails(addList);
System.out.println("批量插入 明细表的 条数为--------------------------> "+ count);
}
// 修改(编辑 + 逻辑删除)
// 修改 前端一般不传要删掉的订单明细id,只传要编辑的订单明细id和业务数据 commodityOrderVo.getEditList() 只包含了要编辑的业务数据
// 获取要逻辑删除的数据 :(库里有,前端没有传的)
List<CommodityOrderDetail> logicDeleteList = orderFromDb.getDetailsList()
.stream()
.filter(detail ->!commodityOrderVo.getDetailsList().contains(detail) )
.map(de -> {
// 设置成删除
de.setIsDelete(1);
return de;
})
.collect(Collectors.toList());
if (!CollectionUtils.isEmpty(commodityOrderVo.getDetailsList()) &&!CollectionUtils.isEmpty(logicDeleteList) ) {
// 要编辑的业务数据 + 要删除的业务数据
commodityOrderVo.getDetailsList().addAll(logicDeleteList);
}
return commodityOrderMapper.updateOrderAndDetails(commodityOrderVo);
}
}
package com.example.demo.service.Impl;
import com.example.demo.entity.CommodityOrderDetail;
import com.example.demo.mapper.CommodityOrderDetailMapper;
import com.example.demo.service.CommodityOrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @program: springboot_01
* @description:
* @author: guoyiguang
* @create: 2021-01-16 13:10
**/
@Service
public class CommodityOrderDetailServiceImpl implements CommodityOrderDetailService {
@Autowired
CommodityOrderDetailMapper commodityOrderDetailMapper;
@Override
public int batchInsertDetails(List<CommodityOrderDetail> list) {
return commodityOrderDetailMapper.batchInsertDetails(list);
}
@Override
public List<CommodityOrderDetail> selectListByOrderId(String orderId) {
// 暂时不实现 ,因为目前是通过 主订单来操作 子表的
return null;
}
}
pom:
<?xml version="1.0"?><project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<!-- 项目信息 begin -->
<groupId>com.microservice</groupId>
<artifactId>spring-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-web</name>
<url>http://maven.apache.org</url>
<!-- 项目信息end -->
<!-- 属性配置 begin -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<!-- 属性配置end -->
<!-- 父依赖 begin -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<!-- 父依赖 end -->
<dependencies>
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
<!-- 添加web包 begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 该包中包含requestMapping restController 等注解 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 添加web包 end -->
<!-- mybatis依赖 begin -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- mybatis依赖 end -->
<!-- mysql数据库配置 begin -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
<!-- mysql数据库配置 end -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
配置文件:
server.port=8080
#redis
spring.redis.host=localhost
spring.redis.port=6379
#spring.redis.password=bigdata123
spring.redis.database=0
#spring.redis.timeout=0
spring.redis.pool.maxTotal=8
spring.redis.pool.maxWaitMillis=1000
#spring.redis.pool.max-idle=8 # pool settings ...
#spring.redis.pool.min-idle=0
#spring.redis.pool.max-active=8
#spring.redis.pool.max-wait=-1
#spring.redis.sentinel.master= # name of Redis server
#spring.redis.sentinel.nodes= # comma-separated list of host:port pairs
# 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/sql_test?serverTimezone=UTC&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456
#连接池配置
#spring.datasource.type=org.apache.commons.dbcp2.BasicDataSource
#mybatis
#entity扫描的包名
mybatis.type-aliases-package=com.springboot.demo.model
#Mapper.xml所在的位置
mybatis.mapper-locations=classpath*:/mybatis/mapper/*Mapper.xml
# 下面这个配置报错: Invalid bound statement (not found): com.example.demo.mapper.RegionMapper.list
#mybatis.mapper-location=classpath*:/mybatis/mapper/*Mapper.xml
#开启MyBatis的二级缓存
mybatis.configuration.cache-enabled=true
#pagehelper
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
#日志配置
logging.level.com.xiaolyuh=debug
logging.level.org.springframework.web=debug
logging.level.org.springframework.transaction=debug
logging.level.org.mybatis=debug
# mybatis plus log
mybatis.configuration.log-impl =org.apache.ibatis.logging.stdout.StdOutImpl
什么时候要写equals和hashCode?当两个对象比较是否相等,集合里是否含有这个自定义对象的时候?具体要哪些字段参与 hash计算根据业务来
对所有字段参与hash
package com.example.demo.entity;
import java.util.Objects;
/**
* @program: springboot_01
* @description:
* @author: guoyiguang
* @create: 2021-01-16 12:39
**/
public class CommodityOrderDetail {
private Integer id ;
private Integer orderId ;
private String commodityName ;
private String commodityId ;
private Integer commodityCount ;
private Integer isDelete;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getOrderId() {
return orderId;
}
public void setOrderId(Integer orderId) {
this.orderId = orderId;
}
public String getCommodityName() {
return commodityName;
}
public void setCommodityName(String commodityName) {
this.commodityName = commodityName;
}
public String getCommodityId() {
return commodityId;
}
public void setCommodityId(String commodityId) {
this.commodityId = commodityId;
}
public Integer getCommodityCount() {
return commodityCount;
}
public void setCommodityCount(Integer commodityCount) {
this.commodityCount = commodityCount;
}
public Integer getIsDelete() {
return isDelete;
}
public void setIsDelete(Integer isDelete) {
this.isDelete = isDelete;
}
@Override
public String toString() {
return "CommodityOrderDetail{" +
"id='" + id + '\'' +
", orderId='" + orderId + '\'' +
", commodityName='" + commodityName + '\'' +
", commodityId='" + commodityId + '\'' +
", commodityCount=" + commodityCount +
", isDelete=" + isDelete +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CommodityOrderDetail that = (CommodityOrderDetail) o;
return Objects.equals(id, that.id) &&
Objects.equals(orderId, that.orderId) &&
Objects.equals(commodityName, that.commodityName) &&
Objects.equals(commodityId, that.commodityId) &&
Objects.equals(commodityCount, that.commodityCount) &&
Objects.equals(isDelete, that.isDelete);
}
@Override
public int hashCode() {
return Objects.hash(id, orderId, commodityName, commodityId, commodityCount, isDelete);
}
}
测试代码:
CommodityOrderDetail cod1 = new CommodityOrderDetail();
cod1.setId(1);
cod1.setOrderId(1);
cod1.setCommodityId("1");
cod1.setCommodityName("苹果");
cod1.setCommodityCount(1);
cod1.setIsDelete(0);
CommodityOrderDetail cod2 = new CommodityOrderDetail();
cod2.setId(1);
cod2.setOrderId(1);
cod2.setCommodityId("1");
cod2.setCommodityName("苹果");
cod2.setCommodityCount(1);
cod2.setIsDelete(0);
CommodityOrderDetail cod3 = new CommodityOrderDetail();
cod3.setId(1);
cod3.setOrderId(1);
cod3.setCommodityId("1");
cod3.setCommodityName("苹果苹果");
cod3.setCommodityCount(10000);
cod3.setIsDelete(1);
System.out.println("----------------------");
// false
System.out.println(cod1 == cod2);
// true
System.out.println(cod1.equals(cod2));
System.out.println("----------------------");
// false
System.out.println(cod1 == cod3);
System.out.println("----------------------");
// false
System.out.println(cod1.equals(cod3));
出现一个场景:
我认为只要 是 订单明细的id 和 orderId 值一样,我就认为这两个对象是一样的(等值的),即需要认为 cod1.equals(cod2) ;od2.equals(cod3) ;
cod1 , cod2 , cod3 是同一个对象
只需要hashCode和equals 的时候 对 id和orderId 参与运算即可,其他字段前端传过来和库里的可能不一样,就不参与 hash运算
package com.example.demo.entity;
import java.util.Objects;
/**
* @program: springboot_01
* @description:
* @author: guoyiguang
* @create: 2021-01-16 12:39
**/
public class CommodityOrderDetail {
private Integer id ;
private Integer orderId ;
private String commodityName ;
private String commodityId ;
private Integer commodityCount ;
private Integer isDelete;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getOrderId() {
return orderId;
}
public void setOrderId(Integer orderId) {
this.orderId = orderId;
}
public String getCommodityName() {
return commodityName;
}
public void setCommodityName(String commodityName) {
this.commodityName = commodityName;
}
public String getCommodityId() {
return commodityId;
}
public void setCommodityId(String commodityId) {
this.commodityId = commodityId;
}
public Integer getCommodityCount() {
return commodityCount;
}
public void setCommodityCount(Integer commodityCount) {
this.commodityCount = commodityCount;
}
public Integer getIsDelete() {
return isDelete;
}
public void setIsDelete(Integer isDelete) {
this.isDelete = isDelete;
}
@Override
public String toString() {
return "CommodityOrderDetail{" +
"id='" + id + '\'' +
", orderId='" + orderId + '\'' +
", commodityName='" + commodityName + '\'' +
", commodityId='" + commodityId + '\'' +
", commodityCount=" + commodityCount +
", isDelete=" + isDelete +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CommodityOrderDetail that = (CommodityOrderDetail) o;
return Objects.equals(id, that.id) &&
Objects.equals(orderId, that.orderId);
}
@Override
public int hashCode() {
return Objects.hash(id, orderId);
}
}
测试代码及结果:
CommodityOrderDetail cod1 = new CommodityOrderDetail();
cod1.setId(1);
cod1.setOrderId(1);
cod1.setCommodityId("1");
cod1.setCommodityName("苹果");
cod1.setCommodityCount(1);
cod1.setIsDelete(0);
CommodityOrderDetail cod2 = new CommodityOrderDetail();
cod2.setId(1);
cod2.setOrderId(1);
cod2.setCommodityId("1");
cod2.setCommodityName("苹果");
cod2.setCommodityCount(1);
cod2.setIsDelete(0);
CommodityOrderDetail cod3 = new CommodityOrderDetail();
cod3.setId(1);
cod3.setOrderId(1);
cod3.setCommodityId("1");
cod3.setCommodityName("苹果苹果");
cod3.setCommodityCount(10000);
cod3.setIsDelete(1);
System.out.println("----------------------");
// false
System.out.println(cod1 == cod2);
// true
System.out.println(cod1.equals(cod2));
System.out.println("----------------------");
// false
System.out.println(cod1 == cod3);
System.out.println("----------------------");
// true
System.out.println(cod1.equals(cod3));