JDBC 로 직접 SQL을 작성했을때의 여러가지 문제가 발생하여 생긴 SQL Mapper로는 크게 JDBCTemplate과 MyBatis가 있다.

@JdbcTest // Jdbc Slice Test
@AutoConfigureTestDatabase(replace = Replace.NONE) // 테스트용 DB 쓰지 않도록
@Rollback(value = false) // Transactional 에 있는 테스트 변경은 기본적으론 롤백 하도록 되어있다.
public class JDBCTemplateTest {
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
@DisplayName("SQL Mapper - JDBC Template 실습")
void sqlMapper_JDBCTemplateTest() {
// given
var accountTemplateDAO = new AccountTemplateDAO(jdbcTemplate);
// when
var id = accountTemplateDAO.insertAccount(new AccountVO("new user2", "new password2"));
// then
var account = accountTemplateDAO.selectAccount(id);
assert account.getUsername().equals("new user2");
}
}

@SpringBootTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Import(DBConfiguration.class)
public class MyBatisTest {
// Mapper 클래스를 받으려면 mapper.xml 빌드 해야하고, 그러려면 main 으로 옮겨서 해야함...
@Autowired
AccountMapper accountMapper;
@Autowired
AccountMapperV2 accountMapperV2;
@Test
@DisplayName("SQL Mapper - MyBatis 실습")
void sqlMapper_MyBatisTest() {
// given
// when
accountMapper.insertAccount(new AccountMyBatisVO("new user3", "new password3"));
var account = accountMapper.selectAccount(1);
// then
assert !account.getUsername().isEmpty();
}
@Test
@DisplayName("SQL Mapper - MyBatis V2 실습")
void sqlMapper_MyBatisV2Test() {
// given
// when
accountMapperV2.insertAccount(new AccountMyBatisVO("new user4", "new password4"));
var account = accountMapperV2.selectAccount(1);
// then
assert !account.getUsername().isEmpty();
}
}
QueryMapper 의 DB의존성 및 중복 쿼리 문제로 ORM 이 탄생했다.
