Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to update the field to null by mybatis plus

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains how to update the field to null by mybatis plus. Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to update the field to null by mybatis plus"!

Mybatis plus updates the field to null. By default, you cannot update the field to null using the update method that comes with mybatis plus without any processing. If you want to update the field to null, you need to do the following

Annotate the required fields with @ TableField and set the properties

UpdateStrategy = FieldStrategy.IGNORED

The test code is the following database table

Entity class package com.sbmp.bean;import java.time.LocalDate;import com.baomidou.mybatisplus.annotation.IdType;import com.baomidou.mybatisplus.annotation.TableId;import lombok.Data;@Datapublic class Userinfo {/ * primary key id * / @ TableId (type = IdType.AUTO) private Integer id; / * * | * name * / private String name / * * Birthday * / private LocalDate birthday;} mapperpackage com.sbmp.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;import com.sbmp.bean.Userinfo;public interface UserinfoMapper extends BaseMapper {} controllerpackage com.sbmp.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import com.sbmp.bean.Userinfo;import com.sbmp.mapper.UserinfoMapper @ RestControllerpublic class UserinfoController {@ Autowired private UserinfoMapper userinfoMapper; @ RequestMapping ("/ info") public String info () {Userinfo info = new Userinfo (); info.setId (1); info.setBirthday (null); info.setName ("Xue Baochai"); userinfoMapper.updateById (info) Return "successful";}} run the test

See if Xue Baochai's birthday can be updated to empty.

Look, the printed sql does not have the splicing of birthday = null. This is the default.

If you want to concatenate birthday = null, you need to add this configuration to the birthday field

@ TableField (updateStrategy = FieldStrategy.IGNORED)

The modified code is as follows

Package com.sbmp.bean;import java.time.LocalDate;import com.baomidou.mybatisplus.annotation.FieldStrategy;import com.baomidou.mybatisplus.annotation.IdType;import com.baomidou.mybatisplus.annotation.TableField;import com.baomidou.mybatisplus.annotation.TableId;import lombok.Data;@Datapublic class Userinfo {/ * primary key id * / @ TableId (type = IdType.AUTO) private Integer id / * * | * name * / private String name; / * * birthday * / @ TableField (updateStrategy = FieldStrategy.IGNORED) private LocalDate birthday;} run the test

Birthday = null updated successfully this time

@ TableField partial attribute description

Look at the official document updateStrategy for handling updates; similarly, use insertStrategy when adding

Involving where conditional processing using whereStrategy

FieldStrategy description

At this point, I believe you have a deeper understanding of "how mybatis plus updates the field to null". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report