tk.mybatis之updateByPrimaryKey、updateByPrimaryKeySelective、updateByExampleSelective、updateByExample记录

目录

 

方法:updateByPrimaryKey      更新对象的字段为null也会被更新,数据库数据被覆盖

方法:updateByPrimaryKeySelective       只会更新对象中有值字段

方法:updateByExampleSelective     只会更新满足条件的数据中对象中有字段的值

方法:updateByExample  更新满足条件的数据中对象所有值,包括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

结论:按条件更新,满足条件的数据库表数据,都会更新成对象的值,没有给值的字段也会被更新到数据库中。

参考:

https://blog.csdn.net/tengdazhang770960436/article/details/80889370

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值