• ss-2.子项目互相访问(order80 -> payment8001)


    order80

    ├─java
    │  └─com
    │      └─cmk
    │          └─consumerOrder80
    │              │  OrderMain80.java  //jetbrains://idea/navigate/reference?project=zy2020&path=com/cmk/consumerOrder80/OrderMain80.java:12:1
    │              │
    │              ├─config
    │              │      ApplicationContextConfig.java
    │              │
    │              ├─controller
    │              │      OrderController.java
    │              │
    │              └─entities
    │                      CommonResult.java
    │                      Payment.java
    │
    └─resources
            application.yml
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    OrderMain80.java

    @SpringBootApplication
    public class OrderMain80 {
        public static void main(String[] args) {
            SpringApplication.run(OrderMain80.class,args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    ApplicationContextConfig.java

    @Configuration
    public class ApplicationContextConfig {
    
        @Bean
        public RestTemplate getRestTemplate(){
            return  new RestTemplate();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    OrderController.java

    @RestController
    @Slf4j
    public class OrderController {
    
        public static final  String PAYMENT_URL = "http://localhost:8001";
        @Resource
        private RestTemplate restTemplate;
    
        @GetMapping("/consumer/payment/create")
        public CommonResult<Payment> create(Payment payment){
            System.out.println(payment);
            return  restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommonResult.class);
        }
    
        @GetMapping("consumer/payment/get/{id}")
        public CommonResult getPayment(@PathVariable("id") long id){
            return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    entities

    CommonResult.java

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class CommonResult<T> {
        //404 not_cound
        private Integer code;
        private String message;
        private T data;
    
        public CommonResult(Integer code, String message) {
            this(code,message,null);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    Payment.java

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class Payment implements Serializable {
        private long id;
        private String serial;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    application.yml

    server:
      port: 80
    
    • 1
    • 2

    payment8001

    ├─main
    │  ├─java
    │  │  └─com
    │  │      └─cmk
    │  │          └─springCloud
    │  │              │  PaymentMain8001.java  --jetbrains://idea/navigate/reference?project=zy2020&path=com/cmk/springCloud/PaymentMain8001.java:12:1
    │  │              │
    │  │              ├─controller
    │  │              │      PaymentController.java
    │  │              │
    │  │              ├─dao
    │  │              │      PaymentDao.java
    │  │              │
    │  │              ├─entities
    │  │              │      CommonResult.java
    │  │              │      Payment.java
    │  │              │
    │  │              └─service
    │  │                  │  PaymentService.java
    │  │                  │
    │  │                  └─impl
    │  │                          PaymentServiceImpl.java
    │  │
    │  └─resources
    │      │  application.yml
    │      │
    │      └─mapper
    │              PaymentMapper.xml
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    PaymentMain8001.java

    @SpringBootApplication
    public class PaymentMain8001 {
        public static void main(String[] args) {
            SpringApplication.run(PaymentMain8001.class,args);
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    PaymentController.java

    @RestController
    @Slf4j
    public class PaymentController {
    
        @Resource
        private PaymentService paymentService;
    
        @PostMapping("/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(400,"插入数据库失败",null);
            }
        }
    
        @GetMapping("/payment/get/{id}")
        public CommonResult getPaymentById(@PathVariable("id") long id){
            Payment result = paymentService.getPaymentById(id);
            log.info("****查询结果"+result+"456");
            if (result !=null){
                return  new CommonResult(200,"查询数据库成功",result);
            }else{
                return  new CommonResult(400,"查询数据库失败",null);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    PaymentDao.java

    @Mapper
    public interface PaymentDao {
         int create(Payment payment);
    
         Payment getPaymentById(@Param("id") long id);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    entities

    CommonResult.java

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class CommonResult<T> {
        //404 not_cound
        private Integer code;
        private String message;
        private T data;
    
        public CommonResult(Integer code, String message) {
            this(code,message,null);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    Payment.java

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class Payment implements Serializable {
        private long id;
        private String serial;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    service

    PaymentService.java

    @Mapper
    public interface PaymentService {
         int create(Payment payment);
    
         Payment getPaymentById(@Param("id") long id);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    PaymentServiceImpl.java

    @Service
    public class PaymentServiceImpl implements PaymentService {
    
        @Resource
        private PaymentDao paymentDao;
    
        public int create(Payment payment){
            return  paymentDao.create(payment);
        }
    
        public Payment getPaymentById(@Param("id") long id){
            return paymentDao.getPaymentById(id);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    application.yml

    server:
      port: 8001
    
    spring:
      application:
        name: cloud-payment-service
      datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
        username: root
        password: 123456
    
    
    mybatis:
      mapper-locations: classpath:mapper/*.xml
      type-aliases-package: com.cmk.springCloud.entities
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    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.cmk.springCloud.service.PaymentService">
        <insert id="create" parameterType="com.cmk.springCloud.entities.Payment">
            insert into payment(serial) values (#{serial});
        </insert>
    
        <select id="getPaymentById" parameterType="long" resultMap="BaseResultMap">
            select * from payment where id=#{id}
        </select>
    
        <resultMap id="BaseResultMap" type="com.cmk.springCloud.entities.Payment">
            <id column="id" property="id" jdbcType="BIGINT"/>
            <id column="serial" property="serial" jdbcType="VARCHAR"/>
        </resultMap>
    
    </mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
  • 相关阅读:
    Go-Excelize API源码阅读(三十三)—— RemoveCol
    重新定义每天进步一点点
    Mac安装redis详解(附图片)
    常见的损失函数
    20天等待,申请终于通过,安装和体验IntelliJ IDEA新UI预览版
    html+css+js实现高度过渡效果
    计算机网络和因特网
    芯片启动以及boot
    Spring cloud alibaba
    多年的生活经历,让我深刻领悟到的20条生活原则
  • 原文地址:https://blog.csdn.net/lhorse003/article/details/125615449