支付微服务模块

目录

构建支付微服务模块

建Module​编辑

改POM,引入依赖​编辑

写yml​编辑

主启动类​编辑

支付功能业务类

建表语句

entity实体类 

 common消息返回类

mapper接口

mapper.xml(mybatis)

service接口

 service实现类

controller层

 整理分层架构

运行启动:

 热部署

Adding devtools to your project

Adding plugin to your pom.xml

Enabling automatic build

Update the value of


约定  > 配置  >  编码

构建支付微服务模块

  1. 建Module

  2. 改POM,引入依赖

  3. 写yml

  4. 主启动类

  5. 支付功能业务类

  • 建表语句
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
  • entity实体类 
package com.tudeen.learn.entity;
import lombok.Data;
import java.io.Serializable;
@Data
public class Payment implements Serializable {
    private Long id;
    private String serial;
}
  •  common消息返回类
package com.tudeen.learn.common;
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);
    }
}
  • mapper接口
package com.tudeen.learn.mapper;

import com.tudeen.learn.entity.Payment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

@Mapper
public interface PaymentMapper {

    public int create(Payment payment);

    public Payment getPaymentByID(@Param("id") Long id);
}
  • mapper.xml(mybatis)
<?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.tudeen.learn.mapper.PaymentMapper">

    <resultMap id="BaseResultMap" type="com.tudeen.learn.entity.Payment">
        <id column="id" property="id" jdbcType="BIGINT"></id>
        <result column="serial" property="serial" jdbcType="VARCHAR"></result>
    </resultMap>

    <insert id="create" parameterType="com.tudeen.learn.entity.Payment" useGeneratedKeys="true"
            keyProperty="id">
        insert into payment(serial) values(#{serial})
    </insert>

    <select id="getPaymentByID" resultMap="BaseResultMap">
        select id,serial from payment where id=#{id}
    </select>

</mapper>
  • service接口
package com.tudeen.learn.service;
import com.tudeen.learn.entity.Payment;

public interface PaymentService {
    public int create(Payment payment);

    public Payment getPaymentById(Long id);
}
  •  service实现类
package com.tudeen.learn.service.impl;

import com.tudeen.learn.entity.Payment;
import com.tudeen.learn.mapper.PaymentMapper;
import com.tudeen.learn.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PaymentServiceImpl implements PaymentService {

    @Autowired
    private PaymentMapper paymentMapper;

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

    @Override
    public Payment getPaymentById(Long id) {
        return paymentMapper.getPaymentById(id);
    }
}
  • controller层
package com.tudeen.learn.controller;

import com.tudeen.learn.common.CommonResult;
import com.tudeen.learn.entity.Payment;
import com.tudeen.learn.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @PostMapping(value = "/payment/create")
    public CommonResult Create(@RequestBody Payment payment) {
        int i = paymentService.create(payment);
        log.info("插入结果{}", i);
        if (i > 0) {
            return new CommonResult(200, "插入成功,serverPort:" + serverPort, i);
        } else {
            return new CommonResult(400, "插入失败", null);
        }
    }

    @GetMapping(value = "/payment/getPaymentByid/{id}")
    public CommonResult getPaymentByid(@PathVariable Long id) {
        Payment payment = paymentService.getPaymentById(id);
        log.info("获取结果{}", payment);
        return new CommonResult(200, "获取成功,serverPort:" + serverPort, payment);
    }
}
  •  整理分层架构

如下

运行启动:

http://localhost:8001/payment/getPaymentByid/31

 热部署

代码如果改动了一会希望自动重启生效,让他自动热部署

Adding devtools to your project

添加热部署Pom文件dependency

  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
 </dependency>
Adding plugin to your pom.xml

 在pom配置文件中添加Maven插件

      <!--cloud工程pom -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <addResources>true</addResources>
                </configuration>
            </plugin>
        </plugins>
    </build>
Enabling automatic build

开启自动部署的选项 

Update the value of

热部署开启,点击Ctrl + Shift + Alt + /  ,勾选后重启idea 如下 :

 

当修改代码后会自动进行部署。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值