MBG中update语句的区别

博客介绍了Java中MyBatis的数据更新方式。包括按主键更新和按条件更新,按主键更新需提供主键,按条件更新则根据Example构建的条件更新,还提及使用MBG实现同等效果,如根据mobile字段更新User的name字段。

int updateByPrimaryKey(User record) thorws SQLException 按主键更新
int updateByPrimaryKeySelective(User record) thorws SQLException 按主键更新值不为null的字段

使用以上的方式更新数据时必须提供主键,MyBatis根据主键进行数据记录的更新。


int updateByExample(User record, UserExample example) thorws SQLException 按条件更新
int updateByExampleSelective(User record, UserExample example) thorws SQLException 按条件更新值不为null的字段

使用以上的方式更新数据时不用提供主键,根据Example中构建的条件MyBatis就可以进行数据记录的更新。

例如:

update tb_user set name=#{name} where mobile=#{mobile}

使用MBG实现同等效果就是:

public Integer updateUser(String name,String mobile)
    TbUser user = new TbUser();
    user.setName(name);
    TbUserExample example=new TbUserExample();
    example.createCriteria().andMobileEqualTo(mobile);
    return userMapper.updateByExampleSelective(user,example);

这样无论TbUser表中有多少字段,只会根据mobile字段的情况更新User的name字段。
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值