专业编程基础技术教程

网站首页 > 基础教程 正文

数据库实体类日期属性直接用jdk8新的日期类型-LocalDateTime

ccvgpt 2024-10-23 09:26:49 基础教程 7 ℃

简述

JDK8新特性之一,新增了几个日期对象,并将原来的Date类的许多方法置为过期,并不再建议使用Date;新增的日期对象分别为日期时间LocalDateTime、日期LocalDate、时间LocalTime、Duration、Period、Instant,今天就来实践一下前三个,直接作为PO的属性类型来操作数据表。

实践

新建实体类UserPO.java

数据库实体类日期属性直接用jdk8新的日期类型-LocalDateTime

@TableName(value = "user")
@Data
public class UserPO {
      @TableId
      private Integer id;
      @TableField(value = "username")
      private String username3;
      private String username2;
      private BigDecimal age;
      @TableField(value = "updateTime")
      private Date updateTime;
      private LocalDateTime updateDateTime;
      private LocalDate updateDate;
      private LocalTime dateTime;
}

为简单化,直接使用mybatis写了个mapper方法:

<mapper namespace="com.demo.ypk.mybatisplus.dao.UserMapper">
    <select id="getAll" resultType="com.demo.ypk.mybatisplus.entity.pojo.UserPO">
          SELECT id,username username3,age,updateTime,
          updateTime updateDateTime,updateTime updateDate,updateTime dateTime
          FROM user where age>#{age} and username=#{username}
    </select>
</mapper>

单元测试走一波:


没得问题,直接可映射

道友,要是文章还不错可以点赞收藏。。。

最近发表
标签列表