Lombok的基本使用
引入相应的maven包
在这里插入代码片 <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
<scope>provided</scope>
</dependency>
@Data:作用于类上,是以下注解的集合:@ToString @EqualsAndHashCode @Getter @Setter @RequiredArgsConstructor
@NoArgsConstructor:生成无参构造器;
@AllArgsConstructor:生成全参构造器
响应给前端的数据封装类:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T> implements Serializable {
private int id;
private String message;
private T data;
}
Controller:使用示例:
@RestController//直接回应到页面json数据不经过视图解析器
@Slf4j
public class PaymentController {
@Autowired
private PaymentService service;
@PostMapping(value = "/payment/create")
//@requestBody可以将请求体中的JSON字符串绑定到相应的bean上
public CommonResult create(@RequestBody Payment payment){
int result = service.create(payment);
log.info("****插入结果:" + result);
if(result > 0){
return new CommonResult(200, "插入数据库成功", result);
}
return new CommonResult(444, "插入数据库失败", null);
}
@GetMapping(value = "/payment/{id}")
//通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过@PathVariable(“xxx“) 绑定到操作方法的入参中。
public CommonResult getPaymentById(@PathVariable("id")Long id){
Payment result = service.getPaymentById(id);
log.info("****查询结果:" + result);
if(result != null){
return new CommonResult(200, "查询成功", result);
}
return new CommonResult(444, "没有对应id的记录", null);
}
}
自定义了快速生成Springboot启动类的快捷键(psvmb):
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class, args);
}
操作数据库顺序:(约束>配置>代码 先弄好环境,再弄好配置,再写代码)