You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
데이터 접근 기술 - MyBatis
1️⃣ MyBatis 소개
JdbcTmeplate과 비교해서 MyBatis는 SQL을 XML에 편리하게 작성할 수 있고 또 동작 쿼리를 매우 편리하게 작성할 수 있다는 점이다.
JdbcTemplate - SQL 쿼리 작성 예시
✅설정의 장단점
2️⃣ MyBatis 설정
dependencies { implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' implementation 'org.springframework.boot:spring-boot-starter-web' //JdbcTemplate 추가 implementation 'org.springframework.boot:spring-boot-starter-jdbc' //MyBatis 추가 ------------------------------------ implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.3' //H2 데이터베이스 추가 runtimeOnly 'com.h2database:h2' compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' //테스트에서 lombok 사용 testCompileOnly 'org.projectlombok:lombok' testAnnotationProcessor 'org.projectlombok:lombok' }✅ 설정
application.properties 설정
✅ 위 코드 설정 설명
mybatis.type-aliases-package
,,;로 구분하면 된다.mybatis.configuration.map-underscore-to-camel-case
logging.level.hello.itemservice.repository.mybatis=trace
3️⃣ MyBatis 적용1 - 기본
위 코드 설명
@Mapper애노테이션을 붙여주어야 한다. 그래야 MyBatis에서 인식할 수 있다.xml의 해당 SQL을 실행하고 결과를 돌려준다.ItemMapper인터페이스의 구현체에 대한 부분은 뒤에 별도로 설명한다.src/main/resources/hello/itemservice/repository/mybatis/ItemMapper.xml
namespace: 앞서 만든 매퍼 인터페이스를 지정하면 된다.#{}문법을 사용하면 된다. 그리고 매퍼에서 넘긴 객체의 프로퍼티 이름을 적어주면 된다.#{}문법을 사용하면PreparedStatement를 사용한다. JDBC의?를 치환한다 생각하면 된다.import org.apache.ibatis.annotations.Param; void update(@Param("id") Long id, @Param("updateParam") ItemUpdateDto updateParam); <update id="update"> update item set item_name=#{updateParam.itemName}, price=#{updateParam.price}, quantity=#{updateParam.quantity} where id = #{id} </update>✅XML 특수문자
and price <= #{maxPrice}<=를사용하지않고<=를사용한것을확인할수있다.그이유는XML에서는데이터영역에<,>같은특수문자를사용할수없기때문이다.이유는간단한데,XML에서TAG가시작하거나종료할때<,>와같은 특수문자를 사용하기 때문이다.All reactions