Spring Boot学习|问题汇总(二)

本文汇总了Spring Boot开发中遇到的问题,包括如何获取当前日期和时间、DefaultSerializer的序列化错误、hibernate_sequence表不存在、IDEA调试时的性能影响及主键无默认值的解决方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题预览

  • 1)java如何获取当前日期和时间
  • 2)DefaultSerializer requires a Serializable payload but received an object of type [XX]
  • 3)Table ‘XXX.hibernate_sequence’ doesn’t exist
  • 4)使用idea在DEBUG的时候出现Method breakpoints may dramatically slow down debugging
  • 5)Field ‘Id’ doesn’t have a default value解决方法

1.java如何获取当前日期和时间

  • 获取标准时间可以通过System.currentTimeMillis()方法获取,此方法不受时区影响,得到的结果是时间戳格式的,然后我们将其转化成我们易于理解的格式

    SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
    Date date = new Date(System.currentTimeMillis());
    System.out.println(formatter.format(date));
    

    在Java中,获取当前日期最简单的方法之一就是直接实例化位于Java包java.util的Date类。

    java.util.Date
    Date date = new Date(); 
    

    上面获取到的日期也可以被format成我们需要的格式,例如:

    SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");  
    System.out.println(formatter.format(date));  
    

    Java 8提供了一个全新的API,包括:

    LocalDate LocalDate只是一个日期,没有时间。 这意味着我们只能获得当前日期,但没有一天的具体时间。

    LocalDate date = LocalDate.now(); 
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");  
    System.out.println(date.format(formatter));  
    

    得到的结果只有年月日

    LocalTime LocalTime与LocalDate相反,它只代表一个时间,没有日期。
    这意味着我们只能获得当天的当前时间,而不是实际日期:

    LocalTime time = LocalTime.now(); 
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");  
    System.out.println(time.format(formatter)); 
    

    LocalDateTime
    最后一个是LocalDateTime,也是Java中最常用的Date / Time类,代表前两个类的组合 - 即日期和时间的值:

    LocalDateTime dateTime = LocalDateTime.now();
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");  
    System.out.println(dateTime.format(formatter));
    

2.DefaultSerializer requires a Serializable payload but received an object of type [XX]

  • 要缓存的 Java 对象必须实现 Serializable 接口,因为 Spring 会将对象先序列化再存入 Redis ,将缓存实体类继承 Serializable
public class XX implements Serializable

3.Table ‘XXX.hibernate_sequence’ doesn’t exist

  • 将ID生成略组改成@GeneratedValue(strategy = GenerationType.IDENTITY).

4.使用idea在DEBUG的时候出现Method breakpoints may dramatically slow down debugging

  • 根据语义可能是断点打在方法上面了,导致在某个断点卡住了。

    打开Breakpoints面板看看,(快捷键:Ctrl - Shift -F8 ) 将前面选择框的"勾勾"去掉点击Done即可。

5.Field ‘Id’ doesn’t have a default value解决方法

  • 检查数据表看看Mysql中有没有将主键设置为自增,所以在使用Mybatis时获取生成主键时出现异常。
    可以在数据库中设置主键自增
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值