Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.