- Use of if statement
We can use the if statement where we need to conditionally include parts of a where clause.
eg: Search Employee. Employee name and role can be added as filter fields.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<select id="searchEmployee" resultType="Employee"> | |
SELECT * FROM Employee | |
WHERE status = 'ACTIVE' | |
<if test="empName != null"> | |
AND empName LIKE #{empName} | |
</if> | |
<if test="empRole != null"> | |
AND empRole = #{empRole} | |
</if> | |
</select> |
- Use of choose, when, otherwise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<select id="findEmployeesByLocation" resultType="Employee"> | |
SELECT * FROM Employee WHERE status = 'ACTIVE' | |
<choose> | |
<when test="locationType == 'local'"> | |
AND local_loc_code = #{loc_code} | |
</when> | |
<when test="locationType == 'remote'"> | |
AND local_loc_code = #{loc_code} | |
</when> | |
<otherwise> | |
AND loc_code = #{loc_code} | |
</otherwise> | |
</choose> | |
</select> |
- Use of where
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<select id="searchEmployee" resultType="Employee"> | |
SELECT * FROM Employee | |
<where> | |
<if test="status != null"> | |
status = #{status} | |
</if> | |
<if test="empName != null"> | |
AND empName LIKE #{empName} | |
</if> | |
<if test="empRole != null"> | |
AND empRole = #{empRole} | |
</if> | |
</where> | |
</select> |
- Use of set
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<update id="updateEmployee"> | |
update Employee | |
<set> | |
<if test="firstName != null">firstName=#{firstName},</if> | |
<if test="lastName != null">lastName=#{lastName},</if> | |
<if test="email != null">email=#{email},</if> | |
<if test="mobileNo != null">mobileNo=#{mobileNo}</if> | |
</set> | |
where id=#{id} | |
</update> |
- Use of foreach
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<select id="selectEmployeesIn" resultType="Employee"> | |
SELECT * | |
FROM Employee | |
WHERE id in | |
<foreach item="item" index="index" collection="list" | |
open="(" separator="," close=")"> | |
#{item} | |
</foreach> | |
</select> |
No comments:
Post a Comment