在网上收集了一些资料,整理成自己项目中使用的内容,由于参考的人数众多,不能一一设置转载,如有侵权请联系删除,谢谢
mybatis plus 创建、更新时间戳
示例
数据库添加字段
create_time:类型为timestamp,默认值current_timestamp
update_time:类型为timestamp,默认值为current_timestamp,且设为自动更新
pojo 层
User
@Data
public class User extends Model<User> {
private static final long serialVersionUID=1L;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
private String name;
private Integer age;
private Sex sex;
private LocalDateTime createTime;
private LocalDateTime updateTime;
}
LocalDateTime 在前端显示会带T,全局去除 T的方法
1引入依赖
<!-- 解决 LocalDateTime 转为字符串后中间含“T“ -->
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.9.8</version>
</dependency>
2.创建config
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Configuration
public class LocalDateTimeConfig {
@Bean
public LocalDateTimeSerializer localDateTimeDeserializer() {
return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
@Bean
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeDeserializer());
}
}