설정
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.type-aliases-package=com.kh.spring18complex.dto,com.kh.spring18complex.vo
mybatis.mapper-locations=/mybatis/mapper/**/*-mapper.xml
XML dtd 선언
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
태그 종류
<select>
<select id="search" resultType="PocketmonDto">
select * from pocketmon
where instr(${column}, #{keyword})>0 order by ${column} asc
</select>
<insert>
<insert id="add" parameterType="PocketmonDto">
insert into pocketmon(no, name, type) values(#{no}, #{name}, #{type})
</insert>
<update>, <set>, <if>
<update id="editAllInOne">
update pocketmon
<set>
<if test="name!=null">
name=#{name},
</if>
<if test="type!=null">
type=#{type},
</if>
</set>
where no=#{no}
</update>
<delete>
<delete id="delete" parameterType="int">
delete pocketmon where no=#{no}
</delete>
<bind>
<bind name="orderListExist" value="orderList!=null and orderList.size()>0"></bind>
<where>
<where>
<if test="memberBirthExist">
and member_birth like #{memberBirth} || '%'
</if>
</where>
// ||는 문자열 연결, '%'는 0개 이상의 임의 문자(와일드카드)
<choose>, <![CDATA[]]>
<choose>
<when test="minPointExist and maxPointExist">
and member_point between #{minPoint} and #{maxPoint}
</when>
<otherwise>
<![CDATA[
and member_point <= #{maxPoint}
]]>
</otherwise>
</choose>
<!-- <부등호는 태그로 인식되기 때문에 CDATA 혹은 lt(<)로 대체한다 -->
<foreach>
<foreach collection="memberLevelList" open="(" close=")" item="memberLevel" separator=",">
#{memberLevel}
</foreach>
날짜 및 구간 검색 요령
<!-- 문자열로 날짜를 입력받아 Date 형태로 변환 후 날짜 처리 -->
and member_join between
to_date(#{beginJoindate}||' '||'00:00:00', 'YYYY-MM-DD HH24:MI:SS')
and
to_date(#{endJoindate}||' '||'23:59:59', 'YYYY-MM-DD HH24:MI:SS')
<!-- 현재시각에서 일수를 뺀 후 문자열처리해서 시간을 날린 다음 날짜형태로 변환
(시간 00:00으로 설정) -->
and member_login between
to_date(
to_char(
sysdate-#{빼는 일 수}, 'YYYY-MM-DD'
),
'YYYY-MM-DD'
) and sysdate
계층형 조회(N+1 이슈 )
- MemberDto memberDto, List<BoardDto>boardList 를 필드로 갖고 있는 MemberWithBOardVO 준비
- <result> : 조회 결과의 특정 컬럼을 특정 필드에 연결
- column : 데이터베이스 항목명
- property : 변수명
- javaType : 자바 자료형(생략 가능)
- jdbcType : 데이터베이스 자료형(생략 가능)
- <association> : 내부에 존재하는 클래스 객체를 의미\
- property : 변수명
- <collection> : 내부에 존재하는 컬렉션 객체를 의미(ex:List)
- select : 실행시켜서 채워줄 구문을 또는 구문의 id를 적어줌 : 컬렉션을 채우기 위한 서브쿼리
- property : 컬렉션의 변수명
- javaType : 컬렉션의 자료형
- ofType : 저장데이터의 자료형(제네릭 타입)
- column : 어떤 column을 가지고 구문을 부를 것인지 지정 : select를 실행할 때 전달할 항목
<resultMap type = "MemberWithBoardVO" id="mbvo">
<association property="memberDto">
<result column="member_id" property="memberId"/>
<result column="member_pw" property="memberPw"/>
</association>
<collection property="boardList" select="boardByMember" javaType="java.util.List"
ofType="BoardDto" column="member_id">
</collection>
</resultMap>
<select id="memberWithBoard" resultMap="mbvo">
select * from member
</select>
<select id="boardByMember" resultType="BoardDto">
select * from board where board_writer = #{memberId}
</select>
- id : memberWithBoard를 부르면 resultMap으로 지정된 id : mbvo가 실행되고, 이 resultMap에서 collection에 있는
select : boardByMember가 실행됨