0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Mybatis resultType:"Integer"と"int"の違い

Posted at

以下のようなメールアドレスをもとにユーザーの存在チェックを行うメソッドを例に挙げる。

UserMapper.xml
<select id="verifyExistsUser1" resultType="Integer">
    SELECT 1
    FROM account
    WHERE email = #{email}
    LIMIT 1
  </select>

  <select id="verifyExistsUser2" resultType="int">
    SELECT 1
    FROM account
    WHERE email = #{email}
    LIMIT 1
  </select>
UserService.java
private void test1(String email) {
    // 存在する場合は1、存在しない場合はNULLが返却される
    Integer result1 = userMapper.verifyExistsUser1(email);
    if (Objects.isNull(result1)) {
      throw new BussinessException( HttpStatus.NOT_FOUND, "User with email " + email + " not found")
    }

    // 存在する場合は1、存在しない場合は0が返却される
    int result2 = userMapper.verifyExistsUser2(email);
    if (result2 == 0) {
      throw new BussinessException( HttpStatus.NOT_FOUND, "User with email " + email + " not found")
    }
}

単純にInteger型で返却されるか、int型で返却されるかの違いだが、結果がNULLの場合の返却値は異なるので、エラー処理するときはその点考慮すること

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?