提交 696209e2 authored 作者: 黄夏豪's avatar 黄夏豪

Merge remote-tracking branch 'origin/master'

# AOP
面向切面编程
### 实现方法:
写一个切面类,在类上使用注解@Aspect实现
给该类设计6个方法,分别使用6个注解:
①切入点:连接点,扫描到具体哪个包哪个类哪个方法
`@Pointcut("execution(public * com.kzj.kzj_rabbitmq.controller..*.*(..))")`
*execution(访问权限 返回类型 包名.类名.方法名(参数))*
②前置增强方法,相当于BeforeAdvice
`@Before("Pointcut()")`
③final增强,不管是抛出异常或者正常退出都会执行
`@After("Pointcut()")`
④后置增强,相当于AfterReturningAdvice,方法退出时执行
`@AfterReturning(value="Pointcut()",returning="result")`
⑤异常抛出增强,相当于ThrowsAdvice
`@AfterThrowing(value="Pointcut()",throwing="e")`
⑥环绕增强,相当于MethodInterceptor
`@Around("Pointcut()")`
执行顺序:**@Around**===>**@Before**===>**方法调用**===>**@Around**===>**@After**===>**@AfterReturning**
### 动态代理
\ No newline at end of file
# JPA
Springboot整合JPA
实体类
```
@Entity
public class User {
@Id
@GeneratedValue
private Long id;`
@Column(name = "name", nullable = true, length = 20)
private String name;
@Column(name = "agee", nullable = true, length = 4)
private int age;
}
```
DAO层使用接口继承JpaRepository
```
public interface UserRepository extends JpaRepository<Person, Long> { }
```
## 使用jpa的 CrudRepository 基本查询
## 使用jpa的 PagingAndSortingRepository 分页查询和排序
## 使用jpa的 Repository 自定义声明式查询方法
## 使用jpa的 JpaRepository 使用hql、jpql或sql查询,@Query等注解
\ No newline at end of file
# Swagger
### 注解说明
**@Api**:用在请求的类上,表示对类的说明
tags="说明该类的作用,可以在UI界面上看到的注解"
value="该参数没什么意义,在UI界面上也看到,所以不需要配置"
**@ApiOperation**:用在请求的方法上,说明方法的用途、作用
value="说明方法的用途、作用"
notes="方法的备注说明"
**@ApiImplicitParams**:用在请求的方法上,表示一组参数说明
**@ApiImplicitParam**:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面
name:参数名
value:参数的汉字说明、解释
required:参数是否必须传
<u>paramType</u>:参数放在哪个地方
· header --> 请求参数的获取:@RequestHeader
· query --> 请求参数的获取:@RequestParam
· path(用于restful接口)--> 请求参数的获取:@PathVariable
· body(不常用)
· form(不常用)
dataType:参数类型,默认String,其它值dataType="Integer"
defaultValue:参数的默认值
**@ApiResponses**:用在请求的方法上,表示一组响应
**@ApiResponse**:用在@ApiResponses中,一般用于表达一个错误的响应信息
code:数字,例如400
message:信息,例如"请求参数没填好"
response:抛出异常的类
**@ApiModel**:用于响应类上,表示一个返回响应数据的信息
(这种一般用在post创建的时候,使用@RequestBody这样的场景,
请求参数无法使用@ApiImplicitParam注解进行描述的时候)
**@ApiModelProperty**:用在属性上,描述响应类的属性
## 异常统一处理
将会利用注解**@ExceptionHandler**
首先自定义异常类:
```
public class BaseException extends Exception {
private int code;
private String message;
public BaseException(String message,int code){
super();
this.code = code;
this.message = message;
}
public BaseException(String message,int code,Throwable e){
super(message,e);
this.message = message;
this.code = code;
}
}
```
第一种思路,设计一个基类:
```
public class BaseController {
@ExceptionHandler
@ResponseBody
public Object expHandler(Exception e){
if(e instanceof SystemException){
BaseException baseException= (BaseException) e;
return baseException.getMessage();
}else{
e.printStackTrace();
return "请求错误";
}
}
}
```
之后所有需要异常处理的**Controller**都继承这个类,从而获取到异常处理的方法。
第二种思路,将上述基类写作接口,更加灵活
第三种思路,在**BaseController**上使用**@ControllerAdvice**注解,可以避免继承
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论