二,创建支付微服务提供者 第二章

<dependencies>
        <!--web-->
        <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>
            <version>2.0.0</version>
        </dependency>
        <!--阿里云数据源-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
        </dependency>
         <!--驱动-->
        <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>
        <!--lombok-->
        <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>

2.4编辑yml

 server:
  port: 8001
 spring:
  application:
    name: springcloud-payment-provider-service
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/springcloud_db?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT
    username: root
    password: xiaoduo456new

 mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: cn.bdqn.domain # 所有Entity别名类所在包

2.5编写启动类

@SpringBootApplication
    public class PaymentApplication {
        public static void main(String[] args) {
            SpringApplication.run(PaymentApplication.class,args);
        }
}

 2.6建库建表

-- create database springcloud_db

-- use springcloud_db;

CREATE TABLE `t_payment`(
	`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
	`flow_number` varchar(32),
	PRIMARY KEY(`id`)
) ENGINE=INNODB AUTO_INCREMENT = 1 DEFAULT CHARSET = UTF8;

2.7 建实体

package cn.bdqn.domain;

import java.io.Serializable;

public class Payment implements Serializable {


    private Integer id;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getFlowNumber() {
        return flowNumber;
    }

    public void setFlowNumber(String flowNumber) {
        this.flowNumber = flowNumber;
    }

    private String flowNumber;

    @Override
    public String toString() {
        return "Payment{" +
                "id=" + id +
                ", flowNumber='" + flowNumber + '\'' +
                '}';
    }
}

2.8编写paymentMapper接口


@Mapper
public interface PaymentMapper {
    //保存一个支付流水
    public void insert(Payment payment);

    //根据id获取具体的支付信息
    public Payment selectById(@Param("id") Integer id);
}

2.9编写pyamentMapper.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="cn.bdqn.Mapper.PaymentMapper">
    <resultMap id="PaymentResultMap" type="cn.bdqn.domain.Payment">
        <id column="id" property="id"></id>
        <id column="flow_number" property="flowNumber"></id>
    </resultMap>

    <insert id="insert" parameterType="cn.bdqn.Mapper.PaymentMapper">
        insert into t_payment (flow_number) values(#{flowNumber});
    </insert>

    <select id="selectById" resultMap="PaymentResultMap">
        select id,flow_number from t_payment where id=#{id};
    </select>
</mapper>

2.10编写payment业务接口以及实现类


public interface PaymentService {
    //保存一个支付流水
    public  void save(Payment payment);

    //根据id获取具体的支付信息
    public Payment queryById(Integer id);

}
@Service
public class PaymentServiceImpl  implements PaymentService{

    @Autowired
    private PaymentMapper paymentMapper;

    @Transactional(propagation = Propagation.REQUIRED)
    public void save(Payment payment) {
        paymentMapper.insert(payment);
    }

   @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    public Payment queryById(Integer id) {
        return paymentMapper.selectById(id);
    }
}

  2.11编写响应结果的Bean

public class ResponseResult<T> {

    //响应的编码
    private  Integer code;

    //响应给前段用户的消息提示
    private String message;

    //响应的数据
    private T data;

    public ResponseResult() {
    }

    public ResponseResult(Integer code, String message) {
        this.code = code;
        this.message = message;
    }
    public void setCode(Integer code) {
        this.code = code;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public T getData() {
        return data;
    }

 2.12编写PaymentController控制器

@Autowired
  private PaymentService paymentService;

  @GetMapping("/payment/get/{id}")
  public ResponseResult queryById(@PathVariable(name="id") Integer id){
      Payment payment=paymentService.queryById(id);
      if(payment!=null){
          return new ResponseResult(200, "成功",payment);
      }else{
          return  new ResponseResult(404,"没有对应记录,查询ID:"+id, null);
      }
  }

  @GetMapping("/payment/save")
  public ResponseResult save(Payment payment){
      try {
          paymentService.save(payment);
          return new ResponseResult(200,"成功",null);
      }catch (Exception e){
          e.printStackTrace();
          return new ResponseResult(500,"失败",null);
      }
  }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值