# springboot-aop-quickstart **Repository Path**: jacobzp/springboot-aop-quickstart ## Basic Information - **Project Name**: springboot-aop-quickstart - **Description**: 复习spring-aop的知识,借助黑马程序员的javaweb课程中的spring-aop课程复习。该仓库用于测试aop的知识。 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-10-04 - **Last Updated**: 2026-10-05 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Spring Boot AOP 快速入门 基于 Spring Boot 3 的部门管理示例,演示如何用 AOP 在不修改业务代码的情况下织入通知,并统计 Service 方法耗时。 ## 技术栈 - Java 17 - Spring Boot 3.2.8 - Spring AOP - MyBatis 3.0.3 - MySQL - Lombok ## 项目结构 ```text src/main/java/com/jacobzp ├── SpringbootAopQuickstartApplication.java 启动类 ├── aop │ ├── MyAspect01.java 五种通知示例 │ └── RecordTimeAspect.java 环绕通知统计方法耗时 ├── controller/DeptController.java 部门接口 ├── service 部门业务 ├── mapper/DeptMapper.java MyBatis 注解 SQL └── pojo Dept、统一响应 Result ``` ## AOP 说明 两个切面都拦截 `com.jacobzp.service.impl` 包下的全部方法。 | 类 | 作用 | | --- | --- | | `MyAspect01` | 演示 `@Before`、`@Around`、`@After`、`@AfterReturning`、`@AfterThrowing` 的执行时机 | | `RecordTimeAspect` | 用 `@Around` 记录目标方法执行耗时并输出日志 | 调用任意部门接口时,Service 方法会被这两个切面增强。 ## 接口 基础路径:`/depts` | 方法 | 路径 | 说明 | | --- | --- | --- | | GET | `/depts` | 查询全部部门 | | GET | `/depts/{id}` | 按 ID 查询 | | POST | `/depts` | 新增部门,请求体为 JSON | | PUT | `/depts` | 更新部门,请求体为 JSON | | DELETE | `/depts?id={id}` | 删除部门 | 统一响应格式: ```json { "code": 1, "msg": "success", "data": null } ``` `code` 为 `1` 表示成功,`0` 表示失败。 ## 数据库 在 MySQL 中创建库 `tlias` 和表 `dept`: ```sql create database if not exists tlias default charset utf8mb4; use tlias; create table if not exists dept ( id int primary key auto_increment comment '主键', name varchar(50) not null comment '部门名称', create_time datetime comment '创建时间', update_time datetime comment '更新时间' ); ``` 数据源配置在 `src/main/resources/application.yml`,按本地 MySQL 账号修改连接信息。 ## 运行 ```bash mvn spring-boot:run ``` 默认端口为 `8080`。启动后访问 `http://localhost:8080/depts` 即可查询部门列表。