728x90

Spring Boot프로젝트 중 게시글에 첨부된 이미지 데이터를 DAO를 통해 List로 반환하려한다.

이 때 반환받은 List가 Null인지 체크할 때 사용

  


1. Spring을 사용하는 경우 (CollectionUtils.isEmpty)

List<FileDTO> files = fileService.getFileList(boardId);

CollectionUtils.isEmpty(files)
  • FileDTO(이미지 파일 정보)를 DB에서 게시판 아이디로 조회하여 받아오는 코드이다.
  • CollectionUtils.isEmpty()를 이용해서 List가 null인지 체크 가능

2. Java

List<String> list = new ArrayList<>();

list.isEmpty();
  • isEmpty()를 이용해서 List가 null인지 체크 가능

 

* 참고 : https://jihyehwang09.github.io/2020/04/13/java-list-null-check/

 

Java- List의 Null을 체크하는 법

다양한 List의 Null 체크 방법들이 있는데, 어떤 방법이 효과적일지에 대해 정리해보도록 하자. TL;DRList의 Null Check를 할 때는Spring에서 Apach Commons 라이브러리의 CollectionUtils.isEmpty()를 사용하자. Null

JihyeHwang09.github.io

 

728x90

비밀번호 찾기를 시도한 사용자의 비밀번호를 초기화 시키고 초기화된 비밀번호를 사용자 이메일로 보낸다.

1. 인증 절차

  • 구글 계정 관리 -> 보안 -> 앱 비밀번호 -> [메일], [windows 컴퓨터] 2개 선택 후 생성
  • 여기서 나오는 비밀번호를 application.properties에 추가할 것이다.

2. 설정

  • gmail -> 톱니바퀴 클릭 후 모든 설정보기

톱니바퀴

  • 전달 및 모든 POP/IMAP 진입

  • POP 다운로드 -> 모든 메일에 POP 사용하기체크
  • IMAP 액세스 -> IMAP사용 체크
  • 변경사항 저장

3. 의존성 추가

// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mail
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-mail'

 

4. SMTP 설정

# GoogleMail
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=사용자 구글 아이디
spring.mail.password=인증 비밀번호
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.smtp.auth=true
  • username=test2@gmail.com
  • password=아까 인증에서 비밀번호

5. MailService

@Autowired
private JavaMailSender javaMailSender;

// 메일을 통해 임시 비밀번호 전송
public void sendMail(String username, String userEmail, String temporaryPassword) {
    List<String> toUserList = new ArrayList<>();
    toUserList.add(userEmail);
    int userSize = toUserList.size();

    SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
    simpleMailMessage.setTo((String[]) toUserList.toArray(new String[userSize]));
    simpleMailMessage.setSubject("임시 비밀번호를 보내드립니다.");
    simpleMailMessage.setText(username + "님의 임시 비밀번호 : " + temporaryPassword);

    javaMailSender.send(simpleMailMessage);
}

 

* 참고 사이트

https://kitty-geno.tistory.com/43

 

Spring Boot | 메일 발송하기 (Google SMTP)

▶ 스프링 부트 메일 발송하기 (Google SMTP) 1. Google 홈페이지 > Google 계정 관리(우측상단) 2. 보안 > 앱 비밀번호 앱 비밀번호는 위에 2단계 인증을 해야 생성됩니다. 3. 메일, Windows 컴퓨터 4. 앱 비..

kitty-geno.tistory.com

 

728x90

자료형을 String으로 변환하는 방법을 기록한다.

1. 사용법

  • String.valueOf( )
Long a = 3l;
String s = String.valueOf(a);
System.out.println(s);

2. 변환 가능한 자료형

변환 가능한 자료형

 

 + 추가

  • 비슷한 사례로 int를 Long으로 변환하려 하면 Long.valueOf( int i ) 를 사용하면 된다.
  • [바뀌고자하는 자료형].valueOf( ) 로 기억하자 

'Language > Java' 카테고리의 다른 글

[Java] List Null 체크  (0) 2022.01.11
[Java] UUID란? (중복제거)  (0) 2022.01.09
[Java] String contains() 문자열 포함 여부  (0) 2022.01.03
[Java] Long, int <-> String 타입 변경  (0) 2022.01.03
[Java] String null, isEmpty() 확인  (0) 2022.01.03
728x90

문자열 포함 여부 확인 방법을 기록한다.

1. 코드

String s = "hello World";

System.out.println("e : " + s.contains("e"));
System.out.println("hello : " + s.contains("hello"));
System.out.println("oW : " + s.contains("oW"));
System.out.println("world : " + s.contains("world"));
System.out.println("World : " + s.contains("World"));
System.out.println("ld : " + s.contains("ld"));

2. 결과

결과

3. contains 결론

  • 붙어있는 문자열 사이 한 글자도 true반환
  • 공백을 구분한다.
  • 대/소문자를 구분한다.
728x90

1. Long에서 String으로 타입 변경과 String에서 Long으로 타입 변경을 기록한다.

  • Long to String
  • Long id = 3l; String s = Long.toString(id);
  • String to Long
  • String s = "3"; Long i = Long.parseLong(s);

2. int에서 String으로 타입 변경과 String에서 Long으로 타입 변경을 기록한다.

  • String to int
  • int to = Integer.parseInt(String s);
  • int to String
  • String s = Integer.toString(int i);
728x90

빈값확인 및 null확인

 

String a = "";
String b = null;

1. a의 경우

System.out.println(a.isEmpty());
  • 결과값 : true

2. b의 경우

System.out.println(b == null);
  • 결과값 : true
728x90

1. MySQL 시간을 담을 컬럼은 DATETIME으로 설정

2. DTO에 Timestamp 타입으로 선언

private Timestamp createDate;

3. 생성 시점에 시간 셋팅해주기

member.setCreateDate(Timestamp.valueOf(LocalDateTime.now())); // 회원가입 시간
  • Member member
  • Timestamp.valueOf(LocalDateTime.now()) : 해당 로직 실행 당시 시간을 리턴

 

오류 발생 : 생성 시간보다 3시간 추가돼서 DB에 저장되는 것을 발견.

 

해결 : application.properties 파일에서 UTC부분을 Asia/Seoul로 수정

spring.datasource.url=jdbc:mysql://localhost:3306/board?serverTimezone=UTC&characterEncoding=UTF-8
spring.datasource.url=jdbc:mysql://localhost:3306/board?serverTimezone=Asia/Seoul&characterEncoding=UTF-8

+ Recent posts