好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Mybatis日期格式自动转换需要用到的两个注解说明

Mybatis日期格式自动转换

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

public class Student {

     public Integer id;

     public String studentName;

     @JsonFormat (pattern= "yyyy-MM-dd" ) //从数据库读出日期格式时,进行转换的规则

     @DateTimeFormat (pattern = "yyyy-MM-dd" ) //接受从前端传入的日期格式,映射到java类日期属性的规则

     public Date birth;   

     public Integer getId() {

         return id;

     }

     public void setId(Integer id) {

         this .id = id;

     }

     public String getStudentName() {

         return studentName;

     }

     public void setStudentName(String studentName) {

         this .studentName = studentName;

     }

     public Date getBirth() {

         return birth;

     }

     public void setBirth(Date birth) {

         this .birth = birth;

     }

}

mybatis注解开发的时间类型处理小记

对时间类型的操作

- select操作

?

1

@ Select ( "select date_format(createTime,'%Y-%m-%d') from tb where id = ?" )

- update操作

更新为当前时间

?

1

@ Update ( "update tb set createTime = date_format(now(),'%Y-%m-%d %H:%i:%s') where id = ?" )

更新为指定时间

?

1

2

3

4

5

6

7

8

/**

*jdbcType = DATE 时,只传入了年月日

*/

@ Update ( "update tb set createTime = #{param, jdbcType=DATE} where id = ?" )

/**

*jdbcType = TIMESTAMP ,年月日+ 时分秒

*/

@ Update ( "update tb set createTime = #{param, jdbcType=TIMESTAMP } where id = ?" )

关于MySQL Date 函数的用法 参考这里

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/weixin_43954217/article/details/103126031

查看更多关于Mybatis日期格式自动转换需要用到的两个注解说明的详细内容...

  阅读:29次