1. 时间格式
package com.hong.entity;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import java.math.BigDecimal;
import java.util.Date;
import javax.persistence.*;
/**
* 表名:t_pay
* 表注释:支付交易表
*/
@Table(name = "t_pay")
@Schema(title = "支付交易表实体类")
public class Pay {
@Id
@GeneratedValue(generator = "JDBC")
@Schema(title = "主键")
private Integer id;
/**
* 支付流水号
*/
@Column(name = "pay_no")
@Schema(title = "支付流水号")
private String payNo;
/**
* 订单流水号
*/
@Column(name = "order_no")
@Schema(title = "订单流水号")
private String orderNo;
/**
* 用户账号ID
*/
@Column(name = "user_id")
@Schema(title = "用户账号ID")
private Integer userId;
/**
* 交易金额
*/
@Schema(title = "交易金额")
private BigDecimal amount;
/**
* 删除标志,默认0不删除,1删除
*/
private Byte deleted;
/**
* 创建时间
*/
@Column(name = "create_time")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@Schema(title = "创建时间")
private Date createTime;
/**
* 更新时间
*/
@Column(name = "update_time")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@Schema(title = "更新时间")
private Date updateTime;
/**
* @return id
*/
public Integer getId() {
return id;
}
/**
* @param id
*/
public void setId(Integer id) {
this.id = id;
}
/**
* 获取支付流水号
*
* @return payNo - 支付流水号
*/
public String getPayNo() {
return payNo;
}
/**
* 设置支付流水号
*
* @param payNo 支付流水号
*/
public void setPayNo(String payNo) {
this.payNo = payNo;
}
/**
* 获取订单流水号
*
* @return orderNo - 订单流水号
*/
public String getOrderNo() {
return orderNo;
}
/**
* 设置订单流水号
*
* @param orderNo 订单流水号
*/
public void setOrderNo(String orderNo) {
this.orderNo = orderNo;
}
/**
* 获取用户账号ID
*
* @return userId - 用户账号ID
*/
public Integer getUserId() {
return userId;
}
/**
* 设置用户账号ID
*
* @param userId 用户账号ID
*/
public void setUserId(Integer userId) {
this.userId = userId;
}
/**
* 获取交易金额
*
* @return amount - 交易金额
*/
public BigDecimal getAmount() {
return amount;
}
/**
* 设置交易金额
*
* @param amount 交易金额
*/
public void setAmount(BigDecimal amount) {
this.amount = amount;
}
/**
* 获取删除标志,默认0不删除,1删除
*
* @return deleted - 删除标志,默认0不删除,1删除
*/
public Byte getDeleted() {
return deleted;
}
/**
* 设置删除标志,默认0不删除,1删除
*
* @param deleted 删除标志,默认0不删除,1删除
*/
public void setDeleted(Byte deleted) {
this.deleted = deleted;
}
/**
* 获取创建时间
*
* @return createTime - 创建时间
*/
public Date getCreateTime() {
return createTime;
}
/**
* 设置创建时间
*
* @param createTime 创建时间
*/
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
/**
* 获取更新时间
*
* @return updateTime - 更新时间
*/
public Date getUpdateTime() {
return updateTime;
}
/**
* 设置更新时间
*
* @param updateTime 更新时间
*/
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
}
2. 统一返回
2.1. 返回编码信息枚举
package com.hong.resp;
import lombok.Getter;
import java.util.Arrays;
/**
* @Description: 返回编码信息枚举
* @Author: hong
* @Date: 2024-04-13 17:10
* @Version: 1.0
**/
@Getter
public enum ReturnCodeEnum {
/**操作失败**/
RC999("999","操作XXX失败"),
/**操作成功**/
RC200("200","success"),
/**服务降级**/
RC201("201","服务开启降级保护,请稍后再试!"),
/**热点参数限流**/
RC202("202","热点参数限流,请稍后再试!"),
/**系统规则不满足**/
RC203("203","系统规则不满足要求,请稍后再试!"),
/**授权规则不通过**/
RC204("204","授权规则不通过,请稍后再试!"),
/**access_denied**/
RC403("403","无访问权限,请联系管理员授予权限"),
/**access_denied**/
RC401("401","匿名用户访问无权限资源时的异常"),
RC404("404","404页面找不到的异常"),
/**服务异常**/
RC500("500","系统异常,请稍后重试"),
RC375("375","数学运算异常,请稍后重试"),
INVALID_TOKEN("2001","访问令牌不合法"),
ACCESS_DENIED("2003","没有权限访问该资源"),
CLIENT_AUTHENTICATION_FAILED("1001","客户端认证失败"),
USERNAME_OR_PASSWORD_ERROR("1002","用户名或密码错误"),
BUSINESS_ERROR("1004","业务逻辑异常"),
UNSUPPORTED_GRANT_TYPE("1003", "不支持的认证模式");
/**自定义状态码**/
private final String code;
/**自定义描述**/
private final String message;
ReturnCodeEnum(String code, String message){
this.code = code;
this.message = message;
}
//遍历枚举V1
public static ReturnCodeEnum getReturnCodeEnum(String code)
{
for (ReturnCodeEnum element : ReturnCodeEnum.values()) {
if(element.getCode().equalsIgnoreCase(code))
{
return element;
}
}
return null;
}
//遍历枚举V2
public static ReturnCodeEnum getReturnCodeEnumV2(String code)
{
return Arrays.stream(ReturnCodeEnum.values()).filter(x -> x.getCode().equalsIgnoreCase(code)).findFirst().orElse(null);
}
}
2.2. 统一返回对象
package com.hong.resp;
import lombok.Data;
import lombok.experimental.Accessors;
/**
* @Description: 统一返回对象
* @Author: hong
* @Date: 2024-04-13 17:13
* @Version: 1.0
**/
@Data
@Accessors(chain = true)
public class ResultData<T> {
private String code;/** 结果状态 ,具体状态码参见枚举类ReturnCodeEnum.java*/
private String message;
private T data;
private long timestamp ;
public ResultData (){
this.timestamp = System.currentTimeMillis();
}
public static <T> ResultData<T> success(T data) {
ResultData<T> resultData = new ResultData<>();
resultData.setCode(ReturnCodeEnum.RC200.getCode());
resultData.setMessage(ReturnCodeEnum.RC200.getMessage());
resultData.setData(data);
return resultData;
}
public static <T> ResultData<T> fail(String code, String message) {
ResultData<T> resultData = new ResultData<>();
resultData.setCode(code);
resultData.setMessage(message);
return resultData;
}
}
3. 修改PayController
package com.hong.controller;
import com.hong.entity.Pay;
import com.hong.entity.PayDTO;
import com.hong.resp.ResultData;
import com.hong.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* @Description: TODO
* @Author: hong
* @Date: 2024-04-11 23:19
* @Version: 1.0
**/
@RestController
@Tag(name = "支付微服务模块",description = "支付CRUD")
public class PayController {
@Resource
PayService payService;
@PostMapping(value = "/pay/add")
@Operation(summary = "新增",description = "新增支付流水方法,json串做参数")
public ResultData<String> addPay(@RequestBody Pay pay){
System.out.println(pay.toString());
int i = payService.add(pay);
return ResultData.success(i > 0 ? "添加成功" : "添加失败");
}
@DeleteMapping(value = "/pay/del/{id}")
@Operation(summary = "删除",description = "删除支付流水方法")
public ResultData<Integer> deletePay(@PathVariable("id") Integer id) {
Integer i = payService.delete(id);
return ResultData.success(i);
}
@PutMapping(value = "/pay/update")
@Operation(summary = "修改",description = "修改支付流水方法,json串做参数")
public ResultData<String> updatePay(@RequestBody PayDTO payDTO){
Pay pay = new Pay();
BeanUtils.copyProperties(payDTO, pay);
int i = payService.update(pay);
return ResultData.success(i > 0 ? "修改成功" : "修改失败");
}
@GetMapping(value = "/pay/get/{id}")
@Operation(summary = "按照ID查流水",description = "查询支付流水方法")
public ResultData<Pay> getById(@PathVariable("id") Integer id){
Pay pay = payService.getById(id);
return ResultData.success(pay);
}
@GetMapping(value = "/pay/getAll")
@Operation(summary = "查询所有支付流水",description = "查询所有支付流水")
public ResultData<List> getAll(){
List<Pay> payList = payService.getAll();
return ResultData.success(payList);
}
}
4. 测试