@RequestMapping
注解在類上:表示該類中所有響應請求的方法都以此地址為父路徑
value(path) 指定請求的實際訪問地址,默認@RequestMapping(“url”)的值url即為value的值。指定的地址可以是 URI Template 模式。
method 指定請求的method類型,主要有 GET、POST、DELETE、PUT等;
params 指定request中必須包含某些參數值,包含才讓該方法處理請求。
headers 指定request中必須包含某些指定的header值,包含才能讓該方法處理請求。
consumes 指定處理請求的提交內容類型(Content-Type),例如:application/json, text/html;
produces 指定返回的內容類型,當且僅當request請求頭中的(Accept)類型中包含該指定類型才返回;
@RequestMapping("/emps")
public interface EmpController
注解在方法上:表示映射請求路徑的具體信息,即訪問該方法的訪問路徑為注解的value值
可以被以下這些注解替代
@GetMapping
使用get方式請求
get請求注解主要用于增刪改查的查
@PutMapping
使用put方式請求
put請求注解主要用于增刪改查的改
@DeleteMapping
使用delete方式請求
delete請求注解主要用于增刪改查的刪
@PostMapping
使用post方式請求
post請求注解主要用于增刪改查的增
@RequestMapping("/testRequestMapping")public String testRequestMapping(){return "success";}
@RequestParam
將請求參數綁定到控制器的方法參數上
@RequestParam("映射參數")
@DateTimeFormat
日期格式化轉換參數
將日期類型的數據以pattern形式解析
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin
@RequestBody
用對象形式接收JSON類型參數
Result insert(@RequestBody Emp emp);
@PathVariable
用指定類型來接收寫在路徑中的參數
@DeleteMapping("/{ids}")
Result delete(@PathVariable List<Integer> ids);
@ResponseBody
包含在@RestController中
如果沒有這個注解,控制類的返回值會解析為跳轉一個路徑
加了注解后如果返回對象會解析為json
如果返回字符串會返回字符串
返回在響應體中
@ResponseBody
@RequestMapping(value = "/loginIn", produces = "application/json;charset=UTF-8")
public Object login(UserBean user)
@Component 聲明bean的基礎注解,將該類的對象交給 IOC 容器管理
@Controller ‘@Component’ 的衍生注解, 標注在控制層類上
@Service ‘@Component’ 的衍生注解, 標注在業務層類上
@Repository ‘@Component’ 的衍生注解, 標注在持久層類上
以上注解功能一樣,都是標記一個類為組件,讓spring在程序啟動時掃描并加載這些組件,將這些類的對象交給IOC管理,在其他類中可以使用@Autowired注入
@Autowired 用來獲取 IOC 容器中管理的對象,獲取的是對象所在實現類實現的接口類型
@Service
public class DeptServiceImpl implements DeptService
@Autowired
DeptService deptService;
@ComponentScan
包含在@SpringBootApplication中,在啟動類上注解
批量注冊bean,默認掃描注解修飾的類所在的包,默認對
@Component 聲明bean的基礎注解,將該類的對象交給 IOC 容器管理
@Controller ‘@Component’ 的衍生注解, 標注在控制層類上
@Service ‘@Component’ 的衍生注解, 標注在業務層類上
@Repository ‘@Component’ 的衍生注解, 標注在持久層類上
帶有這些注解的類進行掃描,交給IOC容器管理
@SpringBootApplication
@ServletComponentScan
public class TliasApplication {public static void main(String[] args) {SpringApplication.run(TliasApplication.class, args);}}
@Primary
設置多個相同接口的實現類對象中的一個為主要對象,注入時會優先注入此對象
@Primary
@Service
public class EmpServiceImpl implements EmpService
@Qualifier
和@Component及其衍生注解一起使用對提交給IOC的對象進行標注
@Service
@Qualifier("test")
public class EmpServiceImpl implements EmpService
和@Autowired一起使用對被標注的對象進行注入
@Autowired
@Qualifier("test")
EmpService empService;
@Resource
與@Qualifier類似,但@Qualifier由Spring提供且在value屬性中標記,而@Resource由Java提供且在name屬性中標記
@Service
@Resource(name = "test")
public class EmpServiceImpl implements EmpService
@Data
由lombok工具類庫提供
自動實現類的get、set、equals、hashCode、toString方法
@NoArgsConstructor
lombok下給實體類生成空參構造
@AllArgsConstructor
lombok下給實體類生成全參構造
@Param
在mapper層中將參數進行映射到sql語句中
@Select("select * from emp where id = #{id}")
Emp selectById(@Param("id") Integer id);
@ConfigurationProperties
將配置文件封裝成實體類,需要時直接使用@Autowired注入使用
@Data
@Component
@ConfigurationProperties(prefix = "aliyun.oss") //指定配置文件
public class AliOSSProperties {private String endpoint;private String accessKeyId;private String accessKeySecret;private String bucketName;
}
@Value
將配置文件中的屬性注入
@Value("${aliyun.oss.endpoint}")
private String endpoint;
@ServletComponentScan
在啟動類上加此注解可以令Servlet,Filter,Listener三大組件直接通過@WebServlet,@WebFilter,@WebListener注解自動注冊,無需其他代碼
@SpringBootApplication
@ServletComponentScan
public class TliasApplication
@RestControllerAdvice
ControllerAdvice+ResponseBody
@Transactional
使方法被事務管理
@Aspect
聲明切面類
@Order(數字)
控制通知的順序,方法執行前按升序執行,方法執行后按降序執行,類似于棧結構
@Around
//此注解標注的通知方法在目標方法前后都執行
//需要自己調用ProceedingJoinPoint.proceed()方法來讓目標方法執行
@Before
//此注解標注的通知方法在目標方法前被執行
@After
//此注解標注的通知方法在目標方法后被執行,無論是否存在異常
(在finally塊中)
@AfterReturning
//同上,但有異常時不會執行,后于@After執行
@AfterThrowing
//在發生異常后執行
通知注解的屬性:
execution
上述注解的屬性,通過((訪問修飾符) 返回值 包名.類名.方法名(方法參數) throws 異常)來對方法進行匹配,和@annotation 功能類似
@annotation()
匹配方法上的注解(需要自己自定義注解,將全限定名寫在括號中)
@annotation通過方法上的注解進行過濾
args()
匹配方法的參數類型
@Around("@annotation(appendAnnotation) && args(Integer)")
@annotation(com.itheima.anno.Log)
@PointCut
通過@PointCut注解,可以抽取一個切入點表達式,然后在其他的地方就可以通過類似于方法調用 的形式來引用該切入點表達式
@Pointcut("execution(* com.itheima.service.impl.DeptServiceImpl.*(..))")public void pt(){}@Around("pt()")public Object recordTime(ProceedingJoinPoint joinPoint) throws Throwable {long begin = System.currentTimeMillis();//調用原始操作Object result = joinPoint.proceed();long end = System.currentTimeMillis();log.info("執行耗時 : {} ms", (end-begin));return result;}