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 oinner 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_idunionselect 2 as detail_id) tmp_detailinner 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_idunionselect 2 as detail_id) tmp_detailinner 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_idunionselect 2 as detail_id) tmp_detailinner join commodity_order_detail detail on tmp_detail.detail_id = detail.id and detail.is_delete = 0inner 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_idunionselect 2 as detail_id) tmp_detailinner join commodity_order_detail detail on tmp_detail.detail_id = detail.id and detail.is_delete = 0inner 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 oinner 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_countunionselect '3' detail_id, '第二次牛丸' as tmp_commodity_name, '100' as commodity_count) as tmpinner join commodity_order_detail d on d.id = tmp.detail_id and d.is_delete = 0inner 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_countunionselect '3' detail_id, '第二次牛丸' as tmp_commodity_name, '100' as tmp_commodity_count) as tmpinner join commodity_order_detail d on d.id = tmp.detail_id and d.is_delete = 0inner 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.csdn.net/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;}@Overridepublic 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;}@Overridepublic String toString() {return "CommodityOrderDetail{" +"id='" + id + '\'' +", orderId='" + orderId + '\'' +", commodityName='" + commodityName + '\'' +", commodityId='" + commodityId + '\'' +", commodityCount=" + commodityCount +", isDelete=" + isDelete +'}';}@Overridepublic 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);}@Overridepublic 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;}@Overridepublic 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 tmpinner join commodity_order_detail d on d.id = tmp.detail_id and d.is_delete = 0inner join commodity_order o on o.id = d.order_id and o.is_delete = d.is_deleteand 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.isDeletewhere  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_deletefrom commodity_order_detailwhere 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 {@AutowiredCommodityOrderMapper commodityOrderMapper;@AutowiredCommodityOrderDetailService commodityOrderDetailService;@Overridepublic CommodityOrder getByOrderId(Integer id){return  commodityOrderMapper.getByOrderId(id);}@Overridepublic 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 {@AutowiredCommodityOrderDetailMapper commodityOrderDetailMapper;@Overridepublic int batchInsertDetails(List<CommodityOrderDetail> list) {return commodityOrderDetailMapper.batchInsertDetails(list);}@Overridepublic List<CommodityOrderDetail> selectListByOrderId(String orderId) {// 暂时不实现 ,因为目前是通过 主订单来操作 子表的return null;}
}

pom:

<?xml version="1.0"?><projectxsi: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=0spring.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;}@Overridepublic String toString() {return "CommodityOrderDetail{" +"id='" + id + '\'' +", orderId='" + orderId + '\'' +", commodityName='" + commodityName + '\'' +", commodityId='" + commodityId + '\'' +", commodityCount=" + commodityCount +", isDelete=" + isDelete +'}';}@Overridepublic 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);}@Overridepublic 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("----------------------");// falseSystem.out.println(cod1 == cod2);// trueSystem.out.println(cod1.equals(cod2));System.out.println("----------------------");// falseSystem.out.println(cod1 == cod3);System.out.println("----------------------");// falseSystem.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;}@Overridepublic String toString() {return "CommodityOrderDetail{" +"id='" + id + '\'' +", orderId='" + orderId + '\'' +", commodityName='" + commodityName + '\'' +", commodityId='" + commodityId + '\'' +", commodityCount=" + commodityCount +", isDelete=" + isDelete +'}';}@Overridepublic 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);}@Overridepublic 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("----------------------");// falseSystem.out.println(cod1 == cod2);// trueSystem.out.println(cod1.equals(cod2));System.out.println("----------------------");// falseSystem.out.println(cod1 == cod3);System.out.println("----------------------");// trueSystem.out.println(cod1.equals(cod3));

mysql 一个update语句 对主表内容和子表批量修改相关推荐

  1. MYSQL(二):update语句执行的秘密

    MYSQL(二):update语句执行的秘密 在上一篇博客mysql查询里,已经大致讲了MySQL的一些组件,和MySQL的查询语句是如何返回结果的,现在再来研究一下MySQL是如何进行更新操作的,虽 ...

  2. MySQL数据库update语句使用详解

    MySQL数据库update语句使用详解 本篇文章我们来讲讲如何对MySQL数据库进行更新操作,对数据库进行增删改查操作是我们必会的基础之一,会了这个增删改查我们可以在这基础上去编写更多的东西,废话不 ...

  3. 警示:一个update语句引起大量gc等待和业务卡顿

    墨墨导读:业务卡顿异常,有几个 insert into 语句的gc等待比较严重,发生业务超时,本文分析了超时原因并详述整个处理过程,希望对大家有帮助. 1. 故障现象 客户报2020年7月9号,8点3 ...

  4. DBeaver mysql 外键设置了级联删除,子表无法添加数据

    DBeaver mysql 外键设置了级联删除,子表无法添加数据 报错:Cannot add or update a child row: a foreign key constraint fails ...

  5. 把我坑惨的一个update语句!

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试资料 来源:ju.outofmemory.cn/entry/336774 ...

  6. mysql update锁表_MySQL执行update语句是锁行还是锁表分析

    我们在数据库执行update语句的时候,到底是锁表还是锁行?这里直接用MySQL上例子测试下. 一.环境准备 1.新建一个表create table test_update( id BIGINTnot ...

  7. mysql的更新语句_IT大叔详谈mysql中update语句和delete语句及应用

    学习是一件枯燥的事,你要牺牲自己的业余时间,你要忍受孤独,坚持下来了你就胜利了,学习是一个过程,只要循序渐进,每天进步一点点,只有这样你才能提高进而成功.今天老韩来讲一讲mysql中的update语句 ...

  8. 【Mysql】 update语句更新原理

    update语句的执行流程图,浅色框表示在InnoDB内部执行的,深色框表示在执行器中执行的. 1.首先客户端通过tcp/ip发送一条sql语句到server层的SQL interface 2.SQL ...

  9. 创建数据库mysql的sql语句是_创建数据库和表的SQL语句

    SQL常用语句: CREATE DATABASE 库名;创建数据库 DROP DATABASE库名: 删除数据库 USE 库名; (1) 数据记录筛选: sql="select * from ...

最新文章

  1. angular5 httpclient的示例实战
  2. gpu无法调用解决方案记录
  3. linux C/C++开发环境搭建指南
  4. 如何远程管理Quartz
  5. Mycat和Mysql搭建高可用企业数据库集群
  6. JavaScript实现封闭区域布尔运算
  7. 第一次接触终极事务处理——Hekaton
  8. 如何设置ubuntu的PATH环境变量
  9. CPU制造工艺完整过程(图文)
  10. 使用scala使用fastjson将map转json报错
  11. 【华为OJ】【算法总篇章】
  12. 《数据结构》C语言版(清华严蔚敏考研版) 全书知识梳理 + 练习习题详解(超详细清晰易懂)
  13. 用粉红噪声煲机_煲机知识 | 煲机常用的粉红噪音和白噪音是什么?
  14. iphone 4 到iphone 6s plus的屏幕尺寸
  15. 【GA MTSP】基于matlab遗传算法求解多旅行商问题(同始终点)【含Matlab源码 1338期】
  16. PHP与其他语言的比较
  17. android 锁屏界面状态栏_Android锁屏下显示来电通知界面
  18. iOS中PCH文件的使用
  19. 【文献数据速递】CEO绿色经历能否促进企业绿色创新
  20. 四种连接类型:inner(内连接),left[outer](左外连接),right[outer](右外连接),full[outer](完全外连接)

热门文章

  1. 《计算机网络自顶向下方法》读书笔记(一)
  2. PLC快速联网,实现设备远程监测管理
  3. 测试需要掌握的重点概念
  4. 给对话框添加菜单 工具栏 状态栏简易方法
  5. 知名大学硕博论文及英文期刊全文资源集合
  6. 求10000!的阶乘末尾一共有多少个0
  7. linux中安装maven插件,Maven插件wagon
  8. 装完linux硬盘有密码吗,deepin 15.11安装过程中全盘安装/全盘加密后查看磁盘信息...
  9. 四叶玫瑰花数java代码_Java 循环语句 四叶玫瑰数,奇数偶数求和
  10. 【历史上的今天】12 月 5 日:分布式系统的“三驾马车”;世界上第一篇计算机科学博士论文;IBM 推出“深蓝”计算机