select 기본
<select id="selectPerson" parameterType="int" resultType="hashmap">
SELECT * FROM PERSON WHERE ID = #{id}
</select>
변수 설정은 #{}
and #{id}
and id like '%'||#{id}||'%' -- concat
if문 <if test=""> - 예제도 그렇지만 보통 null 체크할때 많이 쓴다.
<select id="findActiveBlogWithTitleLike" resultType="Blog">
SELECT * FROM BLOG
WHERE state = ‘ACTIVE’
<if test="title != null">
AND title like #{title}
</if>
</select>
choose,when,otherwise
- 게시판에서 전체/제목/내용에 따라 검색방법을 달리할때 처럼
해당 변수가 조회타입(?)의 성격을 가질때 사용하면 좋을듯.
<select id="findActiveBlogLike"
resultType="Blog">
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
<choose>
<when test="title != null">
AND title like #{title}
</when>
<when test="author != null and author.name != null">
AND author_name like #{author.name}
</when>
<otherwise>
AND featured = 1
</otherwise>
</choose>
</select>
insert, update, delete
<insert id="insertAuthor">
insert into Author (id,username,password,email,bio)
values (#{id},#{username},#{password},#{email},#{bio})
</insert>
<update id="updateAuthor">
update Author set
username = #{username},
password = #{password},
email = #{email},
bio = #{bio}
where id = #{id}
</update>
<delete id="deleteAuthor">
delete from Author where id = #{id}
</delete>
insert시 key 값을 자동으로 +1을 하고 싶을때는 keyProperty 구문을 사용
<insert id="insertAuthor">
<selectKey keyProperty="id" resultType="int" order="BEFORE">
select CAST(RANDOM()*1000000 as INTEGER) a from SYSIBM.SYSDUMMY1
</selectKey>
insert into Author
(id, username, password, email,bio, favourite_section)
values
(#{id}, #{username}, #{password}, #{email}, #{bio}, #{favouriteSection,jdbcType=VARCHAR})
</insert>
insert 구분이 실행되기전에 selectKey가 먼저 수행되어 id값을 만들고 insert문에서 사용한다.참고 사이트 http://mybatis.github.io/mybatis-3/ko/index.html
댓글
댓글 쓰기