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 resultMapの使い方

Posted at

resultMapの使い方

  1. カラム名とフィールド名が異なる場合
  2. ネストされたオブジェクトをマッピングする場合
  3. 複雑な型変換やカスタムマッピングが必要な場合

基本例:カラム名とフィールド名が異なる場合

前提条件

  • データベースのテーブル名:users
  • テーブルのカラム:
    • id(主キー)
    • user_name(ユーザ名)
    • email
User.java(取得結果を格納するエンティティクラス)
@Data
public class User {
    private Long id;
    private String username; // カラム名は `user_name`
    private String email;
}

以下のようにresultMapを定義して、カラム名とフィールド名のマッピングルールを記述します。

UserMapper.xml
<mapper namespace="com.example.UserMapper">

    <!-- ResultMapの定義 -->
    <resultMap id="UserResultMap" type="com.example.User">
        <id column="id" property="id" />
        <result column="user_name" property="username" />
        <result column="email" property="email" />
    </resultMap>

    <!-- クエリ -->
    <select id="findAllUsers" resultMap="UserResultMap">
        SELECT id, user_name, email FROM users
    </select>

</mapper>
UserMapper.java
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;

@Mapper
public interface UserMapper {
    List<User> findAllUsers();
}

応用例:ネストされたオブジェクトをマッピングする場合

前提条件

  • usersテーブルに加えて、addressesテーブルが存在。
    • usersテーブル:
      • id
      • user_name
      • email
    • addressesテーブル:
      • id
      • user_idusers.idとの外部キー)
      • street
      • city
Address.java
@Data
public class Address {
    private Long id;
    private String street;
    private String city;
}
User.java
@Data
public class User {
    private Long id;
    private String username;
    private String email;
    private Address address; // ネストされたオブジェクト
}

以下のようにresultMap内でassociationを使い、ネストされたオブジェクトをマッピングします。

xml name=UserMapper.xml
<mapper namespace="com.example.UserMapper">

    <!-- AddressのResultMap -->
    <resultMap id="AddressResultMap" type="com.example.Address">
        <id column="id" property="id" />
        <result column="street" property="street" />
        <result column="city" property="city" />
    </resultMap>

    <!-- UserのResultMap -->
    <resultMap id="UserResultMap" type="com.example.User">
        <id column="id" property="id" />
        <result column="user_name" property="username" />
        <result column="email" property="email" />
        <association property="address" resultMap="AddressResultMap" />
    </resultMap>

    <!-- クエリ -->
    <select id="findAllUsersWithAddress" resultMap="UserResultMap">
        SELECT u.id AS id, u.user_name AS user_name, u.email AS email,
               a.id AS address_id, a.street AS street, a.city AS city
        FROM users u
        LEFT JOIN addresses a ON u.id = a.user_id
    </select>

</mapper>
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?