๋ฐ์ํ
๊ฐ๋ฐํ๊ฒฝ : IntelliJ IDEA
, H2 DataBase
, JAVA 11
์คํ๋ง ๋ถํธ ์คํํฐ ์ฌ์ดํธ์์ ์คํ๋ง ํ๋ก์ ํธ ์์ฑ
https://start.spring.io
- ํ๋ก์ ํธ ์ ํ
- Project : Gradle Project
- Spring Boot : 2.3.x
- Language : Java
- Java : 11
-
Project Metadata
- groupId : hello
- artifactId: hello-spring
- Dependencies : Spring Web, Hateoas, H2 database
H2 Database
์ค์น ๋ฐ ์ค์ ๋ฐฉ๋ฒ ๋ฅผ ์ฐธ๊ณ ํ์
ํ๊ฒฝ์ค์ build.gradle
ํ์ผ์ jdbc
, h2 database
๊ด๋ จ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ถ๊ฐ
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
runtimeOnly 'com.h2database:h2'
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop
์คํ๋ง ๋ถํธ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ ์ค์ ์ถ๊ฐresources/application.properties
spring.datasource.url=jdbc:h2:tcp://localhost/~/test
spring.datasource.driver-class-name=org.h2.Driver
Domain
package com.iseunghan.accounts;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Account {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Controller
package com.iseunghan.accounts;
import static org.springframework.hateoas.server.mvc.ControllerLinkBuilder.linkTo;
import java.net.URI;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/accounts")
public class AccountController {
@Autowired
AccountService accountService;
@GetMapping()
public ResponseEntity findAll() {
List<Account> accountList = accountService.findAll();
return ResponseEntity.ok(accountList);
}
@GetMapping("/{name}")
public ResponseEntity findByName(@PathVariable String name) {
Optional<Account> account = accountService.findByName(name);
return ResponseEntity.ok(accountService.findByName(name));
}
/*@GetMapping("/{id}")
public ResponseEntity findById(@PathVariable Integer id) {
return ResponseEntity.ok(accountService.findById(id));
}*/
@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity insertAccount(@RequestBody Account insertDTO) {
Account account = accountService.join(insertDTO);
URI createUri = linkTo(AccountController.class).slash(account.getId()).toUri();
return ResponseEntity.created(createUri).body(account);
}
@PutMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity updateAccount(@RequestBody Account updateDTO) {
Account account = accountService.join(updateDTO);
URI createUri = linkTo(AccountController.class).slash(account.getId()).toUri();
return ResponseEntity.created(createUri).body(account);
}
@DeleteMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity deleteAccount(@RequestBody Account deleteDTO) {
accountService.deleteAccount(deleteDTO);
return ResponseEntity.ok().build();
}
}
Service
package com.iseunghan.accounts;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class AccountService {
@Autowired
AccountRepository accountRepository;
public List<Account> findAll() {
return accountRepository.findAll();
}
public Optional<Account> findByName(String name) {
return accountRepository.findByName(name);
}
public Optional<Account> findById(Integer id) {
return accountRepository.findById(id);
}
public Account join(Account insertDTO) {
validateDuplicateException(insertDTO);
return accountRepository.save(insertDTO);
}
public void deleteAccount(Account deleteDTO) {
accountRepository.delete(deleteDTO);
}
public void validateDuplicateException(Account account) {
findByName(account.getName())
.ifPresent(m ->{
throw new IllegalStateException("์ด๋ฏธ ์กด์ฌํ๋ ํ์์
๋๋ค.");
});
}
}
Repository
package com.iseunghan.accounts;
import java.util.List;
import java.util.Optional;
import org.springframework.stereotype.Repository;
@Repository
public interface AccountRepository {
Account save(Account account);
Optional findById(Integer id);
Optional findByName(String name);
void delete(Account delete);
List findAll();
}
repository ๊ตฌํ์ฒด
Spring Data JPA
package com.iseunghan.accounts;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
public interface SpringDataJpaAccountRepository extends JpaRepository<Account, Integer>, AccountRepository{
Optional findByName(String name);
void delete(Account delete);
}
๋ฐ์ํ