This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vincent.demo; | |
import java.util.*; | |
import java.util.stream.Collectors; | |
public class JavaMain { | |
public static void main(String[] args) { | |
Set<Integer> selectedNumbers = new HashSet<>(); | |
Scanner scan = new Scanner(System.in); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vincent.demo; | |
import java.util.*; | |
public class JavaMain { | |
public static void main(String[] args) { | |
Scanner scan = new Scanner(System.in); | |
System.out.println("請輸入你的年齡"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 找出name欄位值有包含參數的所有文件,且不分大小寫 | |
List<Product> findByNameLikeIgnoreCase(String name); | |
// 找出id欄位值有包含在參數之中的所有文件 | |
List<Product> findByIdIn(List<String> ids); | |
// 是否有文件的email欄位值等於參數 | |
boolean existsByEmail(String email); | |
// 找出username與password欄位值皆符合參數的一筆文件 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 查詢price欄位在特定範圍的文件(參數亦可使用Date) | |
// gte:大於等於;lte:小於等於;Between:大於及小於,兩者略有差異。 | |
@Query("{'price': {'$gte': ?0, '$lte': ?1}}") | |
List<Product> findByPriceBetween(int from, int to); | |
// 查詢name字串欄位有包含參數的文件,不分大小寫 | |
@Query("{'name': {'$regex': ?0, '$options': 'i'}}") | |
List<Product> findByNameLikeIgnoreCase(String name); | |
// 查詢同時符合上述兩個條件的文件 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 回傳id欄位值有包含在參數之中的文件數量 | |
@Query(value = "{'_id': {'$in': ?0}}", count = true) | |
int countByIdIn(List<String> ids); | |
// 回傳是否有文件的id欄位值包含在參數之中 | |
@Query(value = "{'_id': {'$in': ?0}}", exists = true) | |
boolean existsByIdIn(List<String> ids); | |
// 刪除id欄位值包含在參數之中的文件 | |
@Query(delete = true) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RestController | |
@RequestMapping("/products") | |
public class ProductController { | |
@Autowired | |
private ProductService productService; | |
@GetMapping("/{id}") | |
public ResponseEntity<Product> getProduct(@PathVariable("id") String id) { | |
Product product = productService.getProduct(id); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RestController | |
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE) | |
public class ProductController { | |
@GetMapping("/products/{id}") | |
public Product getProduct(@PathVariable("id") String id) { | |
Product product = new Product(); | |
product.setId(id); | |
product.setName("Romantic Story"); | |
product.setPrice(200); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@GetMapping("/products/{id}") | |
public ResponseEntity<Product> getProduct(@PathVariable("id") String id) { | |
Product product = new Product(); | |
product.setId(request.getId()); | |
product.setName(request.getName()); | |
product.setPrice(request.getPrice()); | |
return ResponseEntity.ok().body(product); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RestController | |
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE) | |
public class ProductController { | |
private final List<Product> productDB = new ArrayList<>(); | |
@PostConstruct | |
private void initDB() { | |
productDB.add(new Product("B0001", "Android Development (Java)", 380)); | |
productDB.add(new Product("B0002", "Android Development (Kotlin)", 420)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@PostMapping("/products") | |
public ResponseEntity<Product> createProduct(@RequestBody Product request) { | |
boolean isIdDuplicated = productDB.stream() | |
.anyMatch(p -> p.getId().equals(request.getId())); | |
if (isIdDuplicated) { | |
return ResponseEntity.status(HttpStatus.CONFLICT).build(); | |
} | |
Product product = new Product(); | |
product.setId(request.getId()); |
OlderNewer