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

被折叠的 条评论
为什么被折叠?



