Mybatis#4) 복합 SQL

2019. 4. 18. 10:50Mybatis

# MemberMapper.xml

 

 

<select id="selectMemberByConditionIF" parameterType="member" resultType="member">

 

select *

from member

<choose>

<when test="id != null">

where id = #{id}

</when>

<when test="name != null">

where name = #{name}

</when>

</choose>

</select>

 

<select id="selectMemberByConditionForeach" parameterType="hashmap" resultType="member">

 

select *

from member

<trim prefix="WHERE">

<if test="names != null">

 

<foreach collection="names" item ="names" index="index" open="(" close=")" separator=",">

#{name}

</foreach>

</if>

</trim>

</select>

 

<select id="selectMemberByConditionTrim" parameterType="member" resultType="member">

 

select *

from member

 

<trim prefix="WHERE" prefixOverrides="AND | OR">

<if test="id != null">

id = #{id}

</if>

<if test="name != null">

AND name = #{name}

</if>

</trim>

</select>

 

<update id="updateMemberByCondition" parameterType="member">

 

UPDATE member

 

<trim prefix="SET" suffixOverrides=",">

<if test="password != null">

password = #{password},

</if>

<if test="name != null">

name = #{name}

</if>

</trim>

password = #{password}, name = #{name}

WHERE id = #{id}

</update>