区别dateById(),updateByPrimaryKey、updateByPrimaryKeySelective、updateByExampleSelective、updateByExample

方法:Mybatis-plus的updateById()方法来更新一条记录时:只会更新不为null的字段,为null的字段会不变。

结论:只会更新不为null的字段,为null的字段会不变。

该方法,建议:new一个新的实体,将id赋值,然后把需要更新的字段set下。

如果真的需要把一个字段设置为null,可以在该字段上加上注解:

@TableField(updateStrategy = FieldStrategy.IGNORED)
1
让mybatisplus忽略判断。该字段默认的模式是NOT_NULL,即通过接口更新数据时数据为NULL值时将不更新进数据库。

方法: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
结论:按条件更新,满足条件的数据库表数据,都会更新成对象的值,没有给值的字段也会被更新到数据库中。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值