Springboot的小技巧(一)

51 阅读1分钟

1.小技巧:lombok的应用:在编译阶段,为实体类自动生成setter getter toString方法

使用步骤:

1. pom文件导入依赖

image.png

2. 在实体类上添加注解

image.png

  1. 有参构造方法和无参构造方法的注解:
  • @NoArgsConstructor 无参构造方法
  • @AllArgsConstructor 有参构造方法
  1. springboot的3个层次:
  • Controller
  • Mapper(接口,但不实现)
  • Service(是接口,所以有实现类impl)
  1. @Autowired 的作用是什么?
    @Autowired 是一个注释,它可以对类成员变量、方法及构造函数进行标注,让 spring 完成 bean 自动 装配的工作。
    @Autowired 默认是按照类去匹配,配合 @Qualifier指定按照名称去装配 bean。

使用的场景:

  1. 成员属性字段使用 @Autowired,无需字段的 set 方法
  2. set 方法使用 @Autowired
  3. 构造方法使用 @Autowired

重点错误:

image.png

修改方法:加@Date

所有pojo都需要有@Date