@Log4j:注解在類上;為類提供一個 屬性名為log 的 log4j 日志對像
package com.atguigu.springcloud.controller; import com.atguigu.springcloud.entities.CommonResult; import com.atguigu.springcloud.entities.Payment; import com.atguigu.springcloud.service.PaymentService; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController @Slf4j public class PaymentController { @Resource private PaymentService paymentService; @PostMapping(value = "/payment/create") public CommonResult create(Payment payment){ int result = paymentService.create(payment); log.info("*****插入結果:"+result); //這里就是使用了Log這個對象 if (result>0){ //成功 return new CommonResult(200,"插入數據庫成功",result); }else { return new CommonResult(444,"插入數據庫失敗",null); } } @GetMapping(value = "/payment/get/{id}") public CommonResult getPaymentById(@PathVariable("id") Long id){ Payment payment = paymentService.getPaymentById(id); log.info("*****查詢結果:"+payment); if (payment!=null){ //說明有數據,能查詢成功 return new CommonResult(200,"查詢成功",payment); }else { return new CommonResult(444,"沒有對應記錄,查詢ID:"+id,null); } } }