首页 > 编程语言 >Java:数据表的字段设计了默认值0不生效的原因

Java:数据表的字段设计了默认值0不生效的原因

时间:2023-03-22 19:56:22浏览次数:39  
标签:NULL Java private 数据表 plus userEntity mybatis 默认值

在数据表里给字段设置了默认值为0,但是在插入的时候不生效,数据表设计如下

 

通过数据表生成的实体类

查看代码
@Data
@TableName(value = "user")
@NoArgsConstructor
@AllArgsConstructor
public class UserEntity{

    @TableId
    @ApiModelProperty("Id")
    private Integer id;

    @ApiModelProperty("用户名")
    private String username;

    @ApiModelProperty("密码")
    private String password;

    @ApiModelProperty("性别")
    private Integer gender;

    @ApiModelProperty("创建时间")
    private LocalDateTime createTime;

    private Integer deleted;
}

 

使用mybatis-plus将数据插入到数据库中

查看代码
UserEntity userEntity = new UserEntity();
        userEntity.setId(5);
        userEntity.setGender(0);
        userEntity.setUsername("username");
        userEntity.setPassword("123214");

        LocalDateTime dateTime = LocalDateTime.now();
        userEntity.setCreateTime(dateTime);

        userService.save(userEntity);

 

 使用mybatis-plus的方法进行插入后,发现deleted的值是NULL,而不是已经设置好的默认值0

 

 通过日志查看mybatis-plus的插入SQL语句,发现SQL语句会将deleted赋值为NULL,所以数据库的默认值就不生效了

 

查看了mybatis-plus的官方文档,发现是不小心改了配置文件,导致生成的SQL语句会将NULL值插入到数据库中

原因:mybatis-plus的插入策略写成了ignored,在生成SQL语句时,就不会判断插入数据库的值是否为NULL

解决:将这段配置删掉,按照mybatis-plus默认的配置,会进行判断是否为NULL,就可以屏蔽NULL值,不会插入到数据库,数据表设置的默认值就生效了

 

标签:NULL,Java,private,数据表,plus,userEntity,mybatis,默认值
From: https://www.cnblogs.com/davidFB/p/17245097.html

相关文章

  • Java单例
    /***@Author:DengJia*@Date:2023/3/22*@Description:单例*/publicclassSingleton{publicstaticvoidmain(String[]args){System.out......
  • Java学习笔记(八)GUI
    GUI编程如何学习?这是什么?它怎么玩?该如何去平时运用?组件窗口弹窗面板文本框列表框按钮图片监听事件鼠标键盘破解工具1.简介Gui的核心技术:SwingAWT,......
  • Java 执行命令 Apache Common-Exec
    command="cmd.exe/c"+command;ByteArrayOutputStreamstdout=newByteArrayOutputStream();PumpStreamHandlerpsh=newPumpStreamHandler(stdout);CommandLinecl......
  • Java 全文搜索框架 Lucene
    Lucene是apache软件基金会一个开放源代码的全文检索引擎工具包,是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎,部分文本分析引擎。Lucene的目的是为软件开发人员......
  • JavaScript 通讯框架 NowJS
    NowJS是一个实现了JavaScript服务器端进行通讯的框架,基于​​Node.js​​开发。示例代码:服务器端:<scriptsrc="/nowjs/now.js"></script>vareveryone=require("now"......
  • Java调用标签打印机打印标签
    标签打印机:TSC TTP-244Pro打印机驱动:file:///C:/Users/admin/Downloads/tsc_2022.1_m-2.exe_1.zip官方文档预览下载: https://fs.chinatsc.cn/system/files/tspl_tspl......
  • elasticsearch RESTful搜索引擎-(java jest 使用[入门])
     elasticsearch简称ESjest好吧下面我介绍下jest(第三方工具)它是ES的java客户端,基于httprestful...jest是开源的 首先看看项目的目录结构我一般习惯了用maven去管理我的......
  • Could not read JSON: Can not deserialize instance of java.lang.Integer out of ST
    Spring会将{id:id}这个json转换成Map对象,只要将@requestBody中的参数改成Map就可以了,如下@RequestMapping(value="/delete",method=RequestMethod.POST,produces="applicat......
  • rapid-framework(java web快速开发脚手架,代码生成器)
    web项目脚手架rapid-framework是一个以spring为核心的项目脚手架(或者称为胶水框架),框架将各个零散的框架(struts,strust2,springmvc,hibernate,ibatis,spring_jdbc,flex)搭......
  • java中数组删除
    ListcommonList=newArrayList(); if(commonList.size()>3)//如果数组个数大于3个,则删掉后面的,只剩余前三个{for(inti=3;i<commonList.size();i......