티스토리 뷰

MySQL 설치

링크 : https://www.youtube.com/watch?v=k5Lww0Wcp2U&feature=youtu.be

 

· standalone

· 포트번호 3306

· 비밀번호 : admin

MySQL Workbench

Local instance MySQL Router 클릭, 로그인

→ Users and Privilleges

→ add account

 

Login Name : jpa_user

Passwrod : jpa_user

 

→ apply

→ Administrative Roles에서 DBA 선택 → apply (권한 부여)

→ file → close connection tap

 

메인 화면에 '+' 기호 클릭

 

이름에 jpa_user

store in valut에 비번 입력 후 ok

커넥션이 뜨면 성공

 

sts 프로젝트 만들기

 

 

 

 

생성 후

pom.xml -> H2 dependency 삭제 

 

데이터 베이스 연결

*src/main/resources -> application.properties

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/jpa_ex?useSSL=false
spring.datasource.username=jpa_user
spring.datasource.password=jpa_user

실행시키면 에러 발생

 

MySQL Workbench -> jpa_uset -> SCHEMAS에서 오른쪽 클릭 -> create schema

 

name : jpa_ex

collation : utf8-default collation

 

생성 후 sts 프로젝트 다시 실행

 

*src/main/resources -> application.properties 에 아래 코드 작성

 

spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
spring.jpa.database=mysql
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect

logging.level.org.org.hibernate=info

 

 

모델 생성(Board 클래스 생성하기)

src/main/java → com.younggeun.models 패키지 생성

com.younggeun.models → Board 클래스 생성

 

*Board.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.younggeun.models;
 
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
 
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.sql.Timestamp;
 
@Entity
public class Board{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long bno;
    private String title;
    private String content;
    private String writer;
 
    @CreationTimestamp
    private Timestamp regdate;
    @UpdateTimestamp
    private Timestamp updatedate;
}
cs

 

· Getter/Setter 생성

· toString 생성(StringBuilder/StringBuffer)

  

Repository 생성(BoardRepository 인터페이스 생성)

src/main/java → com.younggeun.repositories 패키지 생성

com.younggeun.repositories → BoardRepository 인터페이스 생성

 

*BoardRepository 인터페이스

1
2
3
4
5
6
7
package com.younggeun.repositories;
 
import com.younggeun.models.Board;
import org.springframework.data.repository.CrudRepository;
 
public interface BoardRepository extends CrudRepository<Board, Long> {
}
cs

 

 

TEST 코드 작성하기

src/test/java → com.younggeun → Boot03ApplicationTests.java

 

*Boot03ApplicationTests.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.younggeun;
 
import com.younggeun.models.Board;
import com.younggeun.repositories.BoardRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
 
@RunWith(SpringRunner.class)
@SpringBootTest
public class Boot03ApplicationTests {
    @Autowired
    BoardRepository boardRepository;
 
    @Test
    public void contextLoads() {
    }
 
    @Test
    public void insert200(){
        for(int i = 1; i <= 200; i++){
            Board board = new Board();
            board.setTitle("제목.." + i);
            board.setContent("내용.." + i);
            board.setWriter("user0.." + (i%10));
            boardRepository.save(board);
        }
    }
 
}
 
cs

 

테스트 성공시 화면

 

 

MySQL Workbench에서 실행결과 확인

jpa_ex 스키마 → tables → board → Result Grid(표 그림) 클릭

 

 

' > SpringBoot,게시판 만들기' 카테고리의 다른 글

[SpringBoot]5/29  (0) 2018.06.01
[SpringBoot]5/15  (2) 2018.05.24
[SpringBoot]5/8  (0) 2018.05.14
[SpringBoot]스프링부트 프로젝트 생성하기  (0) 2018.04.27
댓글