๐ฅ ๋ง์ด๋ฐํฐ์ค, ์ธํฐ์ ํฐ, ๋น์ค์ฝํ (https://wiken.io/ken/10484) 1๊ฐ ~ 6๊ฐ
๐ ๊ณต๋ถ ํ ๋ชฉ์ : JPA / Mybatis ์ฌ์ด์ ์ฅ๋จ์ ์ ๋น๊ตํ๋ค. (๋ ๋ค DB ๋ฅผ ๋ค๋ฃจ๊ธฐ ์ํ Tool)
โ JPA : ํ๊ต, ์คํํธ์
โก Mybatis : SI, ๊ณต๊ณต, ์ฐจ์ธ๋ ํ๋ก์ ํธ
1๏ธโฃ ํ๋ก์ ํธ ์ ํ
โ ์์กด์ฑ ์ถ๊ฐ
SQL : MariaDB Driver / MyBatis Framework
DEVELOPER TOOLS : Lombok / Sprint Boot DevTools
WEB : Spring Web
TEMPLATE ENGINES : Thymelead
SECURITY : Spring Security
โก DB ์ธํ (application.yml)
# DB ๊ด๋ จ ์ธํ
spring:
profiles:
active: dev
datasource:
driver-class-name:
# driver-class-name: net.sf.log4jdbc.sql.jdbcapi.DriverSpy
url: jdbc:mariadb://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=utf8&autoReconnect=true&serverTimezone=Asia/Seoul
username: lldj
password: lldj123414
2๏ธโฃ ๊ฒ์๋ฌผ ๋ฆฌ์คํธ ํ์ธ ํ ์คํธ (SELECT) ์์ฑ ๋ฐ ๊ด๋ จ class ์์ฑ
โ ํ ์คํธ ์์ฑ
@SpringBootTest
class AppTest {
@Autowired
private AriticleService ariticleService;
@Test
@DisplayName("๊ฒ์๋ฌผ_๋ฆฌ์คํธ_๋ฐํ")
void t1() {
List<Article> articles = articleService.getArticles();
System.out.println(articles);
System.out.println(articles.get(1).getSubject());
}
}
โก Service ์์ฑ
@Service
@RequiredArgsConstructor
public class ArticleService {
private final ArticleRepository articleRepository;
public List<Article> getArticles() {
return articleRepository.getArticles();
}
}
โข Repository ์์ฑ
@Mapper
public interface ArticleRepository {
@Select("""
<script>
SELECT *
FROM article
</script>
""")
List<Article> getArticles();
}
๐ค @Select ๋ฅผ ์ฌ์ฉํ์ง ์์ ๊ฒฝ์ฐ
๋ฐ๋ก ์ ๋ฆฌ : https://like099.tistory.com/71
(MyBatis) Service ์ Mapper/Repository ์ ๊ด๊ณ (feat. @Mapper, @Select, xml ํ์ผ)
๐ ๊ณต๋ถํ๊ฒ ๋ ๊ณ๊ธฐ MyBatis ์ด๊ธฐ ์ธํ ํ ํ ์คํธ ์ฝ๋๋ฅผ ์์ฑํ๋ ๊ณผ์ ์์ ๊ธฐ์กด JPA ์ ๋ค๋ฅด๊ฒ ์์ฑํ๋ ๋ถ๋ถ์ด ํฌ๊ฒ โ 'MyBatis ๋ ๋ฐ๋ก Entity ์ ๊ฐ๋ ์ด ์๋ค.' โก 'Repository interface ์์ JpaReposito..
like099.tistory.com
โฃ DTO ํด๋์ค ์์ฑ (MyBatis ์๋ Entitiy ์ ๊ฐ๋ ์ด ๋ฐ๋ก ์์)
@Getter
public class Article {
private long id;
private String subject;
private String content;
}
โค DB ์์ฑ
DROP DATABASE IF EXISTS mybatis;
CREATE DATABASE mybatis;
USE mybatis;
CREATE TABLE article (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
createDate DATETIME NOT NULL,
modifyDate DATETIME NOT NULL,
`subject` CHAR(100) NOT NULL,
content LONGTEXT NOT NULL
);
INSERT INTO article
SET createDate = NOW(),
modifyDate = NOW(),
`subject` = '์ ๋ชฉ1',
content = '๋ด์ฉ1';
INSERT INTO article
SET createDate = NOW(),
modifyDate = NOW(),
`subject` = '์ ๋ชฉ2',
content = '๋ด์ฉ2';
3๏ธโฃ ๊ฒ์๋ฌผ ์์ฑ ํ ์คํธ (INSERT) ์์ฑ ๋ฐ ํ์ธ
โ ํ ์คํธ ์์ฑ
@SpringBootTest
@Transactional // ํ
์คํธ ๋ด์ฉ DB ์ ๋ฐ์๋์ง ์๋๋ก
class AppTest {
// (์๋ต)
@Test
@DisplayName("๊ฒ์๋ฌผ_์์ฑ")
void t2() {
String subject = "์ ๋ชฉ3";
String content = "๋ด์ฉ3";
long id = articleService.writeArticle(subject, content);
assertThat(id).isGreaterThan(2);
}
โก Service
@Service
@RequiredArgsConstructor
public class ArticleService {
// (์๋ต)
public long writeArticle(String subject, String content) {
articleRepository.writeArticle(subject, content);
return articleRepository.getLastInsertId();
}
}
โข Repository
@Mapper
public interface ArticleRepository {
// (์๋ต)
@Insert("""
<script>
INSERT INTO article
SET createDate = NOW(),
modifyDate = NOW(),
subject = #{subject},
content = #{content}
</script>
""")
void writeArticle(String subject, String content);
// ๋ง์ง๋ง์ผ๋ก ์ถ๊ฐ๋ id ๊ฐ์ ๊ฐ์ง๊ณ ์ค๋ ์ฟผ๋ฆฌ๋ฌธ
@Select("""
SELECT LAST_INSERT_ID()
""")
public long getLastInsertId();
}
โข ํ ์คํธ ํ์ธ

๐ฌ ํ๋ก๊ทธ๋๋จธ์ค MySQL ๋ฌธ์ ํ์ด
๋ฐ๋ก ์ ๋ฆฌ : https://like099.tistory.com/73
(Programmers) SQL ํ์ด (10)
โ๏ธ Programmers MySQL ์ ๋ต๋ฅ 37~54% ์ ์ฒด ํ์ด 1๏ธโฃ ๊ทธ๋ฃน๋ณ ์กฐ๊ฑด์ ๋ง๋ ์๋น ๋ชฉ๋ก ์ถ๋ ฅํ๊ธฐ (https://school.programmers.co.kr/learn/courses/30/lessons/131124) SELECT MP.MEMBER_NAME, R.REVIEW_TEXT, DA..
like099.tistory.com
'TIL ๐' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| TIL 19์ผ์ฐจ (2022.11.04) (0) | 2022.11.04 |
|---|---|
| TIL 18์ผ์ฐจ (2022.11.03) (0) | 2022.11.03 |
| TIL 16์ผ์ฐจ (2022.10.31) (0) | 2022.10.31 |
| TIL 15์ผ์ฐจ (2022.10.25) (0) | 2022.10.27 |
| TIL 14์ผ์ฐจ (2022.10.26) (0) | 2022.10.26 |