mybatisplus判断数据库中ymd字段是否存在与str字符串相等的值若不相等则添加
可以使用MyBatis-Plus的Wrapper进行条件查询和更新操作。
假设ymd字段的类型为字符串,可以按照以下步骤进行操作:
- 创建一个Wrapper对象,指定查询条件为ymd字段等于str字符串。
Wrapper<Entity> wrapper = new QueryWrapper<Entity>().eq("ymd", str);
- 使用selectCount方法查询符合条件的记录数。
int count = entityMapper.selectCount(wrapper);
- 判断count是否等于0,如果等于0则说明数据库中不存在与str字符串相等的ymd值,需要添加一条新记录。
if(count == 0){
Entity entity = new Entity();
entity.setYmd(str);
entityMapper.insert(entity);
}
- 如果count不等于0,则说明数据库中已经存在与str字符串相等的ymd值,不需要添加新记录。
完整代码如下:
Wrapper<Entity> wrapper = new QueryWrapper<Entity>().eq("ymd", str);
int count = entityMapper.selectCount(wrapper);
if(count == 0){
Entity entity = new Entity();
entity.setYmd(str);
entityMapper.insert(entity);
}
原文地址: https://www.cveoy.top/t/topic/bfBe 著作权归作者所有。请勿转载和采集!