Mybatis-Plus在Oracle中LambdaUpdateWrapper无法更新字段为null值,报错无效的列类型:1111

在使用Mybatis-Plus3.5.9版本的LambdaUpdateWrapper设置null值时,遇到Oracle数据库报错,原因是JdbcType为Other,不被支持。而在Mysql数据库中此操作正常。解决方案包括修改全局配置为field-strategy:IGNORE或针对特定字段设置jdbcType。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

现象

使用mybatis-plus 3.5.9版本中LambdaUpdateWrapper set(R column, Object value),当value为null时无法更新

报错如下

Caused by: org.apache.ibatis.type.TypeException: 
Could not set parameters for mapping:
ParameterMapping{property='ew.paramNameValuePairs.MPGENVAL4', mode=IN, javaType=class java.lang.Object, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. 
Cause: org.apache.ibatis.type.TypeException: 
Error setting null for parameter #4 with JdbcType OTHER . 
Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property.
Cause: java.sql.SQLException: 无效的列类型: 1111

上述是Oracle报的错,Mysql下却能正常更新
在这里插入图片描述
在这里插入图片描述

问题分析

org.apache.ibatis.type.BaseTypeHandler类中方法

 @Override
  public void setParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException {
    if (parameter == null) {
      if (jdbcType == null) {
        throw new TypeException("JDBC requires that the JdbcType must be specified for all nullable parameters.");
      }
      try {
        ps.setNull(i, jdbcType.TYPE_CODE);
      } catch (SQLException e) {
        throw new TypeException("Error setting null for parameter #" + i + " with JdbcType " + jdbcType + " . "
              + "Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. "
              + "Cause: " + e, e);
      }
    } else {
      try {
        setNonNullParameter(ps, i, parameter, jdbcType);
      } catch (Exception e) {
        throw new TypeException("Error setting non null for parameter #" + i + " with JdbcType " + jdbcType + " . "
              + "Try setting a different JdbcType for this parameter or a different configuration property. "
              + "Cause: " + e, e);
      }
    }
  }

ps.setNull(i, jdbcType.TYPE_CODE);jdbcTypeOther

public enum JdbcType {
......
  OTHER(Types.OTHER),
......

}

在这里插入图片描述
报错信息即为无效类型:1111

解决方案

  1. 使用MySQL数据库
    这种有点不太现实,但自己研究还是可以的
    目前支持Other的数据库有PostgreSQLMySQL,SQLServer
  2. 目前Mybatis-Plus3.x版本以上支持LambdUpdate方式更新null字段
  3. 配置更新策略
    全局配置:
mybatis-plus:
	global-config:
		field-strategy: IGNORE 

使用注解

    FieldStrategy insertStrategy() default FieldStrategy.DEFAULT;

    /**
     * 字段验证策略之 update: 当更新操作时,该字段拼接set语句时的策略
     * <p>
     * IGNORED: 直接拼接 update table_a set column=#{columnProperty}, 属性为null/空string都会被set进去
     * NOT_NULL: update table_a set <if test="columnProperty != null">column=#{columnProperty}</if>
     * NOT_EMPTY: update table_a set <if test="columnProperty != null and columnProperty!=''">column=#{columnProperty}</if>
     * NOT_EMPTY 如果针对的是非 CharSequence 类型的字段则效果等于 NOT_NULL
     *
     * @since 3.1.2
     */
    FieldStrategy updateStrategy() default FieldStrategy.DEFAULT;

若只针对指定字段需要指定的jdbcType
4. 若字段为null直接赋值空串

### MyBatis-Plus 配置参数详解 #### 1. Configuration 参数说明 `configuration` 是 MyBatis 中的核心配置对象,默认为 `null`。它主要用于加载和管理 MyBatis 的全局配置项,这些配置可以来自 XML 文件或者通过代码动态设置[^1]。 在 MyBatis-Plus 中,`Configuration` 对象继承自 MyBatis 原生的功能,并扩展了一些额外的支持特性。开发者可以通过该对象调整 SQL 映射行为、缓存策略以及其他高级选项。 #### 2. PaginationInnerInterceptor 插件配置 为了实现分页查询功能,MyBatis-Plus 提供了一个名为 `PaginationInnerInterceptor` 的拦截器插件。此插件需要显式注册到 Spring Boot 或其他框架的应用上下文中才能生效[^4]。 以下是启用分页功能的典型代码示例: ```java import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MyBatisPlusConfig { @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } } ``` 上述代码片段展示了如何通过 Spring Bean 注册方式引入分页插件。 #### 3. 数据模型类中的常用注解 除了核心配置外,MyBatis-Plus 还提供了一系列便捷的注解来简化实体类定义过程。例如,Lombok 库中的 `@Data` 注解能够自动为类生成常见的 getter 和 setter 方法,从而减少样板代码编写工作量[^3]。 下面列举几个重要的注解及其作用: - **@TableName**: 指定数据库表名与实体类之间的映射关系。 - **@TableField**: 定义字段的具体属性,比如是否忽略某些列或指定特殊逻辑名称。 - **@Version**: 支持乐观锁机制下的版本号控制。 - **@KeySequence**: 主键生成策略之一,适用于 Oracle 等支持序列的数据源环境。 #### 4. 自动填充功能 借助于 `MetaObjectHandler` 接口以及相关注解(如 `@TableLogic`, `@InsertFill`, `@UpdateFill`),可以在不修改业务代码的前提下完成时间戳更新或其他默认注入操作。 以下是一个简单的 MetaObjectHandler 实现例子: ```java @Component public class MyMetaObjectHandler implements MetaObjectHandler { @Override public void insertFill(MetaObject metaObject) { this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now()); } @Override public void updateFill(MetaObject metaObject) { this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now()); } } ``` 以上方法会在每次新增记录时自动填写创建时间和最后修改时间为当前时刻点。 --- ### 总结 综上所述,MyBatis-Plus 不仅保留了原有 MyBatis 的全部能力,还增加了许多实用的新特性和优化手段。无论是基础 CRUD 操作还是复杂场景需求处理方面均表现出色。合理利用其内置组件可以帮助团队更高效地构建基于 Java 技术栈的企业级应用系统。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

游侠小马哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值