Skip to content

Instantly share code, notes, and snippets.

@rararamorio
Created April 1, 2018 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rararamorio/9f0d770fab656fec5647ac161b62c5af to your computer and use it in GitHub Desktop.
Save rararamorio/9f0d770fab656fec5647ac161b62c5af to your computer and use it in GitHub Desktop.
autoMapping を 有効にした場合のデータの取得方法
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="person">
<resultMap id="personMap" autoMapping="true" type="jp.sample.mybatis.entity.Person">
<id property="personId" column="person_id" />
<collection property="books" columnPrefix="b_" resultMap="BookMap" />
</resultMap>
<resultMap id="BookMap" autoMapping="true" type="jp.sample.mybatis.entity.Book">
<id property="bookId" column="book_id" />
<association property="category" columnPrefix="c_" resultMap="CategoryMap" />
</resultMap>
<resultMap id="CategoryMap" autoMapping="true" type="jp.sample.mybatis.entity.Category">
<id property="categoryId" column="category_id" />
</resultMap>
<select id="selectByPersonId" resultMap="personMap">
SELECT
p.person_id
, p.name
, b.book_id as b_book_id
, b.name as b_name
, c.category_id as b_c_category_id
, c.name as b_c_name
FROM
person p
INNER JOIN person_book pb USING(person_id)
INNER JOIN book b USING(book_id)
INNER JOIN category c USING(category_id)
WHERE
p.person_id=#{person_id}
</select>
</mapper>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment