
애노테이션 사용
@Select("select id, item_name, price, quantity from item where id=#{id}")
Optional<Item> findById(Long id);
문자열 대체(String Substitution)
- 문자 그대로를 처리하고 싶은 경우 사용
- 인젝션 공격을 당할 수 있음
${}
@Select("select * from user where ${column} = #{value}")
User findByColumn(@Param("column") String column, @Param("value") String value);
재사용 가능한 SQL 조각
<sql id="userColumns"> ${alias}.id,${alias}.username,${alias}.password </sql>
<select id="selectUsers" resultType="map">
select
<include refid="userColumns"><property name="alias" value="t1"/></include>, <include refid="userColumns"><property name="alias" value="t2"/></include>
from some_table t1
cross join some_table t2
</select>
Result Maps
<resultMap id="userResultMap" type="User">
<id property="id" column="user_id" />
<result property="username" column="user_name"/>
<result property="password" column="hashed_password"/>
</resultMap>
<select id="selectUsers" resultMap="userResultMap">
select user_id, user_name, hashed_password
from some_table where id = #{id}
</select>