MyBatis空where拦截器

最近项目中出现了因为Mybatis的动态where条件不满足导致实际sql语句的where条件为空,进而更新了全表
如何禁止这种情况,个人觉得三种措施:
● 1.在逻辑层面加充分的参数有效性检查;
● 2.在where条件中如果索引条件都不满足,加上1=2这种必然失败的条件;

● 3.Mybatis拦截器
前两种措施都是依赖人,从这个层面讲,是不靠谱的,即一个策略不是强制的,就是不靠谱的.相对而言,第三种是不依赖程序员的自觉性,是最靠谱的

MyBatis提供了一种插件(plugin)的功能,虽然叫做插件,但其实这是拦截器功能。

MyBatis 允许你在已映射语句执行过程中的某一点进行拦截调用。默认情况下,MyBatis 允许使用插件来拦截的方法调用包括:

Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
拦截执行器的方法

ParameterHandler (getParameterObject, setParameters)
拦截参数的处理

ResultSetHandler (handleResultSets, handleOutputParameters)
拦截结果集的处理

StatementHandler (prepare, parameterize, batch, update, query)
拦截Sql语法构建的处理

实现:

package org.apache.ibatis.plugin;

import java.util.Properties;

/**

  • @author Clinton Begin
    */
    public interface Interceptor {

Object intercept(Invocation invocation) throws Throwable;

Object plugin(Object target);

void setProperties(Properties properties);

}

intercept:它将直接覆盖你所拦截的对象,有个参数Invocation对象,通过该对象,可以反射调度原来对象的方法;
plugin:target是被拦截的对象,它的作用是给被拦截对象生成一个代理对象;
setProperties:允许在plugin元素中配置所需参数,该方法在插件初始化的时候会被调用一次;

明确拦截器对什么方法启用
因为我们是要对sql语句进行拦截,所以我们拦截的应该是StatementHandler的prepare方法

具体代码:

@Intercepts({ @Signature(type = StatementHandler.class,
method = “prepare”,
args = { Connection.class, Integer.class }) })
@Component
public class EmptyWhereInterceptor implements Interceptor {

private static final Logger logger = LoggerFactory.getLogger(EmptyWhereInterceptor.class);
/**
 * 拦截的 COMMAND 类型
 */
private static final Set<String> INTERCEPTOR_COMMAND =  new HashSet<String>() {{
    add("update");
    add("delete");
}};


@Override
public Object intercept(Invocation invocation) throws Throwable {
    logger.info("----进入拦截器");


    //对于StatementHandler其实只有两个实现类,一个是RoutingStatementHandler,另一个是抽象类BaseStatementHandler,
    //BaseStatementHandler有三个子类,分别是SimpleStatementHandler,PreparedStatementHandler和CallableStatementHandler,
    StatementHandler handler = (StatementHandler) invocation.getTarget();


    //Mybatis在进行Sql语句处理的时候都是建立的RoutingStatementHandler,
    // 里面有个StatementHandler类型的delegate变量,其实现类是BaseStatementHandler
    if (handler instanceof RoutingStatementHandler) {
        handler = (BaseStatementHandler) ReflectUtil.getFieldValue(handler, "delegate");
    }


    //BaseStatementHandler的成员变量mappedStatement
    //获取SqlCommandType
    String commandType = getCommandType(handler);


    if (INTERCEPTOR_COMMAND.contains(commandType)) {
        //获取sql
        String originSql = handler.getBoundSql().getSql().toLowerCase();
        if (!originSql.contains("where")) {
            logger.error("Prohibit the use of SQL statements without where conditions.originSql={}", originSql);
            throw new RuntimeException("Prohibit the use of SQL statements without where conditions.originSql"+originSql);

        }
    }

    return invocation.proceed();
}

@Override
public Object plugin(Object target) {
    return Plugin.wrap(target, this);
}

@Override
public void setProperties(Properties properties) {
}

/**
 * 获取Command类型,小写化返回
 *
 * @param handler
 * @return
 */
private String getCommandType(StatementHandler handler) {
    MappedStatement mappedStatement = (MappedStatement) ReflectUtil.getFieldValue(handler, "mappedStatement");
    return mappedStatement.getSqlCommandType().toString().toLowerCase();
}

}

涉及一个反射的工具类

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

/**

  • @author lihuiyue

  • @date 2019-01-21
    */
    public class ReflectUtil {
    public ReflectUtil() {
    }

    /**

    • 改变 Accessible,便于访问private等属性
    • @param field
      */
      private static void makeAccessible(Field field) {
      if(!Modifier.isPublic(field.getModifiers())) {
      field.setAccessible(true);
      }
      }

    /**

    • 获取 object 的字段,字段名称为filedName,获取不到返回null

    • @param object

    • @param filedName

    • @return
      */
      private static Field getDeclaredField(Object object, String filedName) {
      Class superClass = object.getClass();

      while(superClass != Object.class) {
      try {
      return superClass.getDeclaredField(filedName);
      } catch (NoSuchFieldException var4) {
      superClass = superClass.getSuperclass();
      }
      }

      return null;
      }

    /**

    • 获取object字段fieldName的值,如果字段不存在直接抛异常

    • @param object

    • @param fieldName

    • @return
      */
      public static Object getFieldValue(Object object, String fieldName) {
      Field field = getDeclaredField(object, fieldName);
      if(field == null) {
      throw new IllegalArgumentException(“Could not find field [” + fieldName + “] on target [” + object + “]”);
      } else {
      makeAccessible(field);
      Object result = null;

       try {
           result = field.get(object);
       } catch (IllegalAccessException var5) {
           var5.printStackTrace();
       }
      
       return result;
      

      }
      }

    /**

    • 设置object字段fieldName的值,如果字段不存在直接抛异常

    • @param object

    • @param fieldName

    • @param value
      */
      public static void setFieldValue(Object object, String fieldName, Object value) {
      Field field = getDeclaredField(object, fieldName);
      if(field == null) {
      throw new IllegalArgumentException(“Could not find field [” + fieldName + “] on target [” + object + “]”);
      } else {
      makeAccessible(field);

       try {
           field.set(object, value);
       } catch (IllegalAccessException var5) {
           var5.printStackTrace();
       }
      

      }
      }
      }

测试:

问题:在batch项目中这个mybatis拦截器失效
原因:因为batch 是个多数据源的项目,每个数据源我们都自定义了SqlSessionFactory,导致此拦截器没有注入。在创建SqlSessionFactory的时候,具体代码如下:

注入拦截器
@Autowired
private EmptyWhereInterceptor emptyWhereInterceptor;

SqlSessionFactoryBean中设置拦截器
sqlSessionFactoryBean.setPlugins(new Interceptor[]{emptyWhereInterceptor});

这里碰到一个坑,就是设置plugins时必须在sqlSessionFactoryBean.getObject()之前

可跟踪源码看到:

sqlSessionFactory = sqlSessionFactoryBean.getObject();

@Override
public SqlSessionFactory getObject() throws Exception {
if (this.sqlSessionFactory == null) {
afterPropertiesSet();
}

return this.sqlSessionFactory;
}

@Override
public void afterPropertiesSet() throws Exception {
notNull(dataSource, “Property ‘dataSource’ is required”);
notNull(sqlSessionFactoryBuilder, “Property ‘sqlSessionFactoryBuilder’ is required”);
state((configuration == null && configLocation == null) || !(configuration != null && configLocation != null),
“Property ‘configuration’ and ‘configLocation’ can not specified with together”);

this.sqlSessionFactory = buildSqlSessionFactory();
}

buildSqlSessionFactory()

if (!isEmpty(this.plugins)) {
for (Interceptor plugin : this.plugins) {
configuration.addInterceptor(plugin);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(“Registered plugin: '” + plugin + “’”);
}
}
}

最后贴上正确的配置代码(DataSourceSqlSessionFactory代码片段)

@Bean
public SqlSessionFactory sqlSessionFactoryV1(){
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSourceV1);
try {
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION));
} catch (IOException e) {
logger.error(“创建V1 SqlSessionFactory 异常”,e);
throw new RuntimeException(“创建V1 SqlSessionFactory 异常”);
}
SqlSessionFactory sqlSessionFactory = null;
try {
sqlSessionFactoryBean.setPlugins(new Interceptor[]{emptyWhereInterceptor});
sqlSessionFactory = sqlSessionFactoryBean.getObject();
} catch (Exception e) {
logger.error(“创建V1 SqlSessionFactory 异常”,e);
throw new RuntimeException(“创建V1 SqlSessionFactory 异常”);
}
return sqlSessionFactory;
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: MyBatis-Plus是基于MyBatis的增强工具库,它提供了一系列的功能增强,其中就包括拦截器拦截器MyBatis执行SQL语句时的一个重要环节,可以在执行SQL语句之前或之后对语句进行处理,实现自定义逻辑。 MyBatis-Plus的拦截器可以使用自定义的逻辑来实现添加where条件的功能。具体操作如下: 1.实现自定义的Interceptor拦截器类,并覆盖该类的intercept()方法。 2.在该方法中通过Invocation对象获取当前的MappedStatement、BoundSql和参数列表等对象。 3.根据当前的MappedStatement对象获取到对应的SQL语句,并在该SQL语句的后面添加where条件。 4.创建新的BoundSql对象,将修改过的SQL语句和参数列表注入到BoundSql对象中。 5.通过反射将修改后的BoundSql对象覆盖原有的BoundSql对象。 6.最后将Invocation对象返回即可。 示例代码如下: public class MyInterceptor implements Interceptor { @Override public Object intercept(Invocation invocation) throws Throwable { MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0]; Object parameter = invocation.getArgs()[1]; BoundSql boundSql = mappedStatement.getBoundSql(parameter); String sql = boundSql.getSql(); // 在该SQL语句的后面添加where条件 sql = sql + " where 1=1"; BoundSql newBoundSql = new BoundSql(mappedStatement.getConfiguration(), sql, boundSql.getParameterMappings(), parameter); // 利用反射将新的BoundSql对象覆盖原有的BoundSql对象 Field field = boundSql.getClass().getDeclaredField("sql"); field.setAccessible(true); field.set(boundSql, newBoundSql.getSql()); return invocation.proceed(); } } 上述代码中的where条件为固定值"where 1=1",你可以根据需要修改为具体的条件。同时,我们也可以使用更加灵活的方式进行拦截器的使用和注入。例如,在MyBatis的配置文件中添加以下配置: <plugins> <plugin interceptor=”com.example.MyInterceptor”/> </plugins> 其中,com.example.MyInterceptor为你实现的自定义拦截器类的完整类名。这样,我们就可以直接将这个拦截器注入到MyBatis中,实现添加where条件的效果。 ### 回答2: Mybatis-Plus是一款基于Mybatis的增强工具包,可以方便地进行CRUD操作。Mybatis-Plus提供了很多的增强功能,其中就包括拦截器功能。 拦截器Mybatis-Plus实现增强功能的一种方式。Mybatis-Plus的拦截器是基于Mybatis拦截器实现的,通过拦截器可以在Sql语句执行前、执行后、异常时进行处理,达到自定义增强Sql语句的目的。 添加Where条件是在查询时经常使用的功能。Mybatis-Plus提供了方便的方式来实现拦截器添加Where条件的功能。具体步骤如下: 1. 创建拦截器类 创建一个拦截器类,实现Mybatis的Interceptor接口,并在实现类中实现intercept方法。 2. 复写intercept方法 在intercept方法中获取执行的Sql语句,判断是否查询语句,如果是查询语句,则获取查询条件,并在Sql语句中添加Where条件。 3. 配置拦截器 将编写好的拦截器配置到Mybatis-Plus的SqlSessionFactoryBean中,可以使用Mybatis-Plus提供的全局拦截器GlobalConfig进行配置。 通过以上步骤,可以很方便地实现拦截器添加Where条件的功能。使用拦截器添加Where条件的好处是可以统一管理,不需要在查询业务上重复添加Where条件。同时,拦截器也可以实现很多自定义功能,例如分页、参数校验等。 ### 回答3: Mybatis-Plus 是一款基于 Mybatis 的增强工具,在 Mybatis の基础上提供了很多实用的功能。其中,拦截器就是其重要的一部分。拦截器能够在 Mybatis-Plus 的执行流程中插入一些自己的逻辑,这些逻辑可以用来增强、定制 Mybatis-Plus 的功能,比如添加 where 条件。 Mybatis-Plus 的拦截器机制是以 Mybatis拦截器机制为基础的。Mybatis-Plus 在 Mybatis拦截器机制基础上,实现了自己的拦截器接口 Interceptor。实现自己的拦截器,需要实现 Interceptor 接口,然后重写 intercept 方法,在中拦截器方法中编写所需的逻辑,比如添加 where 条件。 添加 where 条件主要是在 SQL 执行的过程中插入一些条件语句,这些语句用于限制查询结果的范围。在 Mybatis 中,可以通过 ParameterHandler 来获取参数,通过 BoundSql 来获取 SQL 语句,并且在 BoundSql 中插入 where 条件语句。在具体的实现中,通过自定义 Mybatis-Plus 的拦截器,就可以轻松地实现添加 where 条件功能。 具体实现步骤如下: 1.自定义拦截器类,实现 Interceptor 接口 2.在 intercept 方法中实现自己的逻辑,获取参数并修改 BoundSql 3.在 Mybatis-Plus 配置文件中配置该拦截器 4.运行代码,查看效果 需要注意的是,添加 where 条件语句必须符合 SQL 语法,否则会导致 SQL 执行失败。另外,如果在拦截器中修改 BoundSql,一定要注意修改之后 SQL 的正确性,以免影响查询结果。 总之,Mybatis-Plus 的拦截器机制为我们提供了很多定制 Mybatis-Plus 功能的方便,添加 where 条件就是其中之一,可以根据实际需求,自定义实现自己的拦截器,并在其中添加需要的 where 条件,以实现更加灵活的查询功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值