房间租期管理+标签管理+配套管理
房间租期管理
1. 找到对应的数据库
- 房间租期数据库表
lease_term
2. 设计接口
接口1:查询全部租期列表
1 |
|
接口2:保存或更新租期信息
1 |
|
接口3:根据ID删除租期
1 |
|
标签管理
1. 找到对应的数据库
- 标签管理数据库表
label_info
2. 设计接口
接口1:(根据类型)查询标签列表
1 |
|
1 | 2026-07-04T19:30:48.558+08:00 WARN 20000 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'com.atguigu.lease.model.enums.ItemType'; Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam com.atguigu.lease.model.enums.ItemType] for value '2'] |
原因:传递参数值为1或2,而方法需要美剧类型ItemType,1/2转换成枚举类型时出错
解决方法:
- 方式一:不使用枚举类型,使用String或Integer
- 方式二:编写转换器,将1/2转换成枚举类型
自定义转换器解决:
- 前端传递 type 参数时,通过 SpringMVC 的 WebDataBinder 或 Converter,将请求参数转换为对应的枚举类型。
- 数据库查询时,通过 MyBatis 的 TypeHandler,将枚举对象中的 type 值转换为数据库字段值,从而拼接 SQL 条件进行查询。
代码实现:
定义转换器:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class StringToBaseEnumConverterFactory implements ConverterFactory<String, BaseEnum> {
public <T extends BaseEnum> Converter<String, T> getConverter(Class<T> targetType) {
return new Converter<String, T>() {
public T convert(String code) {
T[] enumConstants = targetType.getEnumConstants();
for (T enumConstant : enumConstants) {
if (enumConstant.getCode().equals(Integer.valueOf(code))) {
return enumConstant;
}
}
throw new IllegalArgumentException("参数非法");
}
};
}
}StringToBaseEnumConverterFactory可以让所有“实现了BaseEnum接口的枚举类”都支持转换。注册转换器:
1
2
3
4
5
6
7
8
9
10
11
public class WebMvcConfiguration implements WebMvcConfigurer {
private StringToBaseEnumConverterFactory stringToBaseEnumConverterFactory;
public void addFormatters(FormatterRegistry registry) {
registry.addConverterFactory(stringToBaseEnumConverterFactory);
}
}为什么要注册?
答:StringToBaseEnumConverterFactory类只是定义了一个转换规则,但 SpringMVC 默认不一定知道什么时候用它。所以要“注册”,就是告诉 SpringMVC:以后 Controller 接收参数时,如果发现前端传的是 String,但方法参数需要 BaseEnum 类型的枚举,就用这个转换器来转换。
ItemType枚举类
1
2
3
4
5
6
7
8
9
10public enum ItemType implements BaseEnum {
APARTMENT(1, "公寓"),
ROOM(2, "房间");
@EnumValue
@JsonValue
private Integer code;
private String name;
...
}@EnumValue注解:告诉 MyBatis-Plus,数据库里存这个枚举的 code 值。@JsonValue注解:告诉 Spring Boot 返回 JSON 时,这个枚举用 code 表示。
接口2:新增或修改标签信息
1 |
|
接口3:根据id删除标签信息
1 |
|
配套管理
1. 找到对应的数据库
- 配套管理数据库表facility_info
2. 设计接口
接口1:[根据类型]查询配套信息列表
1 |
|
接口2:新增或修改配套信息
1 |
|
接口3:根据id删除配套信息
1 |
|
基本属性管理

1. 找到对应的数据库
- 基本属性名称表
attr_key - 基本属性值表
attr_value
2. 设计接口
*** 接口1:查询全部属性名称和属性值列表**
1. 拓展实体类
- 由于返回结果包含多张表数据,创建vo类型用于封装最终结果
1 |
|
1 |
|
2. Controller
1 |
|
3. Service
1 | public interface AttrKeyService extends IService<AttrKey> { |
4. ServiceImpl
1 |
|
Mapper.java
1 |
|
Mapper.xml
1 | <mapper namespace="com.atguigu.lease.web.admin.mapper.AttrKeyMapper"> |
拓展
- 多领域模型介绍
- POJO : POJO指的是普通的Java对象,没有任何特殊限制或要求,不依赖于特定的框架或接口。它通常用于表示简单的数据对象,只包含私有字段、对应的
getter和setter方法以及一些业务逻辑。 - Entity: Entity表示系统中具有独特身份的业务对象。它通常映射到数据库表中的记录,有唯一的标识符(ID)并包含与业务相关的数据和行为。
- VO: VO(Value Object)是一种用于表示值的对象,通常是不可变的,只包含数据而没有业务行为。它用于封装一组相关的数据,常用于传递数据结构。
- POJO : POJO指的是普通的Java对象,没有任何特殊限制或要求,不依赖于特定的框架或接口。它通常用于表示简单的数据对象,只包含私有字段、对应的
接口2:根据id删除属性值
1 |
|
*** 接口3:根据id删除属性名称**
1 |
|
接口4:新增或更新属性名称
1 |
|
接口5:新增或更新属性值
1 |
|
公寓杂费管理
同基本属性管理
1. 找到对应的数据库
- 杂费名称表
fee_key - 杂费值表
fee_value
2. 设计接口
接口1:查询全部杂费名称和杂费值列表
1. 拓展实体类
1 |
|
1 |
|
2. Controller
1 |
|
3. Service
1 | public interface FeeKeyService extends IService<FeeKey> { |
4. ServiceImpl
1 |
|
5. Mapper.java
1 |
|
6. Mapper.xml
1 | <mapper namespace="com.atguigu.lease.web.admin.mapper.FeeKeyMapper"> |
接口2:根据id删除杂费值
1 |
|
接口3:根据id删除杂费名称
1 |
|
接口4:保存或更新杂费名称
1 |
|
接口5:保存或更新杂费值
1 |
|
地区信息管理
1. 概述
实现省市区三级联动

对应数据库:
- 省信息表
province_info - 市信息表
city_info - 区信息表
district_info
- 省信息表
2. 设计接口

接口1:查询所有省
1 |
|
接口2:查询省下的所有市
1 |
|
接口3:查询市下的所有区
1 |
|
导入图片
1. 引入依赖
1 | <dependency> |
2. 添加配置信息
1 | minio: |
3. 创建类读取配置文件内容
1 |
|
4. 创建配置类将Minio交给Spring管理
1 |
|
拓展:
@EnableConfigurationProperties让某个配置属性类生效,把配置文件中的内容绑定到 Java 对象上。
5. 设计接口
- FileUploadController
1 |
|
6. Service
7. ServiceImpl
1 |
|
文件上传大小
1 | spring: |
统一异常处理
问题演示
目前接口如果出现异常:

现在需要统一异常处理,就算出现异常也要返回Result格式数据

添加GlobalExceptionHandler
1 |
|

公寓管理
1. 数据库表
- apartment_info:公寓基本信息表
- apartment_facility:公寓配置信息数据表
- apartment_label:公寓标签数据
- apartment_fee_value:公寓杂费管理
- graph_info:图片表
2. 设计接口
接口1:保存或更新公寓信息
1. 拓展实体类
1 |
|
2. Controller
1 |
|
Service
1 | public interface ApartmentInfoService extends IService<ApartmentInfo> { |
ServiceImpl
1 |
|
接口2:根据条件分页查询公寓列表
1. 配置分页插件
1 |
|
2. Controller
1 |
|
3. Service
1 | IPage<ApartmentItemVo> selectApartmentInfoPage( Page<ApartmentItemVo> page, ApartmentQueryVo queryVo); |
4. ServiceImpl
1 |
|
5. Mapper
1 | <mapper namespace="com.atguigu.lease.web.admin.mapper.ApartmentInfoMapper"> |
测试时开启参数功能
1
2springdoc:
default-flat-param-object: true
接口3:根据ID获取公寓详细信息
Controller
1 |
|
Service
1 | public interface ApartmentInfoService extends IService<ApartmentInfo> { |
ServiceImpl
1 |
|
Mapper
省略
接口4:根据ID删除公寓详细信息
Controller
1 |
|
Service
1 | void removeApartmentInfo(Long id); |
ServiceImpl
1 |
|
接口5:根据ID修改公寓发布状态
Controller
1 |
|
接口6: 根据区县ID查询公寓信息列表
1 |
|
