目录
一、@Param
注解是为SQL语句中参数赋值而服务的,给参数命名,不使用@Param注解时,参数只能有一个
public User selectUser(@Param("userName") String name,@Param("password") String pwd);
二、@RequsetParam
前端提交的form表单数据的name属性和方法中的参数名不一致时,springMVC就无法自动封装参数,所以需要@RequestParam(前端name属性名称)来指定前端提交的表单的name属性的名称
public Map findsPage(HttpSession session, @RequestParam(value = "pageNo", defaultValue = "1",required = false) Long pageNo){}
三、@ResponseBody
@ResponseBody的作用其实是将java对象转为json格式的数据。
四、@RequestMapping
@RequestMapping给出外界访问方法的路径,或者说触发路径 ,触发条件
五、@SuppressWarnings
屏蔽一些无关紧要的警告
示例:
· @SuppressWarnings(“unchecked”)
告诉编译器忽略 unchecked 警告信息,如使用List,ArrayList等未进行参数化产生的警告信息。@SuppressWarnings(“all”)
抑制所有类型的警告
· @SuppressWarnings(“serial”)
如果编译器出现这样的警告信息:The serializable class WmailCalendar does not declare a static final serialVersionUID field of type long,使用这个注释将警告信息去掉。
· @SuppressWarnings(“deprecation”)
如果使用了使用@Deprecated注释的方法,编译器将出现警告信息。使用这个注释将警告信息去掉。
· @SuppressWarnings(“unchecked”, “deprecation”)
告诉编译器同时忽略unchecked和deprecation的警告信息。
· @SuppressWarnings(value={“unchecked”, “deprecation”})
等同于@SuppressWarnings(“unchecked”, “deprecation”)
六、@FunctionalInterface
函数式接口 :一个接口只有一个抽象方法,实现Lambda关键
@FunctionalInterface
interface NoParameterNoReturn {
//注意:只能有一个方法
void test();
}
七、@JsonFormat
日期格式转为中文时间
pattern: 表示日期的格式
timezone: 默认是GMT,中国需要GMT+8
locale: 根据位置序列化的一种格式
@JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
八、@PathVariable
RestFul风格,接收多个参数
@PostMapping("/student/{id}") public int del(@PathVariable("id") Integer id)
九、@EnableScheduling
@EnableScheduling:加在类上方,表示开启定时任务
@Component
@EnableScheduling
public class ScheduledDemo {....}
十、@Scheduled
@Scheduled:加在方法上,设定定时时间fixedRate
@Slf4j
@Component
@EnableScheduling
public class ScheduledDemo {
@Scheduled(initialDelay = 5000,fixedRate = 10000)
public void fixedRate() throws InterruptedException {
log.info("项目启动时延迟5秒才执行第一次,在第一次执行后每过10秒固定执行一次,不受方法执行耗时影响");
Thread.sleep(2000);
}
}
十一、@Slf4j
@Slf4j:打印日志,需要导入lombok
@Slf4j
public class log{
log.info("=============打印日志=============");
}
十二、@PostConstruct
@PostConstruct:项目启动时就执行一次
@PostConstruct
@Scheduled(initialDelay = 5000,fixedRate = 10000)
public void fixedRate() throws InterruptedException {
log.info("项目启动时延迟5秒才执行第一次,在第一次执行后每过10秒固定执行一次,不受方法执行耗时影响");
Thread.sleep(2000);
}
十三、@EnableAsync
@EnableAsync:写在类上,将类改为异步
十四、@Async
@Async:将方法改为异步,会找到对应的@Bean相同的名字的方法
相同类中的方法调用带@Async的方法是无法异步的,这种情况仍然是同步。
十五、@ConfigurationProperties
只要在 Bean 上添加上了这个注解,指定好配置文件的前缀,那么对应的配置文件数据就会自动填充到 Bean 中。适用于复用
config.username=jay.zhou
config.password=3333
SpringBoot项目中使用@ConfigurationProperties的Bean,它的username与password就会被自动注入值了。就像下面展示的那样
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "config")
public class TestBean{
private String username;
private String password;
}
十六、@Value
适用于单个属性
minio:
endpoint: http://192.168.1.108:9000
accessKey: gUNGlEvT7Q6EKxl7
@Component
public class MinIoClientConfig {
@Value("${minio.endpoint}")
private String endpoint;
@Value("${minio.accessKey}")
private String accessKey;
}
十七、@Nullable
@Nullable 注解可以使用在方法、属性、参数上,分别表示方法返回可以为空、属性值可以为空、参数值可以为空。
十八、@MapperScan
扫描dao/mapper层
@MapperScan("com.yixin.mybatis_plus.mapper")
十九、@SneakyThrows
@SneakyThrows直接把捕获异常的代码嵌入到了class文件里,不需要在手动写异常
1、@SneakyThrows
2、@SneakyThrows(InterruptedException.class)
二十、@Retention
@Retention(RetentionPolicy.RUNTIME)
首先要明确生命周期长度 SOURCE < CLASS < RUNTIME ,所以前者能作用的地方后者一定也能作用。一般如果需要在运行时去动态获取注解信息,那只能用 RUNTIME 注解;如果要在编译时进行一些预处理操作,比如生成一些辅助代码(如 ButterKnife),就用 CLASS注解;如果只是做一些检查性的操作,比如 @Override 和 @SuppressWarnings,则可选用 SOURCE 注解。
二十一、@ModelAttribute
public String loginCheck(@ModelAttribute("user") User user) {
“用在方法的参数上 注解在参数上,会将客户端传递过来的参数按名称注入到指定对象中, 并且会将这个对象自动加入ModelMap中”