问题描述
<update>
标签中使用了不带条件的更新语句
<update id="updateUser" parameterType="map">
UPDATE user SET name = #{name}, age = #{age}
</update>
<where>
标签中做了if判断,id如果为null,if语句自动去掉,相当于使用了不带条件的更新语句
<update id="updateUser" parameterType="map">
UPDATE user
<set>
<if test="name != null">name = #{name},</if>
<if test="age != null">age = #{age},</if>
</set>
<where>
<if test="id != null">id = #{id}</if>
</where>
</update>
解决办法
代码规范:id不做 null判断
<update id="updateUser" parameterType="map">
UPDATE user
<set>
<if test="name != null">name = #{name},</if>
<if test="age != null">age = #{age},</if>
</set>
<where>
id = #{id}
</where>
</update>
引入一些拦截器插件
MyBatis Plus 提供了 BlockAttackInnerInterceptor (阻断攻击Sql解析器) 插件,该插件可以阻止全表更新和删除操作。在一定程度上,保证了数据库数据的安全。
版本要求:MyBatis Plus 3.4.0 版本以上。
注入MybatisPlusInterceptor类,并配置BlockAttackInnerInterceptor拦截器:
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
return interceptor;
}
}