-
51일 차 MyBatis resultMap삼성SDS_멀티캠퍼스/MyBatis 2015. 11. 19. 10:13반응형
Mapper.xml 기존에는 namespace이후 바로 SQL구문을 사용했는데
기존의 방식은 컬럼명과 모델클래스의 변수명이 일치해야 했었다.
하지만 resultMap을 이용하면 모델클래스의 변수명과 DB의 컬럼명이 달라도 할 수 있다.
**그러니까 여기서는 일부러 모델클래스의 변수명을 DB의 컬럼명과 다르게 했습니다**
짚고 넘어갈 것
#{프로퍼티} -> 타입에 맞춰서 값을 꺼낸다
${프로퍼티} -> 그냥 꺼낸다
$의 사용법은 %${프로퍼티}% 이런식으로 like 구문을 사용할 때 사용한다.
아래 코드중에서 기존에는 select문에서 result값을 받아올 때에는
resultType에 모델클래스의 이름을 참조했는데
resultMap을 사용 할 경우에는 resultType대신 resultMap과 해당 id를 입력해야 한다.
만약 resultType을 사용하면 id를 읽을 수 없다고 나온다.
<mapper namespace="dao.IBoardDao">
<resultMap type="board" id="boardMap">
<id column="id" property="id" />
<id column="title" property="title" />
<id column="content" property="content" />
<id column="writer" property="writer" />
<id column="reg_date" property="regDate" />
<id column="hit" property="hit" />
</resultMap>
<insert id="insertBoard" parameterType="board">
insert into board values(
0, #{title}, #{content}, #{writer},
#{regDate}, #{hit} )
</insert>
<update id="updateBoard" parameterType="board">
update board set
title = #{title},
content = #{content},
writer = #{writer},
reg_date = #{regDate},
hit = #{hit}
where id = #{id}
</update>
<delete id="deleteBoard" parameterType="int">
delete from board where
id = #{id}
</delete>
<select id="selectOne" parameterType="int" resultMap="boardMap">
select *
from board where id = #{id}
</select>
<select id="selectAll" resultMap="boardMap">
select * from board
</select>
</mapper>
나머지는 다 이전과 같이 구현을 하고
결과를 출력해보자.
import dao.BoardDao;
public class selectOneTest {
public static void main(String[] args) {
System.out.println(BoardDao.getInstance().selectOne(118));
}
}
결과는 성공적으로 출력되었다.
그런데 insert, update구문은 set값이 비어있으면 제대로 실행이 되질 않는다.
그렇다면 비어있는 값일 때 null이 아닌 다른값을 넣어주려면
1. DB에서 default값을 설정해준다
2. 동적쿼리로 null값일 때 default값을 설정해준다
2가지 방법이있는데
DB는 건들 수 없다고 가정하고 이번에는 2번 방법을 사용하겠다.
여기서 choose구문 밑에있는
when은 if와 같고
otherwise는 else와 같다
예전에도 한번 사용한 적이 있는데
자주 까먹으므로 되도록이면 반복학습을 하자.
<?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="dao.IBoardDao">
<resultMap type="board" id="boardMap">
<id column="id" property="id" />
<id column="title" property="title" />
<id column="content" property="content" />
<id column="writer" property="writer" />
<id column="reg_date" property="regDate" />
<id column="hit" property="hit" />
</resultMap>
<sql id="content">
<choose>
<when test="#{content == null}">
'내용없음'
</when>
<otherwise>
#{content}
</otherwise>
</choose>
</sql>
<sql id="title">
<choose>
<when test="#{title == null}">
'제목없음'
</when>
<otherwise>
#{title}
</otherwise>
</choose>
</sql>
<sql id="regDate">
<choose>
<when test="#{regDate == null}">
now()
</when>
<otherwise>
#{regDate}
</otherwise>
</choose>
</sql>
<sql id="writer">
<choose>
<when test="#{writer == null}">
'작성자 없음'
</when>
<otherwise>
#{writer}
</otherwise>
</choose>
</sql>
<insert id="insertBoard" parameterType="board">
insert into board values(
0, <include refid="title"></include>,
<include refid="content"></include>,
<include refid="writer"></include>,
<include refid="regDate"></include>, #{hit} )
</insert>
<update id="updateBoard" parameterType="board">
update board set
title = <include refid="title"></include>,
content = <include refid="content"></include>,
writer = <include refid="writer"></include>,
regDate = <include refid="regDate"></include>,
hit = #{hit}
where id = #{id}
</update>
<delete id="deleteBoard" parameterType="int">
delete from board where
id = #{id}
</delete>
<select id="selectOne" parameterType="int" resultMap="boardMap">
select *
from board where id = #{id}
</select>
<select id="selectAll" resultMap="boardMap">
select * from board
</select>
</mapper>
결과를 출력해보자.
이번엔 제목 또는 내용으로 검색할 수 있도록 만들어 보겠다.
<select id="selectCondition" parameterType="board" resultMap="boardMap">
select * from board where 1=2
<if test="title != null">
or title like '%${title}%'
</if>
<if test="content != null">
or content like '%${content}%'
</if>
</select>
반응형'삼성SDS_멀티캠퍼스 > MyBatis' 카테고리의 다른 글
52일 차 MyBatis와 Spring 연동 (0) 2015.11.20 50일 차 MyBatis 에노테이션과 xml을 혼용하기 (0) 2015.11.18 50일 차 MyBatis 인터페이스를 사용한 매핑정보(에노테이션 사용) (0) 2015.11.18 49일 차 MyBatis 만든 것 자료 (0) 2015.11.17 49일 차 MyBatis (0) 2015.11.17