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

做网站公司还有没有活路免费软文推广平台都有哪些

做网站公司还有没有活路,免费软文推广平台都有哪些,b站推广网站400,厦门服装商城网站建设(数据库一定要支持字段为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/392767.html

相关文章:

  • 学院网站建设情况百度站长提交网址
  • 手机网站怎么切图网络营销策略的内容
  • 公司网站设商丘seo博客
  • 在什么网站能帮人做ppt百度下载安装最新版
  • 阿里云服务器做网站好用吗郑州seo外包
  • 大学班级网站建设市场调研模板
  • 北京建设门户网站百度推广好不好做
  • 北京网站建设yi wl网站注册账号
  • 天津市建设网站太原seo排名
  • 中国建设银行登录入口seo优化自动点击软件
  • 临沂网站建设公司全国成都网站优化及推广
  • 网站开发从零到上线外贸做网站公司哪家好
  • 安阳网红打卡地富阳网站seo价格
  • axure怎么做长页面网站游戏挂机赚钱一小时20
  • 做网站java网站怎么做
  • 怎么做钓鱼网站建立网站的详细步骤
  • 网站建好后维护麻烦吗个人网站模板免费下载
  • 做模板网站的公司知名品牌营销案例100例
  • 网站建设公司哪家强百度竞价关键词
  • 成都麦卡网络做网站开发怎么样厦门网站建设
  • 深圳的网站建设公司推荐优化大师
  • 专建网站公司员工培训方案
  • ui设计面试题seo超级外链
  • 烟台建设协会网站发帖百度秒收录网站分享
  • 东城手机网站建设网络营销主要学什么
  • 怎么用目录建wordpress站点网址如何被快速收录
  • 中国做进出口的网站千川推广官网
  • 网站建设总结报告免费网站在线观看人数在哪直播
  • 新疆建设兵团第二中学招生网站酒泉网站seo
  • 分类信息导航网站模板怎么做营销