好得很程序员自学网

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

springboot mybatis里localdatetime序列化问题的解决

问题起因

主要是使用mybatis作为orm之后,返回的对象为map,然后对于数据库的datetime,datestamp类型返回为时间戳而不是标准的时间,这个问题解决方案有两种,大叔分析一下:

1.在mapper的select里,使用mysql这些数据库的函数,dateformat进行转化,缺点,单元测试里使用h2数据库时会找不到这些函数

2.在objectmapper反序列化时统一进行处理,这种方式更好,与具体数据库解耦了

实现

>引用依赖包

?

1

2

'org.mybatis:mybatis-typehandlers-jsr310:1.0.2' ,

'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.9.2'

>添加组件类

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

/**

  * 序列化localdatetime处理.

  */

@component

public class jacksonconfig {

 

  /**

  * 注入时间处理.

  *

  * @return

  */

  @bean

  @primary

  public objectmapper objectmapper() {

  objectmapper mapper = new objectmapper();

  mapper.registermodule( new jsr310module());

  mapper.setdateformat( new simpledateformat( "yyyy-mm-dd't'hh:mm:ss.sss'z'" ));

  return mapper;

  }

 

}

>成功解决问题

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

{

  "pagecurrent" : 1 ,

  "pagesize" : 10 ,

  "pagetotal" : 1 ,

  "data" : [

   {

    "freedays" : 8 ,

    "city" : "" ,

    "leadingperson" : "" ,

    "contactperson" : "zhangsan" ,

    "source" : 1 ,

    "customername" : "i-counting" ,

    "intention" : 1 ,

    "province" : "" ,

    "appointmenttime" : "2018-09-20t0.000z" ,

    "createtime" : "2018-09-27t06:33:49.000z" ,

    "telephonestatus" : 1 ,

    "id" : 10000 ,

    "contactphone" : "135"

   }

  ]

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:http://www.cnblogs.com/lori/p/9755985.html

查看更多关于springboot mybatis里localdatetime序列化问题的解决的详细内容...

  阅读:12次