ํฐ์คํ ๋ฆฌ ๋ทฐ
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() {
//ํ
์ด๋ธ ์์ฑ
}
}
|
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(0, 10, 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(0, 10);
Page<Object[]> result = repo.findByTitle3("66", paging);
result.getContent().forEach(arr -> System.out.println(Arrays.toString(arr)));
}
}
|
'(๊ตฌ)Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์คํ๋ง๋ถํธ] ํ์๋ฆฌํ (thymeleaf) (0) | 2019.07.16 |
---|---|
[์คํ๋ง๋ถํธ] ๋ถํธ์์ JSP์ ๋ง์ด๋ฐํฐ์ค ์ฌ์ฉํ๊ธฐ (0) | 2019.07.16 |
[์คํ๋ง๋ถํธ]RestController (0) | 2019.07.10 |
[์คํ๋ง๋ถํธ] Spring Boot ์์ํ๊ธฐ (0) | 2019.07.09 |
[Spring] 6. ์์กด๊ฐ์ฒด ์๋์ฃผ์ (์ด๋ ธํ ์ด์ ) (0) | 2019.06.06 |
- Total
- Today
- Yesterday
- Java
- ๊ฒ์๋ฌผ์กฐํ
- ๊ฐ๋ฐ
- ๊ฐ๋ฐํ๊ฒฝ๊ตฌ์ถ
- ๊ฒ์ํ ์ญ์
- ๋ถํธ ์๋์์ฑ
- ๋ณ๋ช ์ฒ๋ฆฌ
- ๊ฒ์ํ๋ง๋ค๊ธฐ
- ์คํ๋ง๋ถํธ ์๋์์ฑ
- ๊ฒ์๋ฌผ ์ญ์
- ์ดํด๋ฆฝ์ค ํ๊ธ ์ธ์ฝ๋ฉ
- ์๊ณ ๋ฆฌ์ฆ
- ์๋ฐ
- ์๋ฃ๊ตฌ์กฐ
- ๊ฒ์ํ ์กฐํ
- ์ ์ฒด๊ฒ์๋ฌผ ์กฐํ
- mysql์ค์น
- Algorithm
- tomcat์ค์น
- typeAliases
- java ํ๊ฒฝ๋ณ์
- ์จ๋ฆฌ์์ค
- java jdk ์ค์น
- ์ดํด๋ฆฝ์ค ์ค์น
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |