WEB

SQL SESSION 메서드

NellKiM 2024. 12. 2. 16:30

1. SELECT 관련 메서드

    • 결과가 없으면 null을 반환.
    • 결과가 여러 개라면 TooManyResultsException이 발생.<T> T selectOne(String statement)
      지정된 SQL ID를 실행하고 하나의 결과를 반환합니다.
  • <E> List<E> selectList(String statement)
    지정된 SQL ID를 실행하고 여러 개의 결과를 List로 반환합니다.
  • <K, V> Map<K, V> selectMap(String statement, String mapKey)
  • 결과를 Map 형태로 반환하며, mapKey를 기준으로 Map의 키를 설정합니다.
User user = sqlSession.selectOne("UserMapper.selectUserById", 1);
List<User> users = sqlSession.selectList("UserMapper.selectAllUsers");
Map<Integer, User> userMap = sqlSession.selectMap("UserMapper.selectAllUsers", "id");

2. INSERT 관련 메서드

  • int insert(String statement)
    지정된 SQL ID를 실행하여 삽입 작업을 수행하고, 영향을 받은 행(row) 수를 반환합니다.
int rows = sqlSession.insert("UserMapper.insertUser", newUser);

3. UPDATE 관련 메서드

  • int update(String statement)
    지정된 SQL ID를 실행하여 갱신 작업을 수행하고, 영향을 받은 행(row) 수를 반환합니다.
     
int rows = sqlSession.update("UserMapper.updateUser", user);

4. DELETE 관련 메서드

  • int delete(String statement)
    지정된 SQL ID를 실행하여 삭제 작업을 수행하고, 영향을 받은 행(row) 수를 반환합니다
int rows = sqlSession.delete("UserMapper.deleteUserById", 1);