Javax
@Resource
javax.annotation.Resource
- Java的注解
- name和type,name属性解析为bean的名称,type属性解析为bean的类型
- 默认的注入方式为
byName
(根据名称进行匹配);type
无法分辨时,可以根据name
分辨,通过name
属性来显式指定Bean名称
。
1 2
| @Resource private UserService service;
|
Spring
@Service
1 2
| @Service public class UserServiceImpl implements UserService {}
|
@Repository
org.springframework.stereotype.Repository
1 2 3
| @Mapper @Repository public interface UserMapper {}
|
@Configuration
org.springframework.context.annotation.Configuration
@Configuration
用于定义配置类,可替换xml配置文件
1 2 3 4 5 6
| ``` ## @Transactional {% note green no-icon %} org.springframework.transaction.annotation.Transactional {% endnote %} ```Java @Transactional public void userCommon(){}
|
@Component
org.springframework.stereotype.Component
@Component
定义Spring管理Bean(将标注@Component
注解的类交由Spring管理)
1 2
| @Component public class UserUtils {}
|
@Autowired
org.springframework.beans.factory.annotation.Autowired
- Spring的注解
- 默认的注入方式为
byType
(根据类型进行匹配);type
无法分辨时,可以根据name
分辨,变量名称
要与Bean名称
一致,也可以通过@Qualifier
注解来显式指定Bean名称
。
- 作用和
@Resource
相似。
1 2
| @Autowired private UserService service;
|
@Qualifier
org.springframework.beans.factory.annotation.Qualifier
- 必须搭配
@Autowired
搭配使用;
- 当需要执行依赖注入时,
@Qualifier
会限定Spring Framework需要选择注入的对象;
- 例如:当项目中一个
Service接口
有多个实现类时,就可以使用@Qualifier
对依赖注入过程进行更详细的控制。
1 2 3 4 5 6 7 8 9
| @Autowired @Qualifier(value = "userService1") private UserService service;
@Service("userService1") public class UserServiceImpl implements UserService {} @Service("userService2") public class UserServiceImpl implements DemoUserService {}
|
@Validated
org.springframework.validation.annotation.Validated
- 对传输的参数进行数据校验的注解,通过配置
@Validation
对数据约束。
@Validated
作用在类、方法和参数上
1 2 3 4 5
| @RequestMapping("/user") @Controller public class UserController { public void getUserInfo(@Validated String name) {} }
|
Lang
@Aspect
org.aspectj.lang.annotation.Aspect
1 2 3
| @Aspect @Component public class UserAspect {}
|
@Pointcut
org.aspectj.lang.annotation.Pointcut
1 2
| @Pointcut("execution(public * com.smart.Aop.controller.*.*(..))") public void aopPointCut(){}
|
@Around
org.aspectj.lang.annotation.Around
1 2 3 4 5 6 7 8
| ```
# MyBatis ## @Mapper {% note green no-icon %} org.apache.ibatis.annotations.Mapper {% endnote %} ```Java @Mapper public interface UserMapper {}
|
@Select
org.apache.ibatis.annotations.Select
1 2 3 4 5
| @Mapper public interface UserMapepr{ @Select("SELECT * FROM sys_user") List<User> getUserList(); }
|
@Param
org.apache.ibatis.annotations.Param
@Param
作用:给Mapper层中SQL的参数赋值
1 2 3 4 5
| @Mapper public interface UserMapepr{ @Select("SELECT * FROM sys_user WHERE id = #{id}") List<User> getUserList(@Param("id") String userId); }
|
Mybatis-plus
@TableName
com.baomidou.mybatisplus.annotation.TableName
@TableName
作用:实现实体类型和数据库中的表实现映射
1 2 3
| @Data @TableName("sysy_user") public class User {}
|
@TableId
com.baomidou.mybatisplus.annotation.TableId
@TableId
作用:将属性所对应的字段指定为主键
- value【属性名】:指定主键的字段,要和数据库表中的属性名一致,要不然最终的查询结果是null
- type【属性】:设置主键生成策略,不指定的话默认雪花算法 (数据库记得勾选自动递增)
1 2 3 4 5
| @Data public class User{ @TableId(value = "id", type = IdType.AUTO) private Integer id; }
|
Lombok
@Data
@Data
作用: 提高代码的简洁,省去代码中大量的get()、set()、toString()等方法
@Data
相当于@Getter
、@Setter
、@RequiredArgsConstructor
、@ToString
、@EqualsAndHashCode
五个注解的合集
1 2
| @Data public class User{}
|
@EqualsAndHashCode
1 2
| @EqualsAndHashCode(callSuper = false) public class User{}
|
@Accessors
lombok.experimental.Accessors
1 2 3
| @Data @Accessors(chain = true) public class User{}
|
@Slf4j
lombok.extern.slf4j.Slf4j
1 2 3 4
| @RequestMapping("/user") @Slf4j @Controller public class UserController {}
|
Swagger
@Api
io.swagger.annotations.Api
1 2
| @Api(tags = "用户Controller层") public class UserController {}
|
@ApiOperation
io.swagger.annotations.ApiOperation
1 2 3 4 5 6 7
| @RequestMapping("/user") @Controller public class UserController { @RequestMapping(value = "/getUserInfo", method = RequestMethod.GET) @ApiOperation(value = "获取用户信息") public void getUserInfo(Integer id) {} }
|
@ApiImplicitParam
io.swagger.annotations.ApiImplicitParam
1 2 3 4 5 6 7
| @RequestMapping("/user") @Controller public class UserController { @RequestMapping(value = "/getUserInfo", method = RequestMethod.GET) @ApiImplicitParam(name="id", value="用户ID") public void getUserInfo(Integer id) {} }
|
@ApiImplicitParams
io.swagger.annotations.ApiImplicitParams
1 2 3 4 5 6 7 8 9 10 11
| @RequestMapping("/user") @Controller public class UserController { @RequestMapping(value = "/getUserInfo", method = RequestMethod.GET) @ApiImplicitParams({ @ApiImplicitParam(name="page", value="页码", required = true), @ApiImplicitParam(name="limit", value="每页数量", required = true) }) public void getUserInfo(Integer id, String name) {} }
|
@ApiModel
io.swagger.annotations.ApiModel
1 2 3
| @Data @ApiModel(value="用户-对象", description="用户") public class User {}
|
@ApiModelProperty
io.swagger.annotations.ApiModelProperty
1 2 3 4 5
| @Data public class User { @ApiModelProperty(value = "名称") private String name; }
|
Jackson
com.fasterxml.jackson.annotation.JsonFormat
@JsonFormat
作用:可用于返回Date日期数据
的时间格式化
1 2 3 4 5
| @Data public class User { @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss", timezone="GMT+8") private Date createTime; }
|