springcloud微服务学习笔记(四):支付模块(下)

springcloud微服务学习笔记目录:
springcloud微服务学习笔记

1.建表
springcloud微服务学习笔记(四):支付模块(下)_第1张图片
2.entities
使用了lombok,可以在idea中安装lombok插件

/**
 * 实体类
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable {

    private Long id;

    private String serial;

}

/**
 * 返回数据
 * @param 
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult {

    private Integer code;

    private String message;

    private T data;

    private CommonResult(Integer code,String message){
       this(code,message,null);
    }
}

3.dao
3.1 mapper映射文件



   
       insert into payment(serial) values(#{serial});
   
    
    
    
        
        
    

    



3.2 dao方法

@Mapper
public interface PaymentDao {

    int create(Payment payment);

    Payment getPaymentById(@Param("id")Long id);
}

4.service
4.1 service接口

public interface PaymentService {

    int create(Payment payment);

    Payment getPaymentById(@Param("id")Long id);
}

4.2service实现类

@Service
public class PaymentServiceImpl implements PaymentService {

    @Resource
    private PaymentDao paymentDao;

    @Override
    public int create(Payment payment) {
        return paymentDao.create(payment);
    }

    @Override
    public Payment getPaymentById(Long id) {
        return paymentDao.getPaymentById(id);
    }
}

5.controller

@RestController
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Resource
    private DiscoveryClient discoveryClient;

    @Value("${server.port}")
    private String serverPort;

    @PostMapping("/payment/creat")
    public CommonResult create(@RequestBody Payment payment){

        int result=paymentService.create(payment);
        log.info("******插入结果"+result);
        if (result>0){
            return new CommonResult(200,"success",result);
        }else {
            return new CommonResult(400,"fail",null);
        }
    }

    @GetMapping("/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, "success,serverPort:"+serverPort, payment);
        }else {
            return new CommonResult(400,"fail",null);
        }

    }

    @GetMapping("/payment/discovery")
    public Object discovery(){
       List services=discoveryClient.getServices();
       for (String service:services){
           log.info("***********service:"+service);
       }
       List serviceInstances=discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
       for (ServiceInstance serviceInstance:serviceInstances){
           log.info(serviceInstance.getHost()+"\t"+serviceInstance.getInstanceId()+"\t"+serviceInstance.getPort()+"\t"+serviceInstance.getUri());
       }
       return this.discoveryClient;
    }
}

完成到这,可以启动主启动类测试一下

你可能感兴趣的:(springcloud微服务学习笔记(四):支付模块(下))