支付微服务设计方案格式模板_Spring cloud:支付微服务-支付

环境

spring cloud Edgware.SR6

jdk 7

sts 4.6.0

mysql 5.7

背景

搭建支付微服务的环境。

搭建步骤

数据层

package jiangbo.springcloud.dao;

import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.jdbc.core.RowMapper;

import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;

import org.springframework.jdbc.core.simple.SimpleJdbcInsert;

import org.springframework.stereotype.Repository;

import jiangbo.springcloud.entity.PaymentInfo;

@Repository

public class PaymentDaoImpl implements PaymentDao {

private static final String QUERY_ALL_PAYMENT_SQL = "select * from payment_info";

private static final String QUERY_PAYMENT_BY_ID_SQL = QUERY_ALL_PAYMENT_SQL + " where id = ?";

private static final RowMapper ROW_MAPPER = new BeanPropertyRowMapper<>(PaymentInfo.class);

private final JdbcTemplate jdbcTemplate;

public PaymentDaoImpl(JdbcTemplate jdbcTemplate) {

this.jdbcTemplate = jdbcTemplate;

}

@Override

public List queryAllPayments() {

return jdbcTemplate.query(QUERY_ALL_PAYMENT_SQL, ROW_MAPPER);

}

@Override

public long insertPaymentInfo(PaymentInfo paymentInfo) {

paymentInfo.setStatus("SUCCESS");

return new SimpleJdbcInsert(jdbcTemplate)

.withTableName("payment_info")

// 指定主键

.usingGeneratedKeyColumns("id")

// 更新的列

.usingColumns("order_id", "amount", "status")

// 参数

.executeAndReturnKey(new BeanPropertySqlParameterSource(paymentInfo)).longValue();

}

@Override

public PaymentInfo queryPaymentInfo(long id) {

return jdbcTemplate.queryForObject(QUERY_PAYMENT_BY_ID_SQL, ROW_MAPPER, id);

}

}

服务层

package jiangbo.springcloud.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import jiangbo.springcloud.dao.PaymentDao;

import jiangbo.springcloud.entity.PaymentInfo;

import jiangbo.springcloud.service.PaymentService;

@Service

public class PaymentServiceImpl implements PaymentService {

private final PaymentDao paymentDao;

public PaymentServiceImpl(PaymentDao paymentDao) {

this.paymentDao = paymentDao;

}

@Override

public List queryAllPayments() {

return paymentDao.queryAllPayments();

}

@Override

public PaymentInfo insertPaymentInfo(PaymentInfo paymentInfo) {

long id = paymentDao.insertPaymentInfo(paymentInfo);

return paymentDao.queryPaymentInfo(id);

}

}

控制层

package jiangbo.springcloud.controller;

import java.util.List;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import jiangbo.springcloud.entity.PaymentInfo;

import jiangbo.springcloud.service.PaymentService;

@RestController

@RequestMapping("/payment")

public class PaymentContrller {

private final PaymentService paymentService;

public PaymentContrller(PaymentService paymentService) {

this.paymentService = paymentService;

}

@PostMapping

public PaymentInfo newPaymentInfo(@RequestBody PaymentInfo paymentInfo) {

return paymentService.insertPaymentInfo(paymentInfo);

}

@GetMapping

public List allPayemtns() {

return paymentService.queryAllPayments();

}

}

验证

使用命名进行数据的新增,看到如下的结果,则证明成功:

curl -H "Content-Type: application/json" -X POST --data '{"orderId":8,"amount":"8.88"}', http://localhost:4420/payment

{"id":2,"orderId":8,"amount":"8.88","status":"SUCCESS","createTime":1587224718000}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值