mybatis-plus 无法更新为null
时间: 2024-04-24 09:23:36 浏览: 112
你好!对于 MyBatis-Plus,当我们使用它进行更新操作时,默认情况下是不会更新为 NULL 的字段的。这是因为 MyBatis-Plus 的设计理念是只更新传入的非空字段,而不是更新所有字段。
如果你想要将某个字段更新为 NULL,可以通过设置字段值为 null 来实现。例如,如果你想将某个实体对象的某个属性设置为 NULL,可以将该属性设置为 null,并调用 MyBatis-Plus 提供的更新方法。
另外,如果你想要强制更新为 NULL,可以使用 MyBatis-Plus 提供的 `set` 方法来手动设置某个字段为 NULL。例如:
```java
UpdateWrapper<Entity> updateWrapper = new UpdateWrapper<>();
updateWrapper.set("column_name", null);
```
这样就可以将指定的字段更新为 NULL。
希望以上信息对你有帮助!如果你还有其他问题,请随时提问。
相关问题
mybatis-plus查询操作把null的字段转换为-1
MyBatis-Plus提供了一种配置方式,允许你在查询结果映射时将数据库返回的NULL值自动转换为特定的默认值,比如你想把所有NULL的字段转为-1,你可以通过全局配置文件(application.yml 或 application.properties)中的`mapper-config.xml`来进行设置。
在全局配置文件中,添加如下内容:
```yaml
mybatis-plus:
mapper-config:
global-config:
result-map-processor: org.apache.ibatis plus.config.GlobalConfiguration.defaultResultMapProcessor
default-result-map-processor-param:
typeHandlerNullValue: -1
```
或者
```properties
mybatis-plus.mapper-config.global-config.result-map-processor=org.apache.ibatis.plus.config.GlobalConfiguration.defaultResultMapProcessor
mybatis-plus.mapper-config.global-config.default-result-map-processor-param.typeHandlerNullValue=-1
```
这会告诉MyBatis-Plus,在处理结果映射时,如果遇到NULL值,就将其替换为-1。注意,这里的`typeHandlerNullValue`参数就是你要替换的默认值。
mybatis-plus-generator和mybatis-plus
mybatis-plus-generator和mybatis-plus是用于简化MyBatis开发的两个工具。mybatis-plus是一个MyBatis的增强工具包,提供了一些便捷的操作,节约了编写简单SQL的时间。而mybatis-plus-generator是一个代码生成器,可以自动生成一些基本的Controller、Service、Mapper和Mapper.xml文件。
通过整合mybatis-plus和mybatis-plus-generator,我们可以更高效地开发项目中的单表增删改查功能。使用mybatis-plus-generator可以自动生成一些基本的文件,例如Controller、Service、Mapper和Mapper.xml,极大地减少了手动创建这些文件的时间和工作量。而mybatis-plus提供的便捷操作可以节约编写简单SQL的时间。
然而,对于一些逻辑复杂、多表操作或动态SQL等情况,建议使用原生SQL来处理。mybatis-plus支持原生SQL的使用,通过写原生SQL可以更灵活地满足这些复杂需求。
综上所述,通过整合mybatis-plus和mybatis-plus-generator,我们可以在开发中更高效地处理单表的增删改查功能,并且对于复杂的需求可以使用原生SQL来满足。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [Spring cloud整合MyBatis-plus和mybatis-plus-generator](https://blog.csdn.net/cssweb_sh/article/details/123767029)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *3* [mybatis-plus-generator(mybatisplus代码生成器篇)](https://blog.csdn.net/b13001216978/article/details/121690960)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文
相关推荐
















