微信支付--在线支付实战,引入Swagger,定义统一结果,创建并连接数据库
目录
2、引入Swagger
2.1、引入依赖
2.2、Swagger配置文件
2.3、Swagger注解
2.4、测试
3、定义统一结果
3.1、引入lombok依赖
3.2、创建R类
3.3、修改controller
3.4、配置json时间格式
3.5、Swagger测试
4、创建数据库
4.1、创建数据库
4.2、IDEA配置数据库连接
(1)打开数据库面板
(2)添加数据库
(3)配置数据库连接参数
4.3、执行SQL脚本
2、引入Swagger
作用:自动生成接口文档和测试页面。
2.1、引入依赖
pom.xml下
<!--Swagger--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.7.0</version></dependency><!--Swagger ui--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.7.0</version></dependency>
2.2、Swagger配置文件
创建config包,创建Swagger2Config类
package com.atthruster.paymentdemo.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;@Configuration
@EnableSwagger2
public class Swagger2Config {@Beanpublic Docket docket(){return new Docket(DocumentationType.SWAGGER_2).apiInfo(new ApiInfoBuilder().title("微信支付案例接口文档").build());}
}
2.3、Swagger注解
controller中可以添加常用注解
@Api(tags = "商品管理")//用在类上@ApiOperation("测试接口")//用在方法上
2.4、测试
访问:http://localhost:8090/swagger-ui.html
3、定义统一结果
作用:定义统一响应结果,为前端返回标准格式的数据。
3.1、引入lombok依赖
简化实体类的开发
<!--lombok--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.30</version></dependency>
注意!!当你使用JDK21时,如果版本号不输入或者低于1.18.30,idea会报错
java: java.lang.NoSuchFieldError: Class com.sun.tools.javac.tree.JCTree$JCImport does not have member field 'com.sun.tools.javac.tree.JCTree qualid'
原因是:Lombok 与 JDK 21 兼容的最低 Lombok 版本是 1.18.30,最小的 Spring Boot 版本是 3.1.4。
3.2、创建R类
在总目录下创建vo包(value object) 创建统一结果类
package com.atthruster.paymentdemo.vo;import lombok.Data;
import lombok.Getter;import java.util.HashMap;
import java.util.Map;@Data
public class R {private Integer code;//响应码private String message;//响应消息private Map<String,Object> data = new HashMap<>();public static R ok(){R r = new R();r.setCode(0);r.setMessage("成功");return r;}public static R error(){R r = new R();r.setCode(-1);r.setMessage("失败");return r;}public R data(String key,Object value){this.data.put(key,value);return this;}}
3.3、修改controller
修改test方法,返回统一结果
package com.atthruster.paymentdemo.controller;import com.atthruster.paymentdemo.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.Date;@Api(tags = "商品管理")//用在类上
@RestController
@RequestMapping("/api/product")
public class ProductController {@ApiOperation("测试接口")//用在方法上@GetMapping("/test")public R test(){return R.ok().data("message","hello").data("now",new Date());}
}
3.4、配置json时间格式
因为返回时间和本地不同,同步本地时间
server:port: 8090spring:application:name: payment-demojackson:date-format: yyyy-MM-dd HH:mm;sstime-zone: GMT+8
3.5、Swagger测试
http://localhost:8090/swagger-ui.html
成功
4、创建数据库
4.1、创建数据库
打开cmd
mysql -uroot -p
mysql> create database payment_demo;
4.2、IDEA配置数据库连接
(1)打开数据库面板
打开ieda
(2)添加数据库
(3)配置数据库连接参数
设置数据库信息
4.3、执行SQL脚本
连接上mysql后会自动出现console区
在里面输入脚本
USE `payment_demo`;/*Table structure for table `t_order_info` */CREATE TABLE `t_order_info` (`id` bigint(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '订单id',`title` varchar(256) DEFAULT NULL COMMENT '订单标题',`order_no` varchar(50) DEFAULT NULL COMMENT '商户订单编号',`user_id` bigint(20) DEFAULT NULL COMMENT '用户id',`product_id` bigint(20) DEFAULT NULL COMMENT '支付产品id',`total_fee` int(11) DEFAULT NULL COMMENT '订单金额(分)',`code_url` varchar(50) DEFAULT NULL COMMENT '订单二维码连接',`order_status` varchar(10) DEFAULT NULL COMMENT '订单状态',`create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',`update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;/*Table structure for table `t_payment_info` */CREATE TABLE `t_payment_info` (`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '支付记录id',`order_no` varchar(50) DEFAULT NULL COMMENT '商户订单编号',`transaction_id` varchar(50) DEFAULT NULL COMMENT '支付系统交易编号',`payment_type` varchar(20) DEFAULT NULL COMMENT '支付类型',`trade_type` varchar(20) DEFAULT NULL COMMENT '交易类型',`trade_state` varchar(50) DEFAULT NULL COMMENT '交易状态',`payer_total` int(11) DEFAULT NULL COMMENT '支付金额(分)',`content` text COMMENT '通知参数',`create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',`update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;/*Table structure for table `t_product` */CREATE TABLE `t_product` (`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '商品id',`title` varchar(20) DEFAULT NULL COMMENT '商品名称',`price` int(11) DEFAULT NULL COMMENT '价格(分)',`create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',`update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;/*Data for the table `t_product` */insert into `t_product`(`title`,`price`) values ('Java课程',1);
insert into `t_product`(`title`,`price`) values ('大数据课程',1);
insert into `t_product`(`title`,`price`) values ('前端课程',1);
insert into `t_product`(`title`,`price`) values ('UI课程',1);/*Table structure for table `t_refund_info` */CREATE TABLE `t_refund_info` (`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '退款单id',`order_no` varchar(50) DEFAULT NULL COMMENT '商户订单编号',`refund_no` varchar(50) DEFAULT NULL COMMENT '商户退款单编号',`refund_id` varchar(50) DEFAULT NULL COMMENT '支付系统退款单号',`total_fee` int(11) DEFAULT NULL COMMENT '原订单金额(分)',`refund` int(11) DEFAULT NULL COMMENT '退款金额(分)',`reason` varchar(50) DEFAULT NULL COMMENT '退款原因',`refund_status` varchar(10) DEFAULT NULL COMMENT '退款状态',`content_return` text COMMENT '申请退款返回参数',`content_notify` text COMMENT '退款结果通知参数',`create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',`update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
全选后执行
四张表导入完成