方法:updateByPrimaryKey 更新对象的字段为null也会被更新,数据库数据被覆盖
SBox record1 = new SBox();
record1.setStreamBoxId(sBoxes.get(0).getStreamBoxId());
System.out.println(record1.getStreamBoxId());
record1.setCapability(3001d);
record1.setUseState((byte)1);
record1.setOpState((byte)1);
streamBoxDao.updateByPrimaryKey(record1);
sql解析:
==> Preparing: UPDATE stream_box SET stream_line_id = ?,start_time = ?,end_time =
?,capability = ?,use_state = ?,op_state = ?,set_id = ?,req_id = ?,spec_id = ?,pre_set_id
= ?,pre_use_capability = ?,last_set_id = ?,last_use_capability = ? WHERE stream_box_id =
?
==> Parameters: null, null, null, 3001.0(Double), 1(Byte), 1(Byte), null, null, null,
null, null, null, null, sboxinsertTKMybatis1(String)
结论:更新的对象中,没有给值的字段也会被更新到数据库中。
方法:updateByPrimaryKeySelective 只会更新对象中有值字段
SBox record2 = new SBox();
record2.setStreamBoxId(sBoxes.get(1).getStreamBoxId());
record2.setCapability(3002d);
record2.setUseState((byte)1);
record2.setOpState((byte)1);
System.out.println(record2.getStreamBoxId());
streamBoxDao.updateByPrimaryKeySelective(record2);
sql解析:
==> Preparing: UPDATE stream_box SET capability = ?,use_state = ?,op_state = ? WHERE stream_box_id = ?
==> Parameters: 3002.0(Double), 1(Byte), 1(Byte), sboxinsertTKMybatis2(String)
<== Updates: 1
结论:更新的对象中,没有给值的字段不会被更新到数据库中,只会更新对象中有值字段
方法:updateByExampleSelective 只会更新满足条件的数据中对象中有字段的值
SBox record3 = new SBox();
record3.setStreamBoxId(sBoxes.get(3).getStreamBoxId());
record3.setCapability(3004d);
record3.setUseState((byte)1);
record3.setOpState((byte)1);
System.out.println(record3.getStreamBoxId());
Example example = new Example(SBox.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("streamBoxId",record3.getStreamBoxId());
streamBoxDao.updateByExampleSelective(record3,example);
sql解析
==> Preparing: UPDATE stream_box SET capability = ?,use_state = ?,op_state = ? WHERE ( ( stream_box_id = ? ) )
==> Parameters: 3004.0(Double), 1(Byte), 1(Byte), sboxinsertTKMybatis4(String)
<== Updates: 1
结论:按条件更新成对象中有字段的值,没有给值的字段不会被更新到数据库中,只会更新对象中有值字段
方法:updateByExample 更新满足条件的数据中对象所有值,包括null
SBox record3 = new SBox();
record3.setStreamBoxId(sBoxes.get(3).getStreamBoxId());
record3.setCapability(3005d);
record3.setUseState((byte)1);
record3.setOpState((byte)1);
System.out.println(record3.getStreamBoxId());
Example example = new Example(SBox.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("streamBoxId",record3.getStreamBoxId());
//streamBoxDao.updateByExampleSelective(record3,example);
streamBoxDao.updateByExample(record3,example);
sql解析
==> Preparing: UPDATE stream_box SET stream_line_id = ?,start_time = ?,end_time = ?,capability = ?,use_state = ?,op_state = ?,set_id = ?,req_id = ?,spec_id = ?,pre_set_id = ?,pre_use_capability = ?,last_set_id = ?,last_use_capability = ? WHERE ( ( stream_box_id = ? ) )
==> Parameters: null, null, null, 3005.0(Double), 1(Byte), 1(Byte), null, null, null, null, null, null, null, sboxinsertTKMybatis4(String)
<== Updates: 1
结论:按条件更新,满足条件的数据库表数据,都会更新成对象的值,没有给值的字段也会被更新到数据库中。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。