一、where标签案例
List<FilterUsersListQueryResDTO> filterUsers(@Param("filterUsersQueryDTO") FilterUsersQueryReqDTO filterUsersQueryDTO);
1、原始sql
简单的查询,弊端为参数必定为不为空
<select id="filterUsers" resultMap="filterUsersResultMap">
SELECT
*
FROM
SYS_USER t1
WHERE
t1.LINKMAN = #{filterUsersQueryDTO.linkman}
</select>
2、简单if标签判断是否为空
只使用if标签,弊端为当所有if标签内条件为空时报错
<select id="filterUsers" resultMap="filterUsersResultMap">
SELECT
*
FROM
SYS_USER t1
WHERE
<if test="filterUsersQueryDTO.linkman != null and filterUsersQueryDTO.linkman != ''">
t1.LINKMAN = #{filterUsersQueryDTO.linkman}
</if>
</select>
3、使用where标签后逻辑代码
使用where标签后,好处当条件全为空时,sql语句不会加where查询条件
<select id="filterUsers" resultMap="filterUsersResultMap">
SELECT
*
FROM
SYS_USER t1
<where>
<if test="filterUsersQueryDTO.linkman != null and filterUsersQueryDTO.linkman != ''">
t1.LINKMAN = #{filterUsersQueryDTO.linkman}
</if>
</where>
</select>
二、@Select注解中当参数为空则不添加该参数的判断
@Select("<script>" +
"select * from school_student " +
"where 1=1" +
"<when test='id !=null'> and id = #{id}" +
"</when>" +
"</script>")
List<SchoolStudent> querySchoolStudent4(@Param("id") Integer id);
|