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

5.学习笔记-支付模块构建

程序员文章站 2022-03-15 21:04:39
...

学习笔记-支付模块构建

  1. youyangcloud-provider-payment8001 微服务提供者支付Module模块

    1. 创建youyangcloud-provider-payment8001

      创建maven,只填写模块名即可,无需其他操作

    2. 改pom

      <?xml version="1.0" encoding="UTF-8"?>
      <project xmlns="http://maven.apache.org/POM/4.0.0"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
          <parent>
              <artifactId>youyangcloud</artifactId>
              <groupId>com.youyang.springcloud</groupId>
              <version>1.0-SNAPSHOT</version>
          </parent>
          <modelVersion>4.0.0</modelVersion>
      
          <artifactId>youyangcloud-provider-payment8001</artifactId>
      
          <dependencies>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-web</artifactId>
              </dependency>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-actuator</artifactId>
              </dependency>
              <dependency>
                  <groupId>org.mybatis.spring.boot</groupId>
                  <artifactId>mybatis-spring-boot-starter</artifactId>
              </dependency>
              <dependency>
                  <groupId>com.alibaba</groupId>
                  <artifactId>druid-spring-boot-starter</artifactId>
                  <version>1.1.10</version>
              </dependency>
              <!--mysql-connector-java-->
              <dependency>
                  <groupId>mysql</groupId>
                  <artifactId>mysql-connector-java</artifactId>
              </dependency>
              <!--jdbc-->
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-jdbc</artifactId>
              </dependency>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-devtools</artifactId>
                  <scope>runtime</scope>
                  <optional>true</optional>
              </dependency>
              <dependency>
                  <groupId>org.projectlombok</groupId>
                  <artifactId>lombok</artifactId>
                  <optional>true</optional>
              </dependency>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-test</artifactId>
                  <scope>test</scope>
              </dependency>
          </dependencies>
      
      </project>
      
    3. 写YML

      server:
        port: 8001
      
      spring:
        application:
          name: youyangcloud-payment-service
        datasource:
          type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
          driver-class-name: org.gjt.mm.mysql.Driver              # mysql驱动包
          url: jdbc:mysql://192.168.25.157:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false
          username: root
          password: root
      
      mybatis:
          mapperLocations: classpath:mapper/*.xml
          type-aliases-package: com.youyang.springcloud.entities    # 所有Entity别名类所在包
      
    4. 主启动

      package com.youyang.springcloud;
      
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      
      /**
       * @Author: yangjian
       * @Data: 2021/04/04 - 11:43
       * @Description:
       * @Version: 1.0
       */
      @SpringBootApplication
      public class PaymentMain8001 {
          public static void main(String[] args) {
              SpringApplication.run(PaymentMain8001.class,args);
          }
      }
      
      
    5. 业务类

        1. 建表SQL

          CREATE TABLE `payment` (
          	`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
          	`serial` varchar(200) DEFAULT '',
          	PRIMARY KEY (`id`)
          ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
          
          
        2. entities

          com.youyang.springcloud.entities.Payment

          package com.youyang.springcloud.entities;
          
          import lombok.AllArgsConstructor;
          import lombok.Data;
          import lombok.NoArgsConstructor;
          
          import java.io.Serializable;
          
          /**
           * @Author: yangjian
           * @Data: 2021/04/04 - 12:01
           * @Description: 支付实体类
           * @Version: 1.0
           */
          @Data
          @AllArgsConstructor
          @NoArgsConstructor
          public class Payment implements Serializable {
              private long id;
              private String serial;
          }
          
          

          JSON 封装体 CommonResult

          package com.youyang.springcloud.entities;
          
          import lombok.AllArgsConstructor;
          import lombok.Data;
          import lombok.NoArgsConstructor;
          
          import java.io.Serializable;
          import java.sql.PreparedStatement;
          
          /**
           * @Author: yangjian
           * @Data: 2021/04/04 - 12:01
           * @Description:  JSON 封装体
           * @Version: 1.0
           */
          @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);
              }
          }
          
          
        3. dao

          com.youyang.springcloud.dao.PaymentDao

          package com.youyang.springcloud.dao;
          
          import com.youyang.springcloud.entities.Payment;
          import org.apache.ibatis.annotations.Mapper;
          import org.apache.ibatis.annotations.Param;
          
          /**
           * @Author: yangjian
           * @Data: 2021/04/04 - 12:08
           * @Description: 使用mapper 不用Respository(插入可能会有问题)
           * @Version: 1.0
           */
          @Mapper
          public interface PaymentDao {
              public int create(Payment payment);
          
              public Payment getPaymentById(@Param("id") Long id);
          }
          
          

          resources\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.youyang.springcloud.dao.PaymentDao">
          
              <resultMap id="BaseResultMap" type="com.youyang.springcloud.entities.Payment">
                  <result column="id" jdbcType="BIGINT" property="id"/>
                  <result column="serial" jdbcType="VARCHAR" property="serial"/>
          
              </resultMap>
          
              <insert id="create" parameterType="Payment" useGeneratedKeys="true" keyProperty="id">
                  insert into payment(serial) value(#{serial});
              </insert>
          
              <select id="getPaymentById" parameterType="Long" resultMap="BaseResultMap">
                  select * from payment where id=#{id};
              </select>
          
          </mapper>
          
        4. service

          com.youyang.springcloud.service.PaymentService

          package com.youyang.springcloud.service.impl;
          
          import com.youyang.springcloud.dao.PaymentDao;
          import com.youyang.springcloud.entities.Payment;
          import com.youyang.springcloud.service.PaymentService;
          import org.springframework.stereotype.Service;
          
          import javax.annotation.Resource;
          
          /**
           * @Author: yangjian
           * @Data: 2021/04/04 - 12:23
           * @Description:
           * @Version: 1.0
           */
          @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);
              }
          }
          
          

          com.youyang.springcloud.service.impl.PaymentServiceImpl

          package com.youyang.springcloud.service;
          
          import com.youyang.springcloud.entities.Payment;
          import org.apache.ibatis.annotations.Param;
          
          /**
           * @Author: yangjian
           * @Data: 2021/04/04 - 12:23
           * @Description:
           * @Version: 1.0
           */
          public interface PaymentService {
              public int create(Payment payment);
          
              public Payment getPaymentById(@Param("id") Long id);
          }
          
          
        5. controller

          com.youyang.springcloud.controller.PayentController

          package com.youyang.springcloud.controller;
          
          import com.youyang.springcloud.entities.CommonResult;
          import com.youyang.springcloud.entities.Payment;
          import com.youyang.springcloud.service.PaymentService;
          import lombok.extern.slf4j.Slf4j;
          import org.springframework.stereotype.Controller;
          import org.springframework.web.bind.annotation.*;
          
          import javax.annotation.Resource;
          
          /**
           * @Author: yangjian
           * @Data: 2021/04/04 - 12:26
           * @Description:
           * @Version: 1.0
           */
          @RestController
          @Slf4j
          public class PayentController {
          
              @Resource
              private PaymentService paymentService;
          
              @PostMapping(value = "/payment/create")
              public CommonResult<Payment> create(Payment payment){
                  int result = paymentService.create(payment);
                  log.info("*******插入结果********:"+result);
                  if (result > 0){
                      return new CommonResult(200,"插入数据库成功",result);
                  }else {
                      return new CommonResult(444,"插入数据库失败",null);
                  }
              }
              @GetMapping(value = "/payment/get/{id}")
              public CommonResult<Payment> create(@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,"查询失败",null);
                  }
              }
          
          }
          
          
    6. 测试

      测试工具 postman

本文仅作为自己笔记,不作为他人参考

相关标签: SpringCloud学习

上一篇: 散列

下一篇: 基础算法题-树相关