본문 바로가기

Kotlin4

Java Entity코드를 Kotlin Entity로.. User.java @Entity public class JavaUser { @Id @GeneratedValue(strategy = IDENTITY) private Long id; @Column(nullable = false) private String name; private Integer age; @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true) private final List userLoanHistories = new ArrayList(); public JavaUser() { } public JavaUser(String name, Integer age) { if (name.isBlank()) { throw n.. 2024. 1. 9.
Java Service코드를 Kotlin Service코드로.. UserService.java @Service public class JavaUserService { private final UserRepository userRepository; public JavaUserService(UserRepository userRepository) { this.userRepository = userRepository; } @Transactional public void saveUser(UserCreateRequest request) { User user = new User(request.getName(), request.getAge(), Collections.EMPTY_LIST,null); userRepository.save(user); } @Transactional(rea.. 2024. 1. 8.
Java Controller를 Kotlin Controller로 UserController.java @RestController public class UserController { private final UserService userService; public UserController(UserService userService) { this.userService = userService; } @PostMapping("/user") public void saveUser(@RequestBody UserCreateRequest request) { userService.saveUser(request); } @GetMapping("/user") public List getUsers() { return userService.getUsers(); } @PutMapping(".. 2024. 1. 8.
JAVA DTO를 Kotlin DTO로.. JAVA DTO를 Kotlin DTO로 변환하기. JAVA로 만들어진 UserResponse DTO이다. 생성자가 존재하며 getter가 존재한다. UserResponse.java public class UserResponse { private final long id; private final String name; private final Integer age; public UserResponse(User user) { this.id = user.getId(); this.name = user.getName(); this.age = user.getAge(); } public long getId() { return id; } public String getName() { return name; } pub.. 2024. 1. 8.