当前位置: 首页 > wzjs >正文

石家庄新闻综合频道节目表百度seo插件

石家庄新闻综合频道节目表,百度seo插件,新建网站,便宜的广州网站建设服务(数据库一定要支持字段为null值) 问题: 假设我现在数据库里有一个值是1,这个字段允许为null。 目前我使用的是的mybaties plus,我希望将这个值更新weinull,如何操作? 提示:如果直接…

(数据库一定要支持字段为null值)

问题:

假设我现在数据库里有一个值是1,这个字段允许为null。 目前我使用的是的mybaties plus,我希望将这个值更新weinull,如何操作? 提示:如果直接给代码更新语句set一个null值进去,系统会提示更新成功,但是实际上因为set的是null值所以并不会更新成功

今日被人问到的e

在使用 MyBatis-Plus 时,如果你希望将数据库中的某个字段更新为 NULL,但发现直接设置为 null 后并没有生效,这通常是因为 MyBatis-Plus 默认的更新策略会忽略值为 null 的字段。也就是说,默认情况下,MyBatis-Plus 不会将字段更新为 NULL

为了解决这个问题,你可以通过以下几种方式来实现将字段更新为 NULL


方法 1:使用 @TableField 注解修改更新策略

在实体类中,可以为需要更新为 NULL 的字段添加 @TableField 注解,并指定 updateStrategy = FieldStrategy.IGNORED。这样,即使字段值为 null,也会被包含在更新语句中。

示例代码:
java深色版本import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;@TableName("your_table_name")
public class YourEntity {private Long id; // 主键@TableField(updateStrategy = FieldStrategy.IGNORED)private String yourNullableField; // 允许为 NULL 的字段// Getter 和 Setter 方法public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getYourNullableField() {return yourNullableField;}public void setYourNullableField(String yourNullableField) {this.yourNullableField = yourNullableField;}
}
更新逻辑:
java深色版本import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;@Service
public class YourEntityService extends ServiceImpl<YourEntityMapper, YourEntity> {public void updateFieldToNull(Long id) {YourEntity entity = new YourEntity();entity.setId(id); // 设置主键entity.setYourNullableField(null); // 将字段设为 null// 调用 MyBatis-Plus 的 updateById 方法this.updateById(entity);}
}

此时,更新语句会生成如下 SQL:

sql深色版本UPDATE your_table_name SET your_nullable_field = NULL WHERE id = ?;

方法 2:使用 UpdateWrapper 显式设置字段为 NULL

如果不想修改实体类的注解配置,可以通过 UpdateWrapper 来显式地将字段设置为 NULL

示例代码:
java深色版本import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.stereotype.Service;@Service
public class YourEntityService extends ServiceImpl<YourEntityMapper, YourEntity> {public void updateFieldToNull(Long id) {UpdateWrapper<YourEntity> updateWrapper = new UpdateWrapper<>();updateWrapper.eq("id", id) // 指定更新条件.set("your_nullable_field", null); // 将字段设为 null// 执行更新操作this.update(updateWrapper);}
}

此时,生成的 SQL 语句类似于:

sql深色版本UPDATE your_table_name SET your_nullable_field = NULL WHERE id = ?;

方法 3:全局配置更新策略

如果你希望在整个项目中都允许字段更新为 NULL,可以在 MyBatis-Plus 的全局配置中修改默认的更新策略。

修改全局配置:

application.ymlapplication.properties 文件中添加以下配置:

yaml深色版本mybatis-plus:global-config:db-config:update-strategy: ignored # 全局更新策略:忽略 null 值检查

或者通过 Java 配置类设置:

java深色版本import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class MyBatisPlusConfig {@Beanpublic SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();factoryBean.setDataSource(dataSource);GlobalConfig globalConfig = new GlobalConfig();globalConfig.setDbConfig(new GlobalConfig.DbConfig().setUpdateStrategy(com.baomidou.mybatisplus.annotation.FieldStrategy.IGNORED));factoryBean.setGlobalConfig(globalConfig);return factoryBean.getObject();}
}

这样,所有字段都会遵循 IGNORED 策略,即使字段值为 null,也会被包含在更新语句中。

http://www.dtcms.com/wzjs/591127.html

相关文章:

  • 南通优化网站公司哪家好pc网站开发语言
  • discuz论坛和网站同步登录凌风wordpress百度云
  • 网页设计与网站建设基础wordpress 模板 国外
  • 免费网站空间城市建设管理网站
  • 社交媒体营销seo综合排名优化
  • 有没有专业做二手老车的网站正规推广赚佣金的平台
  • 怎么看一个网站用什么做的营销的手段和方法
  • 开发网站建设有网站的源代码怎么生成网站
  • 网站开发+语音网站建设公司接单
  • 新手做自己的网站教程中国建设银行网站首页下载
  • 甘肃省建设局网站360百度网站怎么做
  • flashfxp 网站廊坊市固安县建设局网站
  • 最新网站发布win主机怎样实现wordpress固定链接静态化
  • 网站代备案需要多少钱阿里云服务器建立网站吗
  • 网站建设移动网络用织梦做的网站是模板的吗
  • wordpress post_type网站优化对企业有什么好处
  • 营销类网站有哪些长春企业网站设计
  • 我的企业网站怎么seo网站开发公司分析
  • 展示型网站系统中山网官网
  • 最专业的做网站公司有哪些百度网站app下载
  • 深圳专业做网站技术唐山快速建站的公司
  • 上海网站建设高端遵义网帮你分类信息网
  • 怎么检查网站死链创建有限公司
  • 做网站需要购买什么深圳网站优化推广
  • 织梦茶叶网站模板特色化示范性软件学院
  • 云南网站建设找三好科技公司网站域名及空间
  • 局网站建设情况汇报哪些购物网站做的比较简洁有品质
  • 什么网站可以在线做雅思制作商城网站公司
  • 网站开发给网站设置图标在什么文件中写代码怎样做网络推广话术
  • 新网站怎么做才会被收录网站开发 报价单 表格