确认
- [X] 我的版本是最新版本, 我的版本号与 version 相同, 并且项目里无依赖冲突
- [X] 我已经在 issue 中搜索过, 确认问题没有被提出过
- [X] 我已经修改标题, 将标题中的 描述 替换为遇到的问题
功能改进
mybatis-plus提供了default-statement-timeout: 60参数用于配置sql查询超时时间,实际开发中有些sql需要超出这个时间,但mybatis-plus没有单独设置的功能 原生mybatis在mapper.xml可以设置timeout,mapper接口可以设置@Options,但是mybatis-plus通过代理生成的查询方法没有设置超时时间的功能 mybatis-plus在注入MappedStatement阶段timeout参数为null,这就意味着代理生成的查询方法不能自定义查询超时时间
MappedStatement被final修饰,本身无法修改,而通过MappedStatement提供的Build类重新生成再赋值内存地址变更又存在极大的安全隐患
参考资料
这是mybatis的代码,在配置的基础上优先获取MappedStatement超时时间 protected void setStatementTimeout(Statement stmt, Integer transactionTimeout) throws SQLException { Integer queryTimeout = null; if (mappedStatement.getTimeout() != null) { queryTimeout = mappedStatement.getTimeout(); } else if (configuration.getDefaultStatementTimeout() != null) { queryTimeout = configuration.getDefaultStatementTimeout(); } if (queryTimeout != null) { stmt.setQueryTimeout(queryTimeout); } StatementUtil.applyTransactionTimeout(stmt, queryTimeout, transactionTimeout); }
这是mybatis-plus的代码,生成 MappedStatement timeout参数为null protected MappedStatement addMappedStatement(Class<?> mapperClass, String id, SqlSource sqlSource, SqlCommandType sqlCommandType, Class<?> parameterType, String resultMap, Class<?> resultType, KeyGenerator keyGenerator, String keyProperty, String keyColumn) { String statementName = mapperClass.getName() + DOT + id; if (hasMappedStatement(statementName)) { logger.warn(LEFT_SQ_BRACKET + statementName + "] Has been loaded by XML or SqlProvider or Mybatis's Annotation, so ignoring this injection for [" + getClass() + RIGHT_SQ_BRACKET); return null; } / 缓存逻辑处理 / boolean isSelect = sqlCommandType == SqlCommandType.SELECT; return builderAssistant.addMappedStatement(id, sqlSource, StatementType.PREPARED, sqlCommandType, null, null, null, parameterType, resultMap, resultType, null, !isSelect, isSelect, false, keyGenerator, keyProperty, keyColumn, configuration.getDatabaseId(), languageDriver, null); }
Comment From: Ai-010
不是应该更多的去优化sql的效率,很少见去设置 timeout 和 fetchSize ; 不过 看源码 你是可以设置 事务时间