初學SpringBoot 易踩雷區與拆彈指南
踩雷地圖持續更新中
2 min readMar 17, 2021
Maven installation
- Homebrew: Your CLT does not support macOS 11.0
- Maven Installation On Mac | How To Install Maven On Mac | Maven Tutorial For Beginners | Simplilearn
MySQL No permission
MySQL Error: : ‘Access denied for user ‘root’@’localhost’
-> Find my.cnf and revise it (/usr/local/etc/my.cnf)
find /usr/local/ -iname "mysql"
Spring Data JPA (超級大雷)
- 如何解决idea报java.sql.SQLException: Access denied for user ‘’@’localhost’ (using password: NO)
- Spring boot starter parent 2.0.0 not found dependency
- springboot启动报错Error creating bean with name requestMappingHandlerMapping defined in class path resou
出現Bean 相關error 的話可以檢查的地方(之後有遇到不一樣的會再新增上來):
1. Controller 名稱是否重複
2. 有些Class 的註解是否忘了加(例如Service 之類的)
3. 若是Repository 無法Autowired 的話,則要確認執行的Class 跟Repository 是否在同一個package 下
版本問題
Thymeleaf
- Cannot resolve org.thymeleaf:thymeleaf-spring5:3.0.2.RELEASE_蓝墨の学习笔记-CSDN博客
- springboot与thymeleaf版本问题冲突_suxintang_time的博客-CSDN博客
@NotBlank
函式新舊寫法
Spring Data JPA — Select
// Old
public Book findOne(long id) {
return bookRepository.findOne(id);
}// New
public Book findOne(long id) {
return bookRepository.findById(id).orElse(null);
}
Spring Data JPA — Delete
// Old
public void delete(long id) {
bookRepository.delete(id);
}// New
public void delete(long id) {
bookRepository.deleteById(id);
}
Sort
public Page<Book> findAllByPage() {
// Sort sort = new Sort(Sort.Direction.DESC, "id"); OLD
Sort sort = Sort.by(Sort.Direction.DESC, "id");
// Pageable pageable = new PageRequest(1, 5); OLD
Pageable pageable = PageRequest.of(1, 5, sort);
return bookRepository.findAll(pageable);
} //Sort sort = new Sort(Sort.Direction.DESC, "id");
Sort sort = Sort.by(Sort.Direction.DESC, "id");
//Pageable pageable = new PageRequest(1, 5);
Pageable pageable = PageRequest.of(1, 5, sort);
return bookRepository.findAll(pageable);
}