PostController
생성 & 작성@RestController
public class PostController {
private static final Logger logger = LoggerFactory.getLogger(PostController.class);
}
PostService
생성 & 작성@Service
public class PostService {
private static final Logger logger = LoggerFactory.getLogger(PostService.class);
}
@Repository
public class PostDao {
private static final Logger logger = LoggerFactory.getLogger(PostDao.class);
}
@RestController
@RequestMapping("post")
public class PostController {
private static final Logger logger = LoggerFactory.getLogger(PostController.class);
private final PostService postservice;
public PostController(
@Autowired PostService postservice
){
this.postservice = postservice;
}
@PostMapping()
public void createPost(){
}
@GetMapping("{id}")
public void readPost(@PathVariable("id")int id){
}
@GetMapping("")
public void readPostAll(){
}
@PutMapping("{id}")
public void updatePost(@PathVariable("id")int id){
}
@DeleteMapping("{id}")
public void deletePost(@PathVariable("id") int id){
}
}
public class PostDto {
private int id;
private String title;
private String content;
private String writer;
private int boardId;
=> dao 까지 이렇게 정의한 dto 오브젝트가 들어가게 될 것이다.
@PostMapping()
@ResponseStatus(HttpStatus.CREATED)
public void createPost(
@RequestBody PostDto postdto
){
}
@GetMapping("{id}")
public PostDto readPost(
@PathVariable("id")int id
){
}
@GetMapping("")
public List<PostDto > readPostAll(){
}
@PutMapping("{id}")
@ResponseStatus(HttpStatus.ACCEPTED)
public PostDto updatePost(
@PathVariable("id")int id,
@RequestBody PostDto postdto
){
}
@DeleteMapping("{id}")
@ResponseStatus(HttpStatus.ACCEPTED)
public PostDto deletePost(
@PathVariable("id") int id
){
}
public class PostService {
private static final Logger logger = LoggerFactory.getLogger(PostService.class);
public void createPost(PostDto postdto){
}
public PostDto readPost(int id){
}
public List<PostDto> readPostAll(){
}
public void updatePost(int id, PostDto postdto){
}
@Repository
public class PostDao {
private static final Logger logger = LoggerFactory.getLogger(PostDao.class);
private final PostRepository postrepository;
//postrepository 지정해주기
public PostDao(
@Autowired PostRepository postrepository
){
this.postrepository=postrepository;
}
public void createPost(PostDto postdto){
PostEntity postentity = new PostEntity();
postentity.setTitle(postdto.getTitle());
postentity.setContent(postdto.getContent());
postentity.setWriter(postdto.getWriter());
postentity.setBoardentity(null);
this.postrepository.save(postentity);
}
public PostEntity readPost(int id){
Optional<PostEntity> postentity = this.postrepository.findById((long) id);
if(postentity.isEmpty()){
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
}
return postentity.get();
}
public Iterator<PostEntity> readPostAll(){
return this.postrepository.findAll().iterator();
}
public void updatePost(int id, PostDto postdto){
PostEntity postentity = new PostEntity();
postentity.setTitle(postdto.getTitle()==null?postentity.getTitle() : postdto.getTitle());
postentity.setContent(postdto.getContent()==null?postentity.getContent() : postdto.getContent());
postentity.setWriter(postdto.getWriter()==null?postentity.getWriter() : postdto.getWriter());
postentity.setBoardentity(null);
this.postrepository.save(postentity);
}
public void deletePost(int id){
Optional<PostEntity> targetentity = this.postrepository.findById((long) id);
if(targetentity.isEmpty()){
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
}
this.postrepository.delete(targetentity.get());
}
}
@Service
public class PostService {
private static final Logger logger = LoggerFactory.getLogger(PostService.class);
private final PostDao postdao; //postdao 추가 선언 (활용 위해)
public PostService(@Autowired PostDao postdao, PostDao postdao1){
this.postdao = postdao; //postdao 초기화
}
public void createPost(PostDto postdto){
this.postdao.createPost(postdto);
}
public PostDto readPost(int id){
PostEntity postentity = this.postdao.readPost(id);
}
public List<PostDto> readPostAll(){
Iterator<PostEntity> iterator=this.postdao.readPostAll();
}
public void updatePost(int id, PostDto postdto){
this.postdao.updatePost(id,postdto);
}
public void deletePost(int id){
this.postdao.deletePost(id);
}
}
@Service
public class PostService {
private static final Logger logger = LoggerFactory.getLogger(PostService.class);
private final PostDao postdao; //postdao 추가 선언 (활용 위해)
public PostService(@Autowired PostDao postdao, PostDao postdao1){
this.postdao = postdao; //postdao 초기화
}
public void createPost(PostDto postdto){
this.postdao.createPost(postdto);
}
public PostDto readPost(int id){
PostEntity postentity = this.postdao.readPost(id);
return new PostDto(
Math.toIntExact(postentity.getId()),
postentity.getTitle(),
postentity.getContent(),
postentity.getWriter(),
postentity.getBoardentity()==null
?0:Math.toIntExact(postentity.getBoardentity().getId())
);
}
public List<PostDto> readPostAll(){
Iterator<PostEntity> iterator=this.postdao.readPostAll();
List<PostDto> postDtoList = new ArrayList<>();
while(iterator.hasNext()){
PostEntity postentity = iterator.next();
postDtoList.add(new PostDto(
Math.toIntExact(postentity.getId()),
postentity.getTitle(),
postentity.getContent(),
postentity.getWriter(),
postentity.getBoardentity()==null
?0:Math.toIntExact(postentity.getBoardentity().getId())
));
}
return postDtoList;
}
public void updatePost(int id, PostDto postdto){
this.postdao.updatePost(id,postdto);
}
public void deletePost(int id){
this.postdao.deletePost(id);
}
}
package jsbdy.jpa;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("post")
public class PostController {
private static final Logger logger = LoggerFactory.getLogger(PostController.class);
private final PostService postservice;
public PostController(
@Autowired PostService postservice
){
this.postservice = postservice;
}
@PostMapping()
@ResponseStatus(HttpStatus.CREATED)
public void createPost(
@RequestBody PostDto postdto
){
this.postservice.createPost(postdto);
}
@GetMapping("{id}")
public PostDto readPost(
@PathVariable("id")int id
){
return this.postservice.readPost(id);
}
@GetMapping("")
public List<PostDto > readPostAll(){
return this.postservice.readPostAll();
}
@PutMapping("{id}")
@ResponseStatus(HttpStatus.ACCEPTED)
public void updatePost(
@PathVariable("id")int id,
@RequestBody PostDto postdto
){
this.postservice.updatePost(id, postdto);
}
@DeleteMapping("{id}")
@ResponseStatus(HttpStatus.ACCEPTED)
public void deletePost(
@PathVariable("id") int id
){
this.postservice.deletePost(id);
}
}
=>
잘 된당 ㅎㅎ
-> 코드 수정
public void updatePost(int id, PostDto postdto){
PostEntity postentity = new PostEntity();
PostEntity postentity = new PostEntity();
로 계속 새로운 엔티티 생성하고 있었삼;;ㅋㅋ public void updatePost(int id, PostDto postdto){
Optional<PostEntity> targetentity = this.postrepository.findById(Long.valueOf(id);
if (targetentity.isEmpty()){
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
}
PostEntity postentity=targetentity.get();
postentity.setTitle(postdto.getTitle()==null?postentity.getTitle() : postdto.getTitle());
postentity.setContent(postdto.getContent()==null?postentity.getContent() : postdto.getContent());
postentity.setWriter(postdto.getWriter()==null?postentity.getWriter() : postdto.getWriter());
postentity.setBoardentity(null);
this.postrepository.save(postentity);
}
Type 1 => http://127.0.0.1?index=1&page=2
- 파라메터의 값과 이름을 함께 전달하는 방식으로 게시판 등에서 페이지 및 검색 정보를 함께 전달하는 방식을 사용할 때 많이 사용합니다.
Type 2 => http://127.0.0.1/index/1
- Rest api에서 값을 호출할 때 주로 많이 사용합니다.
@GetMapping("read")
public ModelAndView getFactoryRead( int factroyId, SearchCriteria criteria)
{ }
위의 경우 /read?no=1와 같이 url이 전달될 때 no 파라메터를 받아오게 됩니다.
@RequestParam 어노테이션의 괄호 안의 경우 전달인자 이름(실제 값을 표시)입니다.
이렇게 @RequestParam의 경우 url 뒤에 붙는 파라메터의 값을 가져올 때 사용을 합니다.
- @ PathVariable의 경우
url에서 각 구분자에 들어오는 값을 처리해야 할 때 사용합니다.
@PostMapping("delete/{idx}")
@ResponseBody
public JsonResultVo postDeleteFactory(@PathVariable("idx") int factoryIdx) {
return factoryService.deleteFacotryData(factoryIdx);
}