【SpringCloud02】订单-支付微服务模块(支付模块构建)

1.微服务cloud整体聚合父工程Project

  1. New Project
    【SpringCloud02】订单-支付微服务模块(支付模块构建)_第1张图片

  2. 聚合总父工程名字

  3. Maven选版本

  4. 工程名字

  5. 字符编码
    【SpringCloud02】订单-支付微服务模块(支付模块构建)_第2张图片

  6. 注解生效激活
    【SpringCloud02】订单-支付微服务模块(支付模块构建)_第3张图片

  7. Java编译版本选8
    【SpringCloud02】订单-支付微服务模块(支付模块构建)_第4张图片

  8. File Type过滤
    【SpringCloud02】订单-支付微服务模块(支付模块构建)_第5张图片

2.父工程POM

2.1Pom.xml文件

<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">
  <modelVersion>4.0.0modelVersion>

  
  
  <groupId>com.atguigu.springcloudgroupId>
  <artifactId>cloud2022artifactId>
  <version>1.0-SNAPSHOTversion>
  <packaging>pompackaging>

  
  <properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <maven.compiler.source>1.8maven.compiler.source>
    <maven.compiler.target>1.8maven.compiler.target>
    <junit.version>4.12junit.version>
    <log4j.version>1.2.17log4j.version>
    <lombok.version>1.16.18lombok.version>
    <mysql.version>5.1.47mysql.version>
    <druid.version>1.1.16druid.version>
    <mybatis.spring.boot.version>1.3.0mybatis.spring.boot.version>
  properties>

  

  <dependencyManagement>
    <dependencies>
      
      <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-dependenciesartifactId>
        <version>2.2.2.RELEASEversion>
        <type>pomtype>
        <scope>importscope>
      dependency>
      
      <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-dependenciesartifactId>
        <version>Hoxton.SR1version>
        <type>pomtype>
        <scope>importscope>
      dependency>
      
      <dependency>
        <groupId>com.alibaba.cloudgroupId>
        <artifactId>spring-cloud-alibaba-dependenciesartifactId>
        <version>2.1.0.RELEASEversion>
        <type>pomtype>
        <scope>importscope>
      dependency>
      <dependency>
        <groupId>mysqlgroupId>
        <artifactId>mysql-connector-javaartifactId>
        <version>${mysql.version}version>
      dependency>
      <dependency>
        <groupId>com.alibabagroupId>
        <artifactId>druidartifactId>
        <version>${druid.version}version>
      dependency>
      <dependency>
        <groupId>org.mybatis.spring.bootgroupId>
        <artifactId>mybatis-spring-boot-starterartifactId>
        <version>${mybatis.spring.boot.version}version>
      dependency>
      <dependency>
        <groupId>junitgroupId>
        <artifactId>junitartifactId>
        <version>${junit.version}version>
      dependency>
      <dependency>
        <groupId>log4jgroupId>
        <artifactId>log4jartifactId>
        <version>${log4j.version}version>
      dependency>
      <dependency>
        <groupId>org.projectlombokgroupId>
        <artifactId>lombokartifactId>
        <version>${lombok.version}version>
        <optional>trueoptional>
      dependency>
    dependencies>
  dependencyManagement>

  <distributionManagement>
    <site>
      <id>websiteid>
      <url>scp://webhost.company.com/www/websiteurl>
    site>
  distributionManagement>

  <build>
    <finalName>cloud2022finalName>
    <plugins>
      <plugin>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-maven-pluginartifactId>
        <version>2.3.5.RELEASEversion>
        <configuration>
          <fork>truefork>
          <addResources>trueaddResources>
        configuration>
      plugin>
    plugins>
  build>
project>

2.2问题:Maven中的DependencyManagement和Dependencies区别

解答:
dependencyManagement:

Maven 使用dependencyManagement 元素来提供了一种管理依赖版本号的方式。
通常会在一个组织或者项目的最顶层的父POM 中看到dependencyManagement 元素。

使用pom.xml 中的dependencyManagement 元素能让所有在子项目中引用一个依赖而不用显式的列出版本号。

Maven 会沿着父子层次向上走,直到找到一个拥有dependencyManagement 元素的项目,然后它就会使用这个dependencyManagement 元素中指定的版本号。
【SpringCloud02】订单-支付微服务模块(支付模块构建)_第6张图片
好处:如果有多个子项目都引用同一样依赖,则可以避免在每个使用的子项目里都声明一个版本号,这样当想升级或切换到另一个版本时,只需要在顶层父容器里更新,而不需要一个一个子项目的修改;另外如果某个子项目需要另外的一个版本,只需要声明version就可。

  • dependencyManagement 里只是声明依赖,并不实现引入,因此子项目需要显示的声明需要用的依赖

  • 如果不在子项目中声明依赖,是不会从父项目中继承下来的;只有在子项目中写了该依赖项,并且没有指定具体版本,才会从父项目中继承该项,并且version和scope都读取自父pom

  • 如果子项目中指定了版本号,那么会使用子项目中指定的jar版本

2.2Maven中如何跳过单元测试

生命周期中的test会有横线
【SpringCloud02】订单-支付微服务模块(支付模块构建)_第7张图片

2.3父工程创建完成执行mvn:install将父工程发布到仓库方便子工程继承

【SpringCloud02】订单-支付微服务模块(支付模块构建)_第8张图片

2.4Rest微服务工程构建

消费者调用支付模块
【SpringCloud02】订单-支付微服务模块(支付模块构建)_第9张图片

约定 > 配置 > 编码

如何构建微服务模块?

  1. 建module
  2. 改POM
  3. 写YML
  4. 主启动
  5. 业务类

2.4.1cloud-provider-payment8001微服务提供者支付模块

1. 建cloud-provider-payment8001

创建完成后到父工程查看pom文件变化
【SpringCloud02】订单-支付微服务模块(支付模块构建)_第10张图片
【SpringCloud02】订单-支付微服务模块(支付模块构建)_第11张图片
【SpringCloud02】订单-支付微服务模块(支付模块构建)_第12张图片

2. 改POM

<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">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>com.atguigu.springcloudgroupId>
        <artifactId>cloud2022artifactId>
        <version>1.0-SNAPSHOTversion>
    parent>

    <artifactId>cloud-provider-payment8001artifactId>

    <properties>
        <maven.compiler.source>8maven.compiler.source>
        <maven.compiler.target>8maven.compiler.target>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druid-spring-boot-starterartifactId>
            <version>1.1.10version>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jdbcartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <scope>runtimescope>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>
project>

3. 写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驱动包 com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/cloud22?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: 123456


mybatis:
  mapperLocations: classpath:mapper/*.xml
  type-aliases-package: com.atguigu.springcloud.entities    # 所有Entity别名类所在包

4. 主启动

【SpringCloud02】订单-支付微服务模块(支付模块构建)_第13张图片

5. 业务类

【SpringCloud02】订单-支付微服务模块(支付模块构建)_第14张图片

  1. 建表SQL
    【SpringCloud02】订单-支付微服务模块(支付模块构建)_第15张图片
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
  1. entities
    主实体Payment类

    package com.atguigu.springcloud.entities;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    import java.io.Serializable;
    
    /**
     * 简要描述
     *
     * @Author: ASuLe
     * @Date: 2023/1/11 16:42
     * @Version: 1.0
     * @Description: 文件作用详细描述....
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class Payment implements Serializable {
    
        private Long id;
        private String serial;
    }
    

    json封装类

    package com.atguigu.springcloud.entities;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    /**
     * 简要描述
     *
     * @Author: ASuLe
     * @Date: 2023/1/11 16:45
     * @Version: 1.0
     * @Description: Json封装类
     */
    @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);
        }
    }
    
    
  2. dao
    dao层

    package com.atguigu.springcloud.dao;
    
    import com.atguigu.springcloud.entities.Payment;
    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Param;
    
    /**
     * 简要描述
     *
     * @Author: ASuLe
     * @Date: 2023/1/11 16:48
     * @Version: 1.0
     * @Description: payment
     */
    @Mapper
    public interface PaymentDao {
    
        public int create(Payment payment);
    
        public Payment getPaymentById(@Param("id") Long id);
    
    }
    
    

    xml隐射文件

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

    package com.atguigu.springcloud.service;
    
    import com.atguigu.springcloud.entities.Payment;
    import org.apache.ibatis.annotations.Param;
    
    /**
     * 简要描述
     *
     * @Author: ASuLe
     * @Date: 2023/1/11 17:07
     * @Version: 1.0
     * @Description: 文件作用详细描述....
     */
    public interface PaymentService {
        public int create(Payment payment);
    
        public Payment getPaymentById(@Param("id") Long id);
    }
    
    

    实现类PaymentServiceImpl

    package com.atguigu.springcloud.service.impl;
    
    import com.atguigu.springcloud.dao.PaymentDao;
    import com.atguigu.springcloud.entities.Payment;
    import com.atguigu.springcloud.service.PaymentService;
    import org.springframework.stereotype.Service;
    
    import javax.annotation.Resource;
    
    /**
     * 简要描述
     *
     * @Author: ASuLe
     * @Date: 2023/1/11 17:08
     * @Version: 1.0
     * @Description: 文件作用详细描述....
     */
    @Service
    public class PaymentServiceImpl implements PaymentService {
        //@Autowired @Resource都可以进行依赖注入
        @Resource
        private PaymentDao paymentDao;
    
    
        @Override
        public int create(Payment payment) {
            return paymentDao.create(payment);
        }
    
        @Override
        public Payment getPaymentById(Long id) {
            return paymentDao.getPaymentById(id);
        }
    }
    
    
  4. controller

    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;
    
    /**
     * 简要描述
     *
     * @Author: ASuLe
     * @Date: 2023/1/11 17:11
     * @Version: 1.0
     * @Description: 文件作用详细描述....
     */
    @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);
            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);
            }
        }
    
    }
    
    

测试

查询测试
【SpringCloud02】订单-支付微服务模块(支付模块构建)_第16张图片
添加测试
【SpringCloud02】订单-支付微服务模块(支付模块构建)_第17张图片

浏览器更多是为了查看,不太支持post请求,使用postman

【SpringCloud02】订单-支付微服务模块(支付模块构建)_第18张图片
查看数据库
【SpringCloud02】订单-支付微服务模块(支付模块构建)_第19张图片
【SpringCloud02】订单-支付微服务模块(支付模块构建)_第20张图片

你可能感兴趣的:(SpringCould学习,微服务,spring,boot,java)