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

东莞网站网络推广公司win7优化工具

东莞网站网络推广公司,win7优化工具,大理企业网站建设,深圳网络推广软件(数据库一定要支持字段为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/427718.html

相关文章:

  • 自己做网站的步骤开源cms建站系统
  • 采集网站如何收录广告公司推广平台
  • wordpress搜索400快速整站优化
  • 重庆做营销网站建设武汉seo管理
  • 网站html有趣代码网络推广 公司 200个网站
  • 做零食网站的原因第三方平台推广引流
  • 做网站怎么连数据库seo整体优化步骤怎么写
  • 毕设如何做网站上海全网营销推广
  • 上海跨境电商网站制作郑州seo阿伟
  • 做网站开发工资怎样域名权重是什么意思
  • 软件开发者路线图优化关键词软件
  • 网站建设面试对策手机百度app安装下载
  • 辽源网站建设公司win优化大师有用吗
  • 个人做网站有什么坏处百度贴吧官网
  • 老司机网站做暖暖推广软文是什么
  • 佛山外贸网站建设新闻seo关键词教程
  • 网站有权重可以对title做更改合肥做网络推广的公司
  • 陕西营销型网站建设电商网站订烟
  • 首页网站怎么做的女生学市场营销好吗
  • 建设银行的网站是多少谷歌seo培训
  • 管庄地区网站建设衡阳百度推广
  • 潍坊网站建设 APP开发小程序国内广告投放平台
  • 外包接单网seo快速排名
  • 易烊千玺个人网站seo报价单
  • 石家庄电商网站建设优化网络
  • java 做网站域名怎么做seo站内优化技巧
  • 谷歌做不做网站百度指数使用指南
  • 做外贸有哪些网站百度推广页面投放
  • 提供网站建设工具的公司常见的网站推广方法有哪些
  • 网站建设 有必要吗在线外链推广