Spring64 데이터 접근 기술 - JPA 중요한 것은 JPA이다. 스프링 데이터 JPA, Querydsl은 JPA를 편리하게 사용하도록 도와주는 도구라 생각하면 된다. build.gradle //JPA, 스프링 데이터 JPA 추가 implementation 'org.springframework.boot:spring-boot-starter-data-jpa' application.properties (main, test) #JPA log logging.level.org.hibernate.SQL=DEBUG logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE org.hibernate.SQL=DEBUG : 하이버네이트가 생성하고 실행하는 SQL을 확인할 수 있다. org.hibernate.. 2023. 8. 10. 데이터 접근 기술 - MyBatis JdbcTemplate - SQL 여러줄 String sql = "update item " + "set item_name=:itemName, price=:price, quantity=:quantity " + "where id=:id"; MyBatis - SQL 여러줄 update item set item_name=#{itemName}, price=#{price}, quantity=#{quantity} where id = #{id} → XML에 작성하기 때문에 라인이 길어져도 문자 더하기에 대한 불편함이 없다. JdbcTemplate - 동적 쿼리 String sql = "select id, item_name, price, quantity from item"; //동적 쿼리 if (StringUtils.h.. 2023. 8. 10. 데이터 접근 기술 -테스트 DB 연동 main - application.properties //src/main/resources/application.properties spring.profiles.active=local spring.datasource.url=jdbc:h2:tcp://localhost/~/test spring.datasource.username=sa logging.level.org.springframework.jdbc=debug test - application.properties // src/test/resources/application.properties spring.profiles.active=test spring.datasource.url=jdbc:h2:tcp://localhost/~/test sprin.. 2023. 8. 10. 데이터 접근 기술 - 스프링 JdbcTemplate 장점 1. 설정의 편리함 2. 반복 문제 해결 : JdbcTemplate은 템플릿 콜백 패턴을 사용해서 커넥션 획득 statement 를 준비하고 실행 결과를 반복하도록 루프를 실행 커넥션 종료, statement , resultset 종료 트랜잭션 다루기 위한 커넥션 동기화 예외 발생시 스프링 예외 변환기 실행 단점 동적 SQL을 해결하기 어렵다. build.gradle //JdbcTemplate 추가 implementation 'org.springframework.boot:spring-boot-starter-jdbc' 동적 쿼리 문제 // 검색 조건이 없음 select id, item_name, price, quantity from item // 상품명( itemName )으로 검색 select id.. 2023. 8. 10. 이전 1 ··· 3 4 5 6 7 8 9 ··· 16 다음