开始第一部分——房间支付方式管理的开发学习

开发前准备

  • 有无接口文档
    • 接口文档。拿到接口文档后,要和领导沟通好,对接人是谁(提前说好有问题找谁)
    • 接口文档。进一步询问细节,不要自己去做

开发步骤

1. 找到对应的数据库

payment_type

payment_type

2. 设计接口

接口1:查询全部支付方式列表

1
2
3
4
5
6
7
8
9
10
// 注入service
@Autowired
private PaymentTypeService paymentTypeService;

// 查询所有支付类型
@GetMapping("list")
public List<PaymentType> listPaymentType(){
List<PaymentType> list = paymentTypeService.list();
return list;
}
  • 使用knife4j进行测试

​ 浏览器访问 http://localhost:8080/doc.html#/全部接口/支付方式管理/listPaymentType

knife4j测试界面

附上knife4j配置文件之后学习

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
@Configuration
public class Knife4jConfiguration {

@Bean
public OpenAPI customOpenAPI() {

return new OpenAPI().info(
new Info()
.title("后台管理系统API")
.version("1.0")
.description("后台管理系统API"));
}

@Bean
public GroupedOpenApi systemAPI() {

return GroupedOpenApi.builder().group("系统信息管理").
pathsToMatch(
"/admin/system/**"
).
build();
}

@Bean
public GroupedOpenApi loginAPI() {

return GroupedOpenApi.builder().group("后台登录管理").
pathsToMatch(
"/admin/login/**",
"/admin/info"
).
build();
}

@Bean
public GroupedOpenApi apartmentAPI() {

return GroupedOpenApi.builder().group("公寓信息管理").
pathsToMatch(
"/admin/apartment/**",
"/admin/room/**",
"/admin/label/**",
"/admin/facility/**",
"/admin/fee/**",
"/admin/attr/**",
"/admin/payment/**",
"/admin/region/**",
"/admin/term/**",
"/admin/file/**"
).build();
}
@Bean
public GroupedOpenApi leaseAPI() {
return GroupedOpenApi.builder().group("租赁信息管理").
pathsToMatch(
"/admin/appointment/**",
"/admin/agreement/**"
).build();
}
@Bean
public GroupedOpenApi userAPI() {
return GroupedOpenApi.builder().group("平台用户管理").
pathsToMatch(
"/admin/user/**"
).build();
}

@Bean
public GroupedOpenApi allAPI() {
return GroupedOpenApi.builder().group("全部接口").
pathsToMatch(
"/**"
).build();
}
}

  • Knife4j 页面里会根据 Controller 或 @Tag 自动拆成很多模块

接口2:添加支付方式

1
2
3
4
5
6
// 根据id查询支付类型
@GetMapping("/getPaymentType/{id}")
public Result getPaymentType(@PathVariable Long id) {
PaymentType paymentType = paymentTypeService.getById(id);
return Result.ok(paymentType);
}

接口3:保存或更新支付方式

1
2
3
4
5
6
7
8
9
10
11
// 保存或更新支付方式
@Operation(summary = "保存或更新支付方式")
@PostMapping("saveOrUpdate")
public Result saveOrUpdate(@RequestBody PaymentType paymentType) {
boolean isSuccess = paymentTypeService.saveOrUpdate(paymentType);
if(isSuccess) {
return Result.ok();
} else {
return Result.fail();
}
}
  • @RequestBody PaymentType paymentType 和 PaymentType paymentType的区别

    • @RequestBody PaymentType paymentType: 以json格式传入,不能get请求方式(get无请求体)

    • PaymentType paymentType: 以拼接参数形式传入 ?name=11&payMonthCount=11

接口4:删除支付方式

1
2
3
4
5
6
7
8
9
10
11
// 根据id删除支付方式
@Operation(summary = "根据id删除支付方式")
@DeleteMapping("deleteById")
public Result deletePaymentType(@RequestParam Long id) {
boolean remove = paymentTypeService.removeById(id);
if(remove) {
return Result.ok();
} else {
return Result.fail();
}
}
  • 在PaymentType实体类中,找到逻辑删除属性,添加@TableLogic注解使用逻辑删除

    1
    2
    3
    4
    5
    6
    7
    8
    9
    @Data
    public class BaseEntity implements Serializable {
    ...
    @Schema(description = "逻辑删除")
    @TableLogic
    @TableField("is_deleted")
    private Byte isDeleted;

    }

3. 测试接口

保存支付方式

  • 可以将数据表中update_timecreate_time设置为CURRENT_TIMESTAMP

image-20260702212608902

  • 测试结果:create_time和update_time成功设置为当前时间

image-20260702214639152

image-20260702214720594

根据id删除支付方式

  • 删除后结果:id为10的支付方式的逻辑删除参数被设置为1
image-20260702214946537

更新支付方式

image-20260702215413553

  • 更新后结果:Id为12的支付方式的name被更新

image-20260702215446925

查询全部支付方式

  • 可以查询到所有未被删除的支付方式

image-20260702215628999

统一返回结果

前后端分离项目中,统一接口返回数据格式,让前端不用适配各种五花八门的返回结构,所有接口都返回相同模板,前端统一解析。

  1. 创建统一返回结果类Result
  2. 向统一返回结果类设置返回数据
1
2
3
4
5
6
7
8
9
10
11
12
// 查询所有支付类型
@GetMapping("list")
public Result listPaymentType(){
List<PaymentType> list = paymentTypeService.list();

/*Result result = new Result();
result.setCode(200);
result.setMessage("成功");
result.setData(list);
return result;*/
return Result.ok(list);
}
  • Result类如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    package com.atguigu.lease.common.result;

    import lombok.Data;

    /**
    * 全局统一返回结果类
    */
    @Data
    public class Result<T> {

    //返回码
    private Integer code;

    //返回消息
    private String message;

    //返回数据
    private T data;

    public Result() {
    }

    private static <T> Result<T> build(T data) {
    Result<T> result = new Result<>();
    if (data != null)
    result.setData(data);
    return result;
    }

    public static <T> Result<T> build(T body, ResultCodeEnum resultCodeEnum) {
    Result<T> result = build(body);
    result.setCode(resultCodeEnum.getCode());
    result.setMessage(resultCodeEnum.getMessage());
    return result;
    }


    public static <T> Result<T> ok(T data) {
    return build(data, ResultCodeEnum.SUCCESS);
    }

    public static <T> Result<T> ok() {
    return Result.ok(null);
    }

    public static <T> Result<T> fail() {
    return build(null, ResultCodeEnum.FAIL);
    }
    }