欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring cloud之支付模块业务编写03

程序员文章站 2022-05-23 15:46:53
...

spring cloud之支付模块业务编写03

前言

  根据我们上一步已经将环境搭建完毕开始编写业务逻辑主要完成的业务有根据编号查询订单号和插入一个订单

业务编写步骤

创建订单的实体类和返回给前端的json数据封装类(entities包下)

Payment订单实体类

 

  package com.atguigu.entities;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    import java.io.Serializable;
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class Payment implements Serializable {
        private Long id ;
        private String serial;
    
    }


    

CommonResult json的封装类

 

  package com.atguigu.entities;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class CommonResult<T> {
        private Integer code;
        private String message;
        private  T data;
    
        public CommonResult(Integer code, String message) {
    //      this(code,message,null);
        }
    }


    

编写持久层的dao接口并且提供俩个方法

PaymentDao

 

   package com.atguigu.dao;
    
    import com.atguigu.entities.Payment;
    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Param;
    @Mapper
    public interface PaymentDao {
        public int create(Payment payment);
    
        public Payment getPaymentById(@Param("id") Long id);
    }


    

并且在mapper包下提供相应的映射文件PaymentMapper.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.atguigu.dao.PaymentDao">
        <insert id="create" parameterType="Payment" useGeneratedKeys="true" keyProperty="id">
            insert into payment(serial) values (#{serial});
        </insert>
        <resultMap id="BaseResultMap" type="com.atguigu.entities.Payment">
            <id column="id" property="id" jdbcType="BIGINT"/>
            <id column="serial" property="serial" jdbcType="VARCHAR"/>
        </resultMap>
        <select id="getPaymentById" parameterType="Long" resultMap="BaseResultMap">
            select  * from payment where id=#{id};
        </select>
    </mapper>

编写业务层PaymentService接口

 

   package com.atguigu.service;
    
    import com.atguigu.entities.Payment;
    import org.apache.ibatis.annotations.Param;
    
    
    public interface PaymentService {
    
        public int create(Payment payment);
    
        public Payment getPaymentById(@Param("id") Long id);
    }
    

给接口提供业务层的实现类PaymentServiceImpl

   

package com.atguigu.service.impl;
    
    import com.atguigu.dao.PaymentDao;
    import com.atguigu.entities.Payment;
    import com.atguigu.service.PaymentService;
    import org.springframework.stereotype.Service;
    
    import javax.annotation.Resource;
    
    @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);
        }
    }


    

最后编写控制器来进行执行业务操作

PaymentController

 

  package com.atguigu.cotroller;
    
    import com.atguigu.entities.CommonResult;
    import com.atguigu.entities.Payment;
    import com.atguigu.service.PaymentService;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.*;
    
    import javax.annotation.Resource;
    
    import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;
    
    @RestController
    @Slf4j
    public class PaymentController {
    @Resource
        private PaymentService paymentService;
    @GetMapping("/payment/create/")
     public CommonResult create(Payment payment){
         int result = paymentService.create(payment);
         if (result>0){
             return new CommonResult(200,"插入数据成功",result);
         }else {
             return new CommonResult(444,"插入数据失败",null);
    
         }
     }
        @GetMapping("/payment/get/{id}")
        public CommonResult getPaymentById(@PathVariable("id") Long id){
            Payment payment = paymentService.getPaymentById(id);
            if (payment!=null){
                return new CommonResult(200,"查询成功",payment);
            }else {
                return new CommonResult(444,"没有查询记录"+id,null);
    
            }
        }
    }


    

总结

  因为java开发尽量是只做后台,返回前端json数据所以这里并没有去写前端的界面,成功会将json数据返回到界面,就没有使用POST请求.