repository package
public interface QuestionRepository extends CrudRepository<Question,Long>{
}
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class UserDto {
@NotBlank
@Pattern(regexp = "^([\\w-]+(?:\\.[\\w-]+)*)@((?:[\\w-]+\\.)*\\w[\\w-]{0,66})\\.([a-z]{2,6}(?:\\.[a-z]{2})?)$")
private String email;
@JsonIgnore
@NotBlank
@Size(min = 4, max = 15)
private String password;
@NotBlank
@Size(min = 6, max = 10)
private String name;
public User toEntity() {
return new User(email, password, name);
}
public User toEntityWithPasswordEncode(PasswordEncoder bCryptPasswordEncoder) {
return new User(email, bCryptPasswordEncoder.encode(password), name);
}
}
https://gmlwjd9405.github.io/2018/12/25/difference-dao-dto-entity.html
@Entity
@Getter
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
@ToString
public class User implements Serializable {
private static final long serialVersionUID = 7342736640368461848L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@JsonProperty
private Long id;
@Column(nullable = false)
@JsonProperty
private String email;
@Column(nullable = false)
@JsonIgnore
private String password;
// @Override
// public boolean equals(Object o) { ... }
// @Override
// public int hashCode() { ... }
// @Override
// public String toString() { ... }
https://gmlwjd9405.github.io/2018/12/25/difference-dao-dto-entity.html
@Controller
@RequestMapping("/")
public class HomeController {
@GetMapping
public String home(HttpSession session) {
if (!SessionUtil.getUser(session).isPresent()) {
return "login";
}
return "index";
}
}
https://gmlwjd9405.github.io/2018/12/25/difference-dao-dto-entity.html
@Controller
-API와 view를 도잇에 사용하는 경우에 사용
-대신 API서비스로 사용하는 경우는 @ResponseBody를 사용하여 객체를 반환합니다.
-view(화면) return이 주목적입니다.
예시2
@RestController
@RequestMapping("/api/users")
public class ApiUserController {
@Autowired
private UserService userService;
@PostMapping("/login")
public ResponseEntity login(@RequestBody @Valid LoginDto loginDto, HttpSession session) {
SessionUtil.setUser(session, userService.login(loginDto));
return new ResponseEntity(HttpStatus.OK);
}
}
https://gmlwjd9405.github.io/2018/12/25/difference-dao-dto-entity.html
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
@Resource(name = "bCryptPasswordEncoder")
private PasswordEncoder bCryptPasswordEncoder;
@Autowired
private MessageSourceAccessor msa;
public User save(UserDto userDto) {
if (isExistUser(userDto.getEmail())) {
throw new UserDuplicatedException(msa.getMessage("email.duplicate.message"));
}
return userRepository.save(userDto.toEntityWithPasswordEncode(bCryptPasswordEncoder);
}
}
https://gmlwjd9405.github.io/2018/12/25/difference-dao-dto-entity.html