栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringCloud学习(一):支付模块的构建

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringCloud学习(一):支付模块的构建

        今天开始学习springcloud,先搭建一个简单的支付模块,后续再慢慢增加功能。编写微服务代码都要遵循以下步骤:

一、创建module

二、更改pom文件

三、编写yml文件

四、编写主启动类

五、编写业务类

        1、sql语句

        2、entity

                (1)实体类

                (2)封装的json类型(用于传给前端)      

                  

        3、mapper

        4、mapper.xml

        5、service

        6、serviceImpl

        7、controller

六、运行测试

        1、测试get请求

        2、测试post请求



一、创建module

        创建一个简单的maven工程和模块,maven选择的版本是3.5.2.

二、更改pom文件

父工程pom.xml:




  4.0.0

  com.shang.cloud
  cloud
  1.0-SNAPSHOT
  
    cloud-provider-payment8001
  
  pom

  
  
    UTF-8
    1.8
    1.8
    4.12
    1.2.17
    1.16.18
    5.1.47
    1.1.16
    1.3.0
  

  
  
    
      
      
        org.springframework.boot
        spring-boot-dependencies
        2.2.2.RELEASE
        pom
        import
      
      
      
        org.springframework.cloud
        spring-cloud-dependencies
        Hoxton.SR1
        pom
        import
      
      
      
        com.alibaba.cloud
        spring-cloud-alibaba-dependencies
        2.1.0.RELEASE
        pom
        import
      
      
        mysql
        mysql-connector-java
        ${mysql.version}
      
      
        com.alibaba
        druid
        ${druid.version}
      


      
        org.springframework.boot
        spring-boot-devtools
        runtime
        true
      
      
        org.mybatis.spring.boot
        mybatis-spring-boot-starter
        ${mybatis.spring.boot.version}
      

      
        junit
        junit
        ${junit.version}
      
      
        log4j
        log4j
        ${log4j.version}
      
      
        org.projectlombok
        lombok
        ${lombok.version}
        true
      
    
  


  
    
      
        org.springframework.boot
        spring-boot-maven-plugin
        
          true
          true
        
      
    
  

支付模块pom文件:



    
        cloud
        com.shang.cloud
        1.0-SNAPSHOT
    
    4.0.0

    cloud-provider-payment8001

    
        
        
            org.springframework.cloud
            spring-cloud-starter-zipkin
        





        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
        





        
            com.alibaba
            druid
            1.0.20
        

        
        
            mysql
            mysql-connector-java
        
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    


三、编写yml文件

application.yml:

server:
  port: 8001

spring:
  application:
    name: cloud-payment-service
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
    driver-class-name: org.gjt.mm.mysql.Driver              # mysql驱动包
    url: jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: root

mybatis:
  mapperLocations: classpath:mapper
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult {

    private Integer code;
    private String message;
    private T data;

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

                  

        3、mapper
package com.shang.cloud.dao;

import com.shang.cloud.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);
}

        4、mapper.xml





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

    
        
        
    
    
    
        select * from payment where id=#{id}
    

        5、service
package com.shang.cloud.service;

import com.shang.cloud.entities.Payment;
import org.apache.ibatis.annotations.Param;

public interface PaymentService {

    public int create(Payment payment);

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

        6、serviceImpl
package com.shang.cloud.service.impl;

import com.shang.cloud.dao.PaymentDao;
import com.shang.cloud.entities.Payment;
import com.shang.cloud.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PaymentServiceImpl implements PaymentService {

    @Autowired
    private PaymentDao paymentDao;

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

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

        7、controller
package com.shang.cloud.controller;

import com.shang.cloud.entities.CommonResult;
import com.shang.cloud.entities.Payment;
import com.shang.cloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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;

@RestController
@Slf4j
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    @PostMapping(value = "/payment/create")
    public CommonResult create(@RequestBody 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 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);
        }
    }
}

六、运行测试

        1、测试get请求

        直接使用浏览器地址栏

        拿到json串

        2、测试post请求

        使用postman工具来测试

        数据库中也成功显示插入的数据 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/342989.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号