1. Parameter Mapping
ㅇ parameterType 속성
Null 값이 전달될 수 있는 컬럼에 대해서는 반드시!! jdbcType 속성 명시
(PreparedStatement#setNull(...,int sqlType)에서 필요
<insert id="insertProduct" parameterType="Product">
insert into PRODUCT(PRD_ID,PRD_DESCR)
values (#{id},#{description})
</insert>
<insert id="insertProduct" parameterType="Product">
insert into PRODUCT(PRD_ID,PRD_DESCR)
values(#{id, javaType=int,jdbcType=NUMBERIC,numericScale=2},#{description, javaType=String,jdbyType=VARCHAR})
</insert>
- Map-type Parameters
ㄴ JavaBeans 객체 사용하지 않고 여러 개의 paramter 값 전달 -> java.util.Map 객체 사용 가능
<select id="getProduct" parameterType="java.util.Map"> // 또는 "hashmap" 사용
select * from PRODUCT
where PRD_CAT_ID=#{catId} and PRD_CODE=#{code}
</select>
// 위 SQL statement 호출 시 "catId"와 "code"를 key로 갖는 Map 객체 전달해야함
Map<String,String> param = new HashMap<String,String>(2);
param.put("catId","FISH");
param.put("code","FI-SW-01");
Product prod = (Product) sqlSession.selectOne("getProduct",param);
// 또는 sqlSession.getMapper(productMapper.class).getProduct(param);
- 여러 개의 parameter 전달
ㄴ 각 parameter에 대한 참조 "param" 키워드와 위치를 나타내느 숫자 이용
// DAO class
public Account getAccount(String username,String password)
throws DataAccessException {
return accountMapper.getAccountByUsernameAndPassward(username,password);
}
<!-- Mapper XML for AccountMapper -->
<select id="getAccountByUserAndPassword" resultType="Account">
SELECT USERNAME,..
FROM ACCOUNT,PROFILE,SIGNON,BANNERDATER
WHERE ACCOUNT.USERID = #{param1} AND SIGNON.PASSWORD = #{param2}
</select>
- 여러 개의 parameter 전달 시 이름 지정 방법
ㄴ 메소드 호출 시 @Param("paramName") annotation 사용
import org.apache.ibatis.annotations.Param;
// a mapper interface
public interface AccountMapper {
Account getAccountByUsernameAndPassword(
@Param("uname") String username,
@Param("passwd") String password);
}
<!-- Mapper XML AccountMapper -->
<select id="getAccountByUsernameAndPassword" resultType="Account">
SELECT USERNAME, ..
FROM ACCOUNT,PROFILE,SIGNON,BANNERDATA
WHERE ACCOUNT.USERID = #{uname} AND SIGNON.PASSWORD=#{passwd}
</select>
2. Result Mapping
ㅇ resultType 속성
- JavaBeans의 객체 property 이름들이 테이블의 column명이나 alias명들과 일치할 경우 사용가능
ㅇ <resultMap>
- 테이블 column 명과 JavaBeans 객체의 property 이름이 일치하지 않을 때 별도의 result mapping 정의
<resultMap id="baseReultMap" type="Comment">
<id column="comment_no" jdbcType="NUMERIC" property="commentNo"/>
<result column="user_id" jdbcType="VARCHAR" property="userId"/>
<result column="comment_content" jdbcType="VARCHAR" property="commentContent"/>
<result column="reg_date" jdbcType="TIMESTAMP" property="regDate"/>
</resultMap>
<select id="selectComment" parameterType="long" resultMap="baseResultMap">
SELECT comment_no,user_id,comment_content,reg_date
FROM Comments WHERE commentNo = #{commentNo}
</select>
- setter method가 없는 immutable object에 대해 <constructor>를 통해 객체 생성
ㄴ element들의 순서는 생성자의 argument들의 순서와 일치해야함
<resultMap id="constructorResultMap" type="Comment">
<constructor>
<idArg column="commentNo" javaType="long" />
<arg column="user_id" javaType="string" />
<arg column="reg_date" javaType="date" />
<arg column="comment_content" javaType="string" />
</contructor>
</resultMap>
- Mapping Complex Type Properties
ㄴ Database 내의 두 table의 관계가 1:1 또는 N:1 관계인 경우
<!--Join 질의 및 <association>을 이용한 mapping-->
<resultMap id="associationResultMap" type="Comment">
<id column="comment_no" jdbcType="NUMERIC" property="commentNo" />
...
<result column="reg_date" jdbcTpe="TIMESTAMP" property="regDate" />
<association column="user_id" property="user" javaType="User">
<id column="user_id" property="userId" />
<result column="user_name" property="userName" />
</association>
</resultMap>
<select id="selectCommentPrimaryKeyAssociation" parameterType="long" resultMap="associationResultMap">
SELECT c.comment_no,c.user_id,c.comment_content,c.reg_date,u.user_name
FROM Comments c, Users u
WHERE c.user_id = u.user_id AND c.comment_no AND c.comment_no=#{commentNo}
</select>
<!--참조 객체에 대한 member 이름과 property명을 이용한 설정-->
<select id="selectCommentByPrimaryKeyAssociation" parameterType="long" resultType="Comment">
SELECT c.comment_no AS commentNo, c.user_id AS userId, c.comment_content AS commentConent,
c.reg_date AS regDate, u.user_id AS "user.userId", u.user_name AS "user.userName"
FROM Comments c, Users u
WHERE c.user_id = u.user_id AND c.comment_no=#{commentno}
<select>
ㄴ Database 내의 두 table의 관계가 1:N 또는 M:N 관계인 경우 -> Collection type property 소유
'SQL > database' 카테고리의 다른 글
Transaction 처리, DBCP (2) | 2024.12.11 |
---|---|
XML 설정(Parameter mapping, Result mapping,Complex Type/Collection property mapping) (3) | 2024.12.08 |
Mybatis (3) | 2024.12.04 |
DAO, DTO (1) | 2024.12.03 |
JDBC Programming (3) | 2024.12.03 |