TIL 23.11.23

ν™©μ€ν•˜Β·2023λ…„ 11μ›” 24일
0

TIL

λͺ©λ‘ 보기
120/146

πŸ“ŒToday I Learned

Spring

μƒˆ ν”„λ‘œμ νŠΈ λ§Œλ“€κΈ°

New > Spring Legacy Project > Spring MVC Project > "MyBatisTest" > "com.test.mybatis" > Finish


MyBatis(iBatis)

  • μ˜μ†μ„± 계측(= λ°μ΄ν„°λ² μ΄μŠ€ μ‘°μž‘ 계측)
  • JDBC μ—­ν• 
  • μ‘μš© ν”„λ‘œκ·Έλž¨ <-> JDBC <-> λ°μ΄ν„°λ² μ΄μŠ€
  • μ‘μš© ν”„λ‘œκ·Έλž¨ <-> MyBatis <-> λ°μ΄ν„°λ² μ΄μŠ€ (μŠ€ν”„λ§ κΈ°μˆ μ€ μ•„λ‹˜)

Spring Mybatis

  • μŠ€ν”„λ§ ν”„λ‘œμ νŠΈ 쀑 ν•˜λ‚˜
  1. XML Mapperλ₯Ό μ‚¬μš©ν•˜λŠ” 방식 (70~80%)
  2. Interface Mapperλ₯Ό μ‚¬μš©ν•˜λŠ” 방식 (20~30%) -> κ°„λ‹¨ν•œκ±° λ§Œλ“€ λ•Œ.

spring과 spring boot의 차이

초기 μ„ΈνŒ…μ„ μ–΄λŠμ •λ„ ν•΄μ£Όλ‚˜μ˜ 차이
spring bootκ°€ μ„ΈνŒ…μ„ 많이 ν•΄μ€€λ‹€.

μ€€λΉ„κ°€ 마치고 λ‚˜λ©΄ μ‹€ν–‰ν•˜λŠ” 방법은 λ™μΌν•˜λ‹€.


ν”„λ‘œμ νŠΈ μ„€μ •

  1. pom.xml
    • JDK 11
    • Spring 5.0.7
  • properties
    - facet
    - JDK 11
  1. pom.xml
    • MyBatis > μ˜μ‘΄μ„± μΆ”κ°€

      C:\class\dev\pom_23.11.23.xml

    • ojdbc6 ν•„μš” > lib 볡사
      -> μ˜›λ‚  λ°©μ‹μœΌλ‘œ μ„€μΉ˜ (buildpath or library)

MyBatisTest > JDBC μ—°κ²° ν…ŒμŠ€νŠΈ

  • 업무 κ΅¬ν˜„(X)
  • ν…ŒμŠ€νŠΈ(O)
  • src/test/main > "com.test.persistence" > "JDBCTest.java"

성곡


μ‹€νŒ¨


Connection

  • DBUtil.open() > DB μ—°κ²° > Connection 객체 생성 X 반볡
  • λͺ¨λ‘ DB μ—°κ²° > κ΄€λ¦¬λ˜μ§€ μ•ŠλŠ” Connection 객체 > λ©”λͺ¨λ¦¬ 차지, μ„±λŠ₯ μ €ν•˜
  • μ‚¬μš©μ΄ μ™„λ£Œλœ Connection > close() > λ²ˆκ±°λ‘œμ›€
    1. List > DBUtil.open() > conn.close()
    2. View > DBUtil.open()
    3. Add > DBUtil.open()

    μž‘μ—…λŸ‰ 많음 + νŒ€μž‘μ—…
    ν”„λ‘œμ νŠΈ λ•Œ μ™œ close 잘 μ•ˆν–ˆλŠ”κ°€? -> 손이 많이 κ°€μ„œ.
    ν•˜μ§€λ§Œ 맀번 λŠμ–΄μ£ΌλŠ”κ²Œ μ’‹λ‹€.

Connection Pool, 컀λ„₯μ…˜ ν’€

  • Commons DBCP
  • Tomcat DBCP
  • HikariCP > μŠ€ν”„λ§ λΆ€νŠΈ 2.0λΆ€ν„° 기본으둜 μ‚¬μš©

CPκ°€ 미리 connection을 λ§Œλ“€μ–΄ 두고 DB와 μ—°κ²°ν•΄λ‘”λ‹€.
첫 번째 μ‚¬λžŒμ΄ connection이 ν•„μš”ν•˜λ‹€
-> μ—°κ²°λœ connection을 λŒ€μ—¬ν•΄μ€€λ‹€.
-> DB μž‘μ—… 끝
-> connection 회수

λ§Œμ•½ 미리 λ§Œλ“€μ–΄λ‘” 객체가 μ—†λ‹€λ©΄ μƒˆλ‘œμš΄ μ‚¬μš©μžλŠ” κΈ°λ‹€λ¦¬κ²Œ ν•œλ‹€.
κΈ°λ‹€λ¦¬λŠ” μ‹œκ°„μ΄ μ§§μœΌλ―€λ‘œ 큰 λ¬Έμ œκ°€ μ—†λ‹€.


Commons DBCP

  • pom.xml > commons-dbcp μ˜μ‘΄μ„± μΆ”κ°€
  • WEB-INF > spring > root-context.xml > μ„€μ • μΆ”κ°€
  • com.test.mybatis.persistence > "DBCPTest.java"
package com.test.mybatis.persistence;

import static org.junit.Assert.assertNotNull;
import javax.sql.DataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import lombok.extern.log4j.Log4j;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")
@Log4j
public class DBCPTest {

	@Autowired
	private DataSource dataSource;

	@Test
	public void testConnection() {

		assertNotNull(dataSource);
	}
}

HikariCP

  • pom.xml > μ˜μ‘΄μ„± μΆ”κ°€
  • root-context.xml > μ„€μ • μΆ”κ°€

MyBatis

  • pom.xml > μ˜μ‘΄μ„± μΆ”κ°€(4개), hikariCP μΆ”κ°€
  • root-context.xml > μ„€μ • μΆ”κ°€(HikariCP, MyBatis, Mapper μœ„μΉ˜ μ„€μ • 및 ν•΄λ‹Ή 폴더와 파일 생성)

생성

  • com.test.mybatis.persistence > "MapperTest.java"

SQL μ‹€ν–‰

  • "com.test.controller" > "MyBatisController.java"

  • "com.test.persistence"
    > "MyBatisDAO.java"(I)
    > "MyBatisDAOImpl.java"(C)

  • "com.test.domain" > "MyBatisDTO.java"

  • src/main/resources > mapper > "mybatis.xml"

  • views
    > "list.jsp"
    > "add.jsp"
    > "addok.jsp"

  • MyBatisTest > "script.sql"


Controller만 λ§Œλ“ λ‹€κ³  ν•˜μ§€λ§Œ
μ•ˆμ—λ₯Ό 듀여닀보면 μ—°μ‡„μ μœΌλ‘œ μ—¬λŸ¬ 객체λ₯Ό μƒμ„±ν•œλ‹€.

1개의 controller 객체 생성 -> 총 6개 객체 생성

profile
μ°¨κ·Όμ°¨κ·Ό ν•˜λ‚˜μ”©

0개의 λŒ“κΈ€