1. @TableId注释
当使用mybatis plus 进行插入时,使用Service层自带的save方法进行插入,他的参数只能是entity实体类,而我们数据库中的id主键是自动递增的我们不需要插入值,直接执行此种方法时会出现
可以看到,id直线上升十分离谱,查阅资料是因为mybatisplus根据雪花算法给我们生成了id,但这明显不是我们想要的,所以就用到了**@TableId** 注释
他的内部实现
其中
AUTO:就是利用数据库自增
NONE:未设置,需要手动设置值
INPUT: 需要手动设置值
ASSIGN_ID : 雪花算法
ASSIGN_UUID :指定主键生成策略为不含中划线的UUID
2.时间格式化
当我们想前台向后台传入时间格式参数时,直接传参可能会报
Field error in object 'user' on field 'admissionDate': rejected value [2021-1-1 23:33:34]; codes [typeMismatch.user.admissionDate,typeMismatch.admissionDate,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.admissionDate,admissionDate]; arguments []; default message [admissionDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'admissionDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2021-1-1 23:33:34'; nested exception is java.lang.IllegalArgumentException]]
参数不匹配的情况,此时我们可以在实体类的字段中加上
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
的注解,来格式化出参和入参。