Java/Java

[JPA] JAVA Spring Boot 더미 데이터 생성 및 DB 연동 및 메일설정

백엔드 신입사원( soft 5.10 입사) 2022. 6. 30. 18:26
반응형

DB설정 관련해서는 application.properties를 통해 진행하는 방식이 있고 application.yml파일을 통해 진행하는 방식이 있다 처음에 시작할 때는 어떤 걸로 설정을 해야 할지 고민이 많았으나 지금 와서는 그냥 저거나 이거나 아무거나 쓰면 된다. 다만 요청에 따라서 달라질 수는 있다. 그것 이외에도 이메일 연동이나 여러 것들을 진행할 수 있다.

 

아래 코드는 properties 방식으로 작성된 방식이며 나는 yml 파일로 DB설정을 진행 했다.

##mariaDB , 3306/[DB?]mysql  , username , password
#spring.datasource.driverClassName=org.mariadb.jdbc.Driver
#spring.datasource.url=jdbc:mariadb://localhost:3306/mysql?useUnicode=true&characterEncoding=utf-8
#spring.datasource.username=root
#spring.datasource.password=temp
#spring.jpa.show-sql=true

 

 

H2 콘솔 서버 방식 통신 

# TCP 서버 방식 h2 통신
spring:
  h2:
    console:
      enabled: true
      path: /h2-console

  datasource:
    url: jdbc:h2:tcp://localhost/~/test # test 부분을 자신이 원하는 것으로 바꾸시면 됩니다.
    username: username 					#username과 password는 자신의 설정에 맞게
    password:
    driver-class-name: org.h2.Driver

  jpa:
    hibernate:
      ddl-auto: update
    properties:
      hibernate:
        format_sql: true 				# 실행되는 query를 보여줌

logging.level:
  org.hibernate.SQL: debug

 

 

MYSQL 연동 설정에는 더미 데이터를 읽어 오기 위해 설정된 내용이 적혀있다.

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver			 #  mysql  8.0이하는 오른쪽 드라이브 이름 사용 com.mysql.jdbc.Driver 테스트
    url: jdbc:mysql://[userhost]/[usertable]?serverTimezone=UTC&characterEncoding=UTF-8
    username: [name]
    password: [password]
    data: classpath:testData.sql						# dummy data를 읽어 오기위해 만들어둔 파일을 설정한다
    initialization-mode: always

  jpa:
    show-sql: true
    hibernate:
      format_sql: true
      ddl-auto: create # none , create
    defer-datasource-initialization: true 				# dummy data 더미데이터를 사용하기 위해 설정


  logging:
    level:
      org:
        hibernate:
          SQL: DEBUG
          type:
            trace
  sql:                                  				# dummy data 동일하다
    init:
      mode: always

# 패턴 적용
---
spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

 

메일 연동 방법은 아래와 같다.

mail:
  host: 아마존 호스트
  region: ap-northeast-2
  port: 33333
  default-encoding: UTF-8
  accessKey: BBBBBBBBBBb
  secretKey: AAAAAAAAAAa
  from: Email@Email.com
  replyTo: Email@Email.com
  fromName: Tistory
  
  fcm-server:
  key: '생성키'
  url: 'https://fcm.googleapis.com/fcm/send'

 

더미데이터는 .SQL 형식으로 작성되었으며 기존 CRUD방식으로 진행하면 된다. 비밀번호 같은경우 생 비밀번호를  데이터베이스에 저장하면 안되기 때문에 암호화 후 저장하면 된다.

insert into test(
tset_email,
test_id,
test_pw
) values
(
'tetet@gmail.com',
'testId',
'a6929a411122b607ab5496cea8262e9499528e2718437a02b173fbfc00bd6727eca7ff500e63abfc07a629535dce199be62e207bbe129ea2dc436a238824d808'),
(
'tetet@gmail.com',
'testId1',
'a6929a411122b607ab5496cea8262e9499528e2718437a02b173fbfc00bd6727eca7ff500e63abfc07a629535dce199be62e207bbe129ea2dc436a238824d808'),
(
'tetet@gmail.com',
'testId2',
'a6929a411122b607ab5496cea8262e9499528e2718437a02b173fbfc00bd6727eca7ff500e63abfc07a629535dce199be62e207bbe129ea2dc436a238824d808'),
(
'tetet@gmail.com',
'testId3',
'a6929a411122b607ab5496cea8262e9499528e2718437a02b173fbfc00bd6727eca7ff500e63abfc07a629535dce199be62e207bbe129ea2dc436a238824d808'),
(
'tetet@gmail.com',
'testId4',
'a6929a411122b607ab5496cea8262e9499528e2718437a02b173fbfc00bd6727eca7ff500e63abfc07a629535dce199be62e207bbe129ea2dc436a238824d808');
select * from test;
반응형