目录
使用Knife4j的Tag注解为接口文档中的模块分组提供名称和描述信息和Operation注解为接口命名。
一、查询所有支付方式列表
接口名称:listPaymentType
请求方式:Get
请求路径:/admin/payment/list
请求参数:无
@Tag(name = "支付方式管理") //使用knife4j的Tag注解为接口文档中的模块分组提供名称和描述信息
@RequestMapping("/admin/payment")
@RestController
public class PaymentTypeController {
@Autowired
private PaymentTypeService service;
}
由于使用MybatisPlus的list()方法直接查询“支付方式”会把所有字段都查询出来,所以使用QueryWrapeer进行条件查询,筛选掉创建时间、逻辑删除id等字段。
LambdaQueryWrapper<PaymentType> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(PaymentType::getIsDeleted,0);
但是还有更加简洁的方法,就是在实体类上的isDeleted属性上使用注解@TableLogic来是程序识别该属性开启逻辑删除和使用Jackson的@JsonIgnore注解使得返回的json数据忽略该属性:
@Data //Lombok下的注解自动生成getter和setter方法
public class BaseEntity implements Serializable {
@Schema(description = "主键")
@TableId(value = "id", type = IdType.AUTO)
private Long id;
@JsonIgnore
@Schema(description = "创建时间")
@TableField(value = "create_time")
private Date createTime;
@JsonIgnore
@Schema(description = "更新时间")
@TableField(value = "update_time")
private Date updateTime;
@TableLogic
@JsonIgnore
@Schema(description = "逻辑删除")
@TableField("is_deleted")
private Byte isDeleted;
}
如今便可以直接使用MybatisPlus的查询方法了。
@Operation(summary = "查询全部支付方式列表") //Operation注解使用为接口命名
@GetMapping("list")
public Result<List<PaymentType>> listPaymentType() {
LambdaQueryWrapper<PaymentType> queryWrapper = new LambdaQueryWrapper<>();
List<PaymentType> list = service.list(queryWrapper);
return Result.ok(list);
二、保存或更新支付方式的接口
接口名称:saveOrUpdatePaymentType
请求方式:Post
请求路径:/admin/payment/saveOrUpdate
请求参数:{
"id": ,
"name": "",
"payMonthCount": "",
"additionalInfo": ""
}
@Operation(summary = "保存或更新支付方式")
@PostMapping("saveOrUpdate")
public Result saveOrUpdatePaymentType(@RequestBody PaymentType paymentType) {
service.saveOrUpdate(paymentType); //当id为null时save,当id不为null时update
return Result.ok();
}
由于修改或插入时不能自动处理create_time和update_time字段的数据,因此可能需要手动赋值处理设置create_time和update_time。但是MybatisPlus为我们提供了注解@TableField,只需在里面设置属性fill即可。
@JsonIgnore
@Schema(description = "创建时间")
@TableField(value = "create_time",fill = FieldFill.INSERT) //插入时填充create_time数据
private Date createTime;
@JsonIgnore
@Schema(description = "更新时间")
@TableField(value = "update_time",fill = FieldFill.UPDATE) //修改时填充update_time数据
private Date updateTime;
但是测试很快发现,只是添加注解还是不可以,需要配置MybatisMetaObjectHandler来指定create_time和update_time的填充值为当前时间。
@Component
public class MybatisMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
}
@Override
public void updateFill(MetaObject metaObject) {
this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
}
}
三、根据ID删除支付方式接口
接口名称:deletePaymentById
请求方式:Delete
请求路径:/admin/payment/deleteById
请求参数:id
注意的是❗:这里的删除只是进行逻辑删除,即把字段is_deleted设置为1,并不是物理删除。
@Operation(summary = "根据ID删除支付方式")
@DeleteMapping("deleteById")
public Result deletePaymentById(@RequestParam Long id) {
service.removeById(id);
return Result.ok();
}
SpringBoot支付接口开发
1950

被折叠的 条评论
为什么被折叠?



