ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฐ˜์‘ํ˜•
JPA ( Java Persistent API )

JPA๋Š” ๋งˆ์ด๋ฐ”ํ‹ฐ์Šค๋ณด๋‹ค ๊ฐ€๋ฒผ์šด ์• ๋กœ ๋ ˆํŒŒ์ง€ํ† ๋ฆฌ๋ฅผ ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ค€๋‹ค.

๋ ˆ๊ฐ€์‹œ์™€ ๋ถ€ํŠธ ๋‘˜ ๋‹ค ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๊ณ  JPA๋Š” ์ž๋™์œผ๋กœ  SQL ์„ ๋งŒ๋“ค์–ด์ค€๋‹ค. 

 

์ƒˆ๋กœ์šด ์Šคํ‚ค๋งˆ ์ƒ์„ฑ

workbench ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด sql์„ ์ž‘์„ฑํ•˜์ง€์•Š๊ณ ๋„ ๋งŒ๋“ค์ˆ˜ ์žˆ๋‹ค.

์™ผ์ชฝ ์œ„๋ถ€๋ถ„์— ๋“œ๋Ÿผํ†ต ๋ชจ์–‘์„ ํด๋ฆญํ•œ ๋’ค ์œ„์™€๊ฐ™์ด ์Šคํ‚ค๋งˆ๋ฅผ ์ƒ์„ฑํ•ด์ฃผ๊ณ  Apply๋กœ ์ ์šฉ์‹œ์ผœ์ค€๋‹ค.

ํ…Œ์ด๋ธ”์ด ์—†๋Š” ์ƒํƒœ๋กœ ๋งŒ๋“ค์–ด๋†“๊ณ  ์„ค์ • ์ž‘์—…์„ ํ•œ๋‹ค.

 

JPA ์„ค์ • ํ•˜๊ธฐ application.properties์†Œ์Šค์ฝ”๋“œ

application.properties์— ๋‹ค์Œ์˜ ์ฝ”๋“œ๋“ค์„ ์ถ”๊ฐ€์ ์œผ๋กœ ์ž‘์„ฑํ•œ๋‹ค.

1
2
3
4
# JPA configurations
# Create table config
# Create : Create after Drop // update : Create and Update
spring.jpa.hibernate.ddl-auto=update

ddl์„ ์ž๋™์œผ๋กœ ์ž‘์„ฑํ•ด์ฃผ๋Š” ์„ค์ • ์ฝ”๋“œ ( ddl - ํ…Œ์ด๋ธ”์˜ ์ƒ์„ฑ ์ˆ˜์ • ์‚ญ์ œ ๋“ฑ )

1
2
# checked database original function
spring.jpa.generate-ddl=false

์šฐ๋ฆฌ๊ฐ€ ์“ฐ๋Š” DB๊ฐ€ ์ง€์›ํ•˜๋Š” ํŠน์ˆ˜๊ธฐ๋Šฅ๋“ค์„ ์‚ฌ์šฉํ•  ๊ฒƒ์ธ๊ฐ€๋ฅผ ์„ค์ •ํ•˜๋Š”๊ฒƒ false

1
2
# checked show SQL log
spring.jpa.show-sql=true

SQL ๋กœ๊ทธ๋ฅผ ๋ณผ ๊ฒƒ์ธ๊ฐ€

1
2
# usage DBMS
spring.jpa.database=mysql

๋‚ด๊ฐ€ ์‚ฌ์šฉํ•  DB์„ค์ •

1
2
# select log level
logging.level.org.hibernate=info

๋กœ๊ทธ ๋ ˆ๋ฒจ ์„ค์ •

 

VOํด๋ž˜์Šค ์ƒ์„ฑ

JPA๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด VO์ž์ฒด๊ฐ€ ํ…Œ์ด๋ธ”, DB์˜ ์—”ํ‹ฐํ‹ฐ๊ฐ€ ๋œ๋‹ค.

ํŒจํ‚ค์ง€์™€ ํด๋ž˜์Šค๋ช… ํ™•์ธ

 

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.springboot.boot01.domain;
 
import java.sql.Timestamp;
 
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
 
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
 
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
 
@Setter
@Getter
@ToString
@Entity
@Table(name="boot_board")
public class Board {
 
    //ํ…Œ์ด๋ธ”์ด ๋งŒ๋“ค์–ด์กŒ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ  ํ•„๋“œ๋“ค์„ ๋‚˜์—ดํ•œ๋‹ค.
    /*
     * # @GeneratedValue : ์ˆซ์žํ˜• ์ฃผ์š”ํ‚ค์— ๋ถ€์—ฌํ•  ์†์„ฑ
     * 1. AUTO : JPA ์ž์ฒด์—์„œ ์‹œํ€€์Šค ๊ธฐ๋Šฅ์„ ๋ถ€์—ฌ
     * 2. SEQUENCE : ORACLE์˜ ์‹œํ€€์Šค ๊ธฐ๋Šฅ์„ ๋ถ€์—ฌ
     * 3. IDENTITY : MYSQL์˜ AUTO_INCREMENT ๊ธฐ๋Šฅ์„ ๋ถ€์—ฌ
     */
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer boardNo;
    private String title;
    private String writer;
    private String content;
    @CreationTimestamp    //INSERT์‹œ์— ํ˜„์žฌ์‹œ๊ฐ„์„ ์ฝ์–ด์„œ ์ €์žฅ
    private Timestamp regDate;
    @UpdateTimestamp    //UPDATE์‹œ์— ํ˜„์žฌ์‹œ๊ฐ„์„ ์ฝ์–ด์„œ ์ €์žฅ
    private Timestamp updateDate;
    
}
 

 

Test

ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•ด ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค. (์œ„์น˜ ์ฃผ์˜)

๋‹จ์œ„๋ณ„๋กœ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

BoardRepositoryTest.java ์†Œ์Šค์ฝ”๋“œ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.springboot.boot01.test.board;
 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
 
 
@RunWith(SpringRunner.class)
@SpringBootTest
public class BoardRepositoryTest {
    
    
    @Test
    //ํ…Œ์ด๋ธ” ์ƒ์„ฑ ํ…Œ์ŠคํŠธ
    public void test() {
        //ํ…Œ์ด๋ธ” ์ƒ์„ฑ
    }
    
 
}
 

์‹คํ–‰ํ•˜๋ฉด ์ฝ˜์†”์ฐฝ์—์„œ Hibernate: ~~ ์‹คํ–‰์ด ๋œ๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
DB์— ํ…Œ์ด๋ธ”์ด ์ƒ์„ฑ๋œ ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

CRUD ํ…Œ์ŠคํŠธ (์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ)

๋งˆ์ด๋ฐ”ํ‹ฐ์Šค์˜ ๊ฒฝ์šฐ์—๋Š” mapper๋ฅผ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ–ˆ์—ˆ๋‹ค.

๋ถ€ํŠธ์—์„œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. 

 

BoardRepository.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package com.springboot.boot01.repository;
 
import java.util.List;
 
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
 
import com.springboot.boot01.domain.Board;
 
//CRUD
//CrudRepository<์—”ํ‹ฐํ‹ฐํด๋ž˜์Šค, ํ”„๋ผ์ด๋จธ๋ฆฌํ‚ค์˜ ํƒ€์ž…>
public interface BoardRepository extends CrudRepository<Board, Integer> {
 
    /*
        # Spring Data JPA๋Š” ๋ฉ”์„œ๋“œ์˜ ์ด๋ฆ„๋งŒ์œผ๋กœ ์›ํ•˜๋Š” SELECT๋ฌธ์„ ์‹คํ–‰์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
        # ์ฟผ๋ฆฌ ๋ฉ”์„œ๋“œ๋Š” ์ด๋ฆ„๊ทœ์น™์— ๋งž๊ฒŒ ์ง€์–ด์ค˜์•ผ ์ •ํ™•ํ•˜๊ฒŒ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.
        ex) find + ์—”ํ‹ฐํ‹ฐํด๋ž˜์Šค๋ช… + By + ์ปฌ๋Ÿผ๋ช…
    
        -- ์ฐธ์กฐ๋ฌธ์„œ
        # https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-creation
     */
    
    //์ œ๋ชฉ์œผ๋กœ ๊ฒŒ์‹œ๋ฌผ ๊ฒ€์ƒ‰ ์ฒ˜๋ฆฌ
    List<Board> findBoardByTitle(String title);
    
    //์ž‘์„ฑ์ž๋กœ ๊ฒŒ์‹œ๋ฌผ ๊ฒ€์ƒ‰ ์ฒ˜๋ฆฌ - ์“ฐ๋Š”๊ฒŒ ํ™•์‹คํ•˜๊ธด ํ•œ๋ฐ ์—”ํ‹ฐํ‹ฐ ํด๋ž˜์Šค๋ช… ์ƒ๋žต ๊ฐ€๋Šฅ! 
    List<Board> findByWriter(String writer);
 
    
    
    
    
    
    /*
        # ๊ฒ€์ƒ‰์ฒ˜๋ฆฌ ์‹œ์— LIKE ๊ตฌ๋ฌธ ์ฒ˜๋ฆฌ
        1. ๋‹จ์ˆœ  LIKE: findBy~~Like
        2. ํ‚ค์›Œ๋“œ + %: findBy~~StartingWith
        3. % + ํ‚ค์›Œ๋“œ: findBy~~EndingWith
        4. % + ํ‚ค์›Œ๋“œ + %: findBy~~Containing
     */
    
    //๊ธ€ ๋‚ด์šฉ์œผ๋กœ ๊ฒ€์ƒ‰  LIKE %keyword% ์ฒ˜๋ฆฌ
    List<Board> findByContentContaining(String keyword);
    
    
    
    
    
    
    
    
    /*
     * # WHERE ์ ˆ์—์„œ AND, OR ์กฐ๊ฑด ์ฒ˜๋ฆฌ
     * - ์ฟผ๋ฆฌ๋ฉ”์†Œ๋“œ ๋„ค์ด๋ฐ์‹œ์— And, Or ๋ฌธ์ž์—ด์„ ์ถ”๊ฐ€ํ•œ๋‹ค.
     * - ํŒŒ๋ผ๋ฏธํ„ฐ๋„ ์ง€์ •ํ•œ ์†์„ฑ ์ˆ˜๋งŒํผ ๋งž์ถฐ์ค˜์•ผ ํ•œ๋‹ค.
     */
    
    //์ œ๋ชฉ + ๋‚ด์šฉ์œผ๋กœ ๊ฒ€์ƒ‰
    List<Board> findByTitleContainingOrContentContaining(String title, String content);
 
    
    
    
    
    
    
    
    
    /*
        # ๋ถ€๋“ฑํ˜ธ ์กฐ๊ฑด ์ฒ˜๋ฆฌ
        1. > : GreaterThan
        2. < : LessThan
     */
   
   //ex: title Like %?% AND board_no > ?
   List<Board> findByTitleContainingAndBoardNoGreaterThan(String title, Integer boardNo);
    
    
   
   
   
   
   
   
   
    
    /*
     * # ORDER BY ์ ˆ ์‚ฌ์šฉ
     * - OrderBy + ์†์„ฑ๋ช… + Asc, Desc ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
     */
    
    //title LIKE %?% ORDER BY board_no DESC
    List<Board> findByTitleContainingOrderByBoardNoDesc(String title);
    
    
    
    
    
    
    
    
    
    
    /*
     * # ํŽ˜์ด์ง• ์ฒ˜๋ฆฌ
     * - ์œ„์™€ ๊ฐ™์€ ์ฟผ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ๋ชจ๋“  ์ฟผ๋ฆฌ ๋ฉ”์„œ๋“œ์˜ ๋งˆ์ง€๋ง‰ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ํŽ˜์ด์ง€ ์ฒ˜๋ฆฌ ๊ตฌ๋ฌธ์ธ
     *      LIMIT ์ ˆ์„ ์ง€์›ํ•˜๋Š” Pageable ์ธํ„ฐํŽ˜์ด์Šค์™€ ์ •๋ ฌ์ฒ˜๋ฆฌ๋ฅผ ์ง€์›ํ•˜๋Š”  Sort ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ
     *   JPA์—์„œ ์ง€์›ํ•˜๊ณ  ์žˆ๋‹ค.
     */
    
    //WHERE board_no > ? ORDER BY board_no DESC LIMIT ?, ?
    List<Board> findByBoardNoGreaterThanOrderByBoardNoDesc(Integer boardNo, Pageable paging);
    
    //Pageable ๊ฐ์ฒด๋กœ ์ •๋ ฌ ์ฒ˜๋ฆฌํ•˜๊ธฐ
    //List<Board> findByBoardNoGreaterThan(Integer boardNo, Pageable paging);
    
    
    
    
    
    
    
    
    
    
    
    
    /*
        # ํŽ˜์ด์ง• ์ฒ˜๋ฆฌ์˜ ํŽธ์˜์„ฑ์„ ๊ฐœ์„ ํ•œ Page<T> ๋ฆฌํ„ดํƒ€์ž…
        - ํŽ˜์ด์ง• ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฐ์ดํ„ฐ๋ฅผ List<T>ํƒ€์ž…์œผ๋กœ ์–ป๋Š” ๊ฒƒ๋ณด๋‹ค Page<T>ํƒ€์ž…์œผ๋กœ ์–ป์„ ๊ฒฝ์šฐ
                 ์ „์ฒด ๊ฒŒ์‹œ๋ฌผ ์ˆ˜๋‚˜, ์ด์ „, ๋‹ค์ŒํŽ˜์ด์ง€ ํ™œ์„ฑํ™” ์—ฌ๋ถ€ ๋“ฑ ๋” ๋งŽ์€ ์ •๋ณด๋ฅผ ์–ป์–ด๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    */
    Page<Board> findByBoardNoGreaterThan(Integer boardNo, Pageable paging);
    
    
    
    
    
    
    
    
    
    
    
    /////////////////////////////////////////////////////////////////////////////
    
    
    //ํ•„์š”ํ•œ ์ปฌ๋Ÿผ๋งŒ ์กฐํšŒํ•ด์•ผํ•  ๊ฒฝ์šฐ(์œ„์—๊ฒƒ๋“ค์€ ๋ฌด์กฐ๊ฑด select * from ์ž„) : @Query ์‚ฌ์šฉ
    @Query("select b.title, b.content from Board b " //์—”ํ‹ฐํ‹ฐ ๋ช…์„ ์ ์–ด์ฃผ์–ด์•ผํ•˜๊ณ  ๊ทธ ๋ณ„์นญ๋„ ์ ์–ด์ค€๋‹ค.
            + "where b.title like %?1% and b.boardNo > 0 order by b.boardNo desc"
    List<Object[]> findByTitle2(String title);
    
    @Query("select b.title, b.content from Board b " //์—”ํ‹ฐํ‹ฐ ๋ช…์„ ์ ์–ด์ฃผ์–ด์•ผํ•˜๊ณ  ๊ทธ ๋ณ„์นญ๋„ ์ ์–ด์ค€๋‹ค.
            + "where b.title like %?1% and b.boardNo > 0 order by b.boardNo desc"
    Page<Object[]> findByTitle3(String title, Pageable paging);
    
    
    
}
 
 

 

BoardRepositoryTest.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package com.springboot.boot01.test.board;
 
import java.util.Arrays;
import java.util.List;
 
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.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.junit4.SpringRunner;
 
import com.springboot.boot01.domain.Board;
import com.springboot.boot01.repository.BoardRepository;
 
 
 
@RunWith(SpringRunner.class)
@SpringBootTest
public class BoardRepositoryTest {
    
    @Autowired
    private BoardRepository repo;
    
    @Test
    //ํ…Œ์ด๋ธ” ์ƒ์„ฑ ํ…Œ์ŠคํŠธ
    public void test() {
        //ํ…Œ์ด๋ธ” ์ƒ์„ฑ
    }
    
    @Test
    //๊ฒŒ์‹œ๋ฌผ ์‚ฝ์ž… ํ…Œ์ŠคํŠธ
    public void testInsert() {
        Board article = new Board();
        article.setTitle("ํ…Œ์ŠคํŠธ1์ค‘!");
        article.setContent("ํ…Œ์ŠคํŠธ1 ์ค‘์ด๋ž๋‹ˆ๋‹ค~~!!!");
        article.setWriter("๊น€ํ…Œ์ŠคํŠธ1");
        
        //insert๊ตฌ๋ฌธ์€ save()๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉ!
        repo.save(article);
        System.out.println("๊ฒŒ์‹œ๊ธ€ ๋“ฑ๋ก ์„ฑ๊ณต!");
    }
    
    @Test
    //ํ•˜๋‚˜์˜ ๊ฒŒ์‹œ๋ฌผ ์กฐํšŒ ํ…Œ์ŠคํŠธ
    public void testSelectOne() {
        //ํ•˜๋‚˜์˜ ๊ฒŒ์‹œ๋ฌผ์„ ์กฐํšŒํ•  ๋•Œ๋Š”  findById().get()๋ฅผ ์‚ฌ์šฉ!
        Board article = repo.findById(2).get();
        System.out.println(article);
    }
    
    @Test
    //๋ชจ๋“  ๊ฒŒ์‹œ๋ฌผ ์กฐํšŒ ํ…Œ์ŠคํŠธ
    public void testSelectAll() {
        
        for(Board article : repo.findAll()) {
            System.out.println(article);
        }    
    }
    
    @Test
    //๊ฒŒ์‹œ๋ฌผ ์ˆ˜์ • ํ…Œ์ŠคํŠธ
    public void testUpdate() {
        //๋จผ์ € ์ˆ˜์ •ํ•  ๊ฒŒ์‹œ๋ฌผ์„ ์กฐํšŒํ•œ๋‹ค.
        Board article = repo.findById(1).get();
        
        //์ˆ˜์ • ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ
        article.setTitle("์ œ๋ชฉ์ˆ˜์ •");
        article.setWriter("์ž‘์„ฑ์ž๋ณ€๊ฒฝ!");
        
        //์ˆ˜์ • ๋ฐ˜์˜ : save()๋ฉ”์„œ๋“œ
        repo.save(article);
        System.out.println("์ˆ˜์ • ์™„๋ฃŒ!");
    }
    
    @Test
    //๊ฒŒ์‹œ๋ฌผ ์‚ญ์ œ ํ…Œ์ŠคํŠธ
    public void testDelete() {
        repo.deleteById(2);
        System.out.println("์‚ญ์ œ ์™„๋ฃŒ!");
    }
    
    //๊ฒŒ์‹œ๋ฌผ 200๊ฐœ ์ง‘์–ด๋„ฃ๊ธฐ
    @Test
    public void testInser200() {
        for(int i=1; i<=200; i++) {
            Board article = new Board();
            article.setTitle("์ œ๋ชฉํ…Œ์ŠคํŠธ" + i);
            article.setContent("๋‚ด์šฉ ํ…Œ์ŠคํŠธ" + i + "์ค‘ ์ž…๋‹ˆ๋‹ค...");
            article.setWriter("์ž‘์„ฑ์ž"+i);
            
            repo.save(article);
        }
    }
 
    
    @Test
    //์ œ๋ชฉ์œผ๋กœ ๊ฒ€์ƒ‰ ํ…Œ์ŠคํŠธ
    public void testByTitle() {
//        List<Board> list = repo.findBoardByTitle("์ œ๋ชฉํ…Œ์ŠคํŠธ3");
//        for(Board board : list) {
//            System.out.println(board);
//        }
        
        //java8๋ถ€ํ„ฐ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ ๋ฌธ๋ฒ•
        //์ œ๋ชฉ์œผ๋กœ ๊ฒ€์ƒ‰ ํ…Œ์Šค๋“œ
        repo.findBoardByTitle("์ œ๋ชฉํ…Œ์Šค๋“œ7")
            .forEach(board -> System.out.println(board));
        //์ž‘์„ฑ์ž๋กœ ๊ฒ€์ƒ‰ ํ…Œ์ŠคํŠธ
        repo.findByWriter("์ž‘์„ฑ์ž7")
            .forEach(b -> System.out.println(b));
    }
    
    
    //WHERE LIKE ์ ˆ ํ…Œ์ŠคํŠธ
    @Test
    public void testByContaining() {
//    repo.findByContentContaining("3")
//        .forEach(b -> System.out.println(b));
//
//    repo.findByTitleContainingOrContentContaining("33", "99")
//        .forEach(b -> System.out.println(b));
//
//    repo.findByTitleContainingAndBoardNoGreaterThan("3", 100)
//        .forEach(b -> System.out.println(b));
 
    repo.findByTitleContainingOrderByBoardNoDesc("44")
    .forEach(b -> System.out.println(b));
        
        
    }
    
    
    //ํŽ˜์ด์ง• ํ…Œ์ŠคํŠธ
    @Test
    public void testByPaging() {
        
        //ํŽ˜์ด์ง€ ์ •๋ณด ๊ฐ์ฒด ์ƒ์„ฑ
        //of๋ฉ”์„œ๋“œ์˜ ์ฒซ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’ : ํŽ˜์ด์ง€ ๋ฒˆํ˜ธ - 1, ๋‘๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’: ํ•œ ํŽ˜์ด์ง€์— ๋ณด์—ฌ์ค„ ๊ฒŒ์‹œ๋ฌผ ์ˆ˜
//        Pageable paging = PageRequest.of(0, 10);
//        
//        repo.findByBoardNoGreaterThanOrderByBoardNoDesc(0, paging)
//            .forEach(b -> System.out.println(b));
        
        //๋ฉ”์„œ๋“œ ๋ช…์ด ๋„ˆ๋ฌด ๊ธฐ๋‹ˆ๊นŒ ์ •๋ ฌ์€ Pageable ๊ฐ์ฒด๋กœ ์ •๋ ฌ ์ฒ˜๋ฆฌํ•˜๊ธฐ
//        Pageable paging = PageRequest.of(0, 10, Sort.Direction.DESC, "boardNo");
//        repo.findByBoardNoGreaterThan(0, paging)
//            .forEach(b -> System.out.println(b));
        
        
        //Page<T>๋ฆฌํ„ด ํƒ€์ž…์„ ์‚ฌ์šฉํ•œ ํŽ˜์ด์ง• ์ฒ˜๋ฆฌ
        Pageable paging = PageRequest.of(010, Sort.Direction.DESC, "boardNo");
        Page<Board> result = repo.findByBoardNoGreaterThan(0, paging);
        
        System.out.println("Page size : " + result.getSize());
        System.out.println("Total size : " + result.getTotalPages());
        System.out.println("Total count : " + result.getTotalElements());
        System.out.println("Prex : " + result.hasPrevious());
        System.out.println("Next : " + result.hasNext());
        
        List<Board> articles = result.getContent();
        articles.forEach(b -> System.out.println(b));
        
    }
    
    
    @Test
    public void testByTitle2() {
//        repo.findByTitle2("33")
//            .forEach(arr -> System.out.println(Arrays.toString(arr)));
        
        Pageable paging = PageRequest.of(010);
        Page<Object[]> result = repo.findByTitle3("66", paging);
        
        result.getContent().forEach(arr -> System.out.println(Arrays.toString(arr)));
    }
    
}
 
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€