目錄
- 1. 常用易混淆注解解釋
- 1.1 @Resource和@Autowired注解的區別
- 1.2 @PathVariable和@RequestParam注解的區別
- 2. Mybatis-Plus高級特性
- 2.1 強大的通用CRUD接口
- 2.2 代碼生成器
- 3. IDEA實用快捷鍵
- 4. 前后端聯調關鍵點
- 4.1 代碼示例
- 4.2 聯調要點
- 4.3 調試技巧
1. 常用易混淆注解解釋
1.1 @Resource和@Autowired注解的區別
在Spring框架中,@Resource
和@Autowired
都是用于依賴注入的注解,但有以下關鍵區別:
特性 | @Autowired | @Resource |
---|---|---|
來源 | Spring框架 | Java標準(JSR-250) |
注入方式 | 默認按類型(byType) | 默認按名稱(byName) |
必需性 | 默認必須(可設required=false) | 非必須 |
指定名稱 | 需配合@Qualifier | 直接使用name屬性 |
構造函數注入 | 支持 | 不支持 |
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import javax.annotation.Resource;// 服務接口
interface PaymentService {void pay();
}// 實現類1
@Service("wechatPay")
class WechatPay implements PaymentService {public void pay() {System.out.println("微信支付");}
}// 實現類2
@Service("aliPay")
class AliPay implements PaymentService {public void pay() {System.out.println("支付寶支付");}
}// 客戶端類
@Service
class ShoppingCart {/* 最常用場景對比 */// 1. 按類型注入(默認方式)@Autowired // Spring方式:存在多個實現時會報錯private PaymentService typeInjectedService;@Resource // Java標準方式:會退化成按名稱注入(變量名作為bean名稱)private PaymentService resourceInjectedService;// 2. 按名稱注入(解決多個實現問題)@Autowired@Qualifier("wechatPay") // Spring方式:需要兩個注解private PaymentService qualifiedService;@Resource(name = "aliPay") // Java標準方式:一個注解搞定private PaymentService namedResourceService;// 3. 構造函數注入(推薦方式)private final PaymentService constructorInjected;@Autowired // 唯一支持構造器注入的方式(Spring 4.3+可省略)public ShoppingCart(PaymentService constructorInjected) {this.constructorInjected = constructorInjected;}/* 實際使用示例 */public void checkout(int paymentType) {switch(paymentType) {case 1:qualifiedService.pay(); // 明確使用微信支付break;case 2:namedResourceService.pay(); // 明確使用支付寶break;default:constructorInjected.pay(); // 使用默認注入的實現}}
}
1.2 @PathVariable和@RequestParam注解的區別
這兩個注解都用于從HTTP請求中獲取參數,但使用場景不同:
特性 | @PathVariable | @RequestParam |
---|---|---|
參數位置 | URL路徑部分 | URL查詢字符串 |
示例URL | /user/{id} | /user?id=123 |
是否必需 | 默認必需 | 可選(可設required=false) |
多值處理 | 不支持 | 支持(數組/集合) |
// @PathVariable 示例
@GetMapping("/users/{userId}")
public User getUser(@PathVariable String userId) {// ...
}// @RequestParam 示例
@GetMapping("/users")
public User getUser(@RequestParam(required = false) String name) {// ...
}
2. Mybatis-Plus高級特性
Mybatis-Plus在Mybatis基礎上提供了諸多便利功能:
2.1 強大的通用CRUD接口
內置通用Mapper,無需編寫簡單SQL
分類 | 方法示例 | 說明 |
---|---|---|
插入 | insert(T entity) | 插入一條記錄 |
刪除 | deleteById(Serializable id) | 根據ID刪除 |
deleteBatchIds() | 批量刪除(根據ID集合) | |
更新 | updateById(T entity) | 根據ID更新 |
update(entity, wrapper) | 根據條件更新 | |
查詢 | selectById() | 根據ID查詢 |
selectOne() | 查詢一條記錄(結果多條會報錯) | |
selectList() | 查詢列表 | |
selectCount() | 查詢總數 | |
分頁 | selectPage() | 分頁查詢 |
2.2 代碼生成器
一鍵生成Entity、Mapper、Service等
3. IDEA實用快捷鍵
功能 | Windows快捷鍵 |
---|---|
全局搜索 | Double Shift |
當前文件查找 | Ctrl+F |
在所有文件/模塊/文件夾查找 | Ctrl+Alt+F |
代碼格式化 | Ctrl+Alt+L |
重寫方法 | Ctrl+O |
重寫/實現方法 | Ctrl+I |
AI智能補全 | Tab |
輸入表達式后使用此后綴快速生成變量 | .var |
4. 前后端聯調關鍵點
4.1 代碼示例
// 正確示例 - 參數名與后端一致
methods: {deleteInterviewer(interviewerId) {...}).then(() => {var params = {interviewerId: interviewerId}
deleteInterviewer: function(params) {return instance({url: '/interviewer/delete',method: 'delete',params: params})
}
后端接口:
@DeleteMapping("/delete")
public Result delete(@RequestParam String interviewerId) {// 參數名必須與前端一致service.delete(interviewerId);return Result.ok();
}
4.2 聯調要點
- 命名一致性:前后端參數名嚴格一致
- 請求方式匹配:
- GET - 查詢
- POST - 創建
- PUT - 更新
- DELETE - 刪除
- 數據格式:
- 明確約定JSON字段命名風格(駝峰/下劃線)
- 日期格式統一(如yyyy-MM-dd HH:mm:ss)
4.3 調試技巧
使用Postman測試接口
開啟SpringBoot的SQL日志:
mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl