$ docker pull redis # redis 이미지 받기
$ docker images # redis 이미지 확인
$ docker run -p 6379:6379 --name some-redis -d redis # redis 시작하기
$ docker ps # redis 실행 확인
package web.backend.module.user.repository;
import lombok.Data;
import web.backend.module.user.User;
@Data
public class UserUpdateDto {
private Long id;
private User user;
}
2. Service
backend/module/user/UserService
package web.backend.module.user;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import web.backend.module.user.repository.UserQueryRepository;
import web.backend.module.user.repository.UserSpringJpaRepository;
import java.util.List;
import java.util.Optional;
@Service
@RequiredArgsConstructor
@Transactional
@Slf4j
public class UserService {
private final UserQueryRepository userQueryRepository;
private final UserSpringJpaRepository userSpringJPARepository;
public List<User> findAll() {
return userSpringJPARepository.findAll();
}
public User findByIndexId(Long id) {
return userSpringJPARepository.findById(id).get();
}
public String save(User user) {
userSpringJPARepository.save(user);
return "ok";
}
public String update(Long id, User user) {
User userOne = userSpringJPARepository.findById(id).get();
userOne.changeUser(user.getUserId());
return "ok";
}
public String delete(Long id) {
userSpringJPARepository.deleteById(id);
return "ok";
}
}
3. Controller
backend/module/user/UserController
package web.backend.module.user;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import web.backend.module.user.repository.UserUpdateDto;
import java.util.List;
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
public class UserController {
private final UserService userService;
@GetMapping
public List<User> findAll() {
return userService.findAll();
}
@GetMapping("/{userId}")
public User findByIndexId(@PathVariable(value = "userId") Long userId ) {
return userService.findByIndexId(userId);
}
@PostMapping
public String save(@RequestBody User user) {
return userService.save(user);
}
@PatchMapping
public String update(@RequestBody UserUpdateDto user) {
return userService.update(user.getId(), user.getUser());
}
@DeleteMapping
public String delete(@RequestParam Long id) {
return userService.delete(id);
}
}