Mybatis-Plus根据条件更新

Mybatis-Plus根据条件更新

在 Mybatis-Plus 项目中,很多时候需要根据条件更新特定的数据行,这时候可以使用到提供的 update() 方法。

下面以 PostCategories 对象为例简单演示下使用的方法。

1、创建对象并填入要更新的字段数据

例如更新 homePage 字段值为 false

PostCategories updateMessage = new PostCategories();
updateMessage.setHomePage(false);

2、创建条件构造器和查询条件

new QueryWrapper<>() 为查询条件构造器,这里用 UpdateWrapper<> 更新条件构造器也可,都继承于AbstractWrapper所以有很多共通的点;

eq("homePage", true) 即是查询条件: where homePage = true,条件根据需要添加。

//查询条件构造器
postCategoriesMapper.update(updateMessage, new QueryWrapper<PostCategories>().eq("homePage", true));
//更新条件构造器
postCategoriesMapper.update(updateMessage, new UpdateWrapper<PostCategories>().eq("homePage", true));

3、查看打印的sql语句

image-20201116144348357

这样就达到了条件更新的目的。

posted @ 2020-11-16 14:47  MyDistance  阅读(13759)  评论(0)    收藏  举报