Spring项目常用注解

一、Controller控制器使用注解

1.类注解

@Slf4j : 注解在类上, 为类提供一个属性名为 log 的 log4j 的日志对象

@Log4j : 注解在类上, 为类提供一个属性名为 log 的 log4j 的日志对象

@Api:用在controller上,对controller进行注释

@RestController:相当于@Controller加上@ResponseBody(返回类型非页面)

@RequestMapping:配置类的路径,举例:@RequestMapping("/{userId}/test"),访问举例:/1/test

2.类属性注解

@Autowired 依赖注入Service

3.方法注解

@ApiOperation:用在API方法上,对该API做注释,说明API的作用;

@PostMapping:举例:@PostMapping("/list")

@GetMapping:举例:@GetMapping("/{id}")

4.方法属性注解

@ApiParam 作用在接口方法上,描述单个参数信息,属性基本与@ApiImplicitParam一样,但可以作用在方法、参数、属性上;

@PathVariable 映射 URL 绑定的占位符,举例: @PathVariable("userId") 

@RequestBody 主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的),所以只能发送POST请求。

@ApiIgnore 注解主要作用在方法上,类上,参数上。当作用在方法上时,方法将被忽略;作用在类上时,整个类都会被忽略;作用在参数上时,单个具体的参数会被忽略。

二、ServiceImpl使用注解(Service无注解)

1.类注解

@Service 自动注册到Spring容器

2.类属性注解

@Autowired    依赖注入Repository

3.方法注解

@Override 重写

@Transactional rollbackFor 属性来定义回滚的异常类型,使用 propagation 属性定义事务的传播行为。

PROPAGATION_REQUIRED    支持当前事务,如果当前没有事务,则创建一个事务,这是最常见的选择。默认为此事务。 

PROPAGATION_REQUIRES_NEW    新建事务,如果当前存在事务,就把当前事务挂起。

举例:下面1情况是开始方法,2情况是1调2

@Transactional(rollbackFor = {Exception.class}, propagation = Propagation.REQUIRED)

@Transactional(rollbackFor = {Exception.class}, propagation = Propagation.REQUIRES_NEW)

@Async    申明该方法是一个异步任务,启动主类必须加@EnableAsync注解

三、RepositoryImpl使用注解(Repository无注解)

1.类注解

@Component 泛指各种组件

2.类属性注解

@Autowired    依赖注入Mapper

3.方法注解

@Override 重写

四、Mapper使用注解

1.方法属性注解

@Param 参数命名,举例: @Param("id")

五、实体类注解

1.类注解

@ApiModel 用在实体类上,对实体类做注释(value:字符串,模型的简短别名,使得在文档的导航中便于识别;description:字符串,模型的附加描述),举例: @ApiModel("测试")

@Data    setter、getter方法

@AllArgsConstructor 所有参数构造器

@NoArgsConstructor 无参数的构造器

@ToString(callSuper = true)    重写toSting方法(将父类中的属性也算到tostring中)

@EqualsAndHashCode(callSuper = false) 重写equalsAndhashCode方法(若为true将父类中的属性也算到tostring中)

@Table 声明此对象映射到数据库的数据表,举例: @Table(name = "test")

2.类属性注解

@ApiModelProperty:用在属性上,对属性做注释,举例:@ApiModelProperty("表ID,主键,供其他表做外键")

@Id 解用于声明一个实体类的属性映射为数据库的主键列

@GeneratedValue 注解用于标注主键的生成策略 默认情况下,JPA自动选择一个最适合低层数据库的主键生成策略:sqlServer对应的identity,mySql对应 auto increment

@Transient 实体类中使用@Table注解后,想要添加数据库表中不存在的字段,就要使用@Transient这个注解。

@Length 属性值长度,举例:@Length(max = 80)

@NotNull 不能为null,但可以为empty

@NotEmpty 不能为null,而且长度必须大于0

@NotBlank 只能作用在String上,不能为null,而且调用trim()后,长度必须大于0

@Valid 验证集合里面的对象,属性为list需要添加

@JsonFormat Date类型get方法加注解,举例:@JsonFormat(pattern = "yyyy-MM-dd")

@DateTimeFormat Date类型set方法加注解,举例:@DateTimeFormat(pattern = "yyyy-MM-dd")

@JsonSerialize 用于数据转换,该注解作用在该属性的getter()方法上,举例:@JsonSerialize(using = ToStringSerializer.class) 让系统序列化时,保留相关精度。

六、引入jar

1.RequestMapping等注解

<dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-web</artifactId>

    <version>5.0.16.RELEASE</version>

</dependency>

2.Transactional注解

<dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-tx</artifactId>

    <version>5.0.16.RELEASE</version>

</dependency>

3.ToStringSerializer类和JsonSerialize注解

<dependency>

    <groupId>com.fasterxml.jackson.core</groupId>

    <artifactId>jackson-databind</artifactId>

    <version>2.9.10.8</version>

</dependency>

4.Param注解

<dependency>

    <groupId>org.mybatis</groupId>

    <artifactId>mybatis</artifactId>

    <version>3.5.7</version>

</dependency>

5.Length等注解

<dependency>

    <groupId>org.hibernate.validator</groupId>

    <artifactId>hibernate-validator</artifactId>

    <version>6.0.13.Final</version>

</dependency>

6.Table等注解

<dependency>

    <groupId>org.hibernate.javax.persistence</groupId>

    <artifactId>hibernate-jpa-2.1-api</artifactId>

    <version>1.0.2.Final</version>

</dependency>

7.swagger注解

<dependency>

    <groupId>io.springfox</groupId>

    <artifactId>springfox-swagger2</artifactId>

    <version>2.7.0</version>

</dependency>

<dependency>

    <groupId>io.springfox</groupId>

    <artifactId>springfox-swagger-ui</artifactId>

    <version>2.7.0</version>

</dependency>

8.lombok注解

<dependency>

    <groupId>org.projectlombok</groupId>

    <artifactId>lombok</artifactId>

    <version>1.16.8</version>

</dependency>

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

linsa_pursuer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值