<selectid="list"resultType="com.java2nb.common.domain.DictDO"> select `id`,`name`,`value`,`type`,`description`,`sort`,`parent_id`,`create_by`,`create_date`,`update_by`,`update_date`,`remarks`,`del_flag` from sys_dict <where> <iftest="id != null and id != ''"> and id = #{id} </if> <iftest="name != null and name != ''"> and name = #{name} </if> <iftest="value != null and value != ''"> and value = #{value} </if> <iftest="type != null and type != ''"> and type = #{type} </if> <iftest="description != null and description != ''"> and description = #{description} </if> <iftest="sort != null and sort != ''"> and sort = #{sort} </if> <iftest="parentId != null and parentId != ''"> and parent_id = #{parentId} </if> <iftest="createBy != null and createBy != ''"> and create_by = #{createBy} </if> <iftest="createDate != null and createDate != ''"> and create_date = #{createDate} </if> <iftest="updateBy != null and updateBy != ''"> and update_by = #{updateBy} </if> <iftest="updateDate != null and updateDate != ''"> and update_date = #{updateDate} </if> <iftest="remarks != null and remarks != ''"> and remarks = #{remarks} </if> <iftest="delFlag != null and delFlag != ''"> and del_flag = #{delFlag} </if> </where> <choose> <whentest="sort != null and sort.trim() != ''"> order by ${sort} ${order} </when> <otherwise> order by id desc </otherwise> </choose> <iftest="offset != null and limit != null"> limit #{offset}, #{limit} </if> </select>
可以看到order by之后的sort不是通过预编译的形式引入参数,是直接将用户的输入拼接到order by之后,乍一眼看就是简单的order by注入,但是在where语句中如果sort不为空<if test="sort != null and sort != ''"> and sort = #{sort} </if>就会引入sort