mybatis 树结构查询时出现的坑

需求期望使用mybatis的级连查询得到树形结构数据返回, 代码如下:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class DocumentTypeTreeNode {

    @ApiModelProperty("类型id")
    @JSONField(name = "value")
    private Long id;

    @ApiModelProperty("类型名称")
    @JSONField(name = "label")
    private String title;

    @ApiModelProperty("子类型")
    private List<DocumentTypeTreeNode> children;

}
<resultMap id="treeNode" type="com.zvan.doc.entity.DocumentTypeTreeNode">
    <id column="id" property="id"/>
    <result column="title" property="title"/>
    <collection column="id" property="children" select="selectTree"/>
</resultMap>

<select id="selectTree" resultMap="treeNode">
    select *
    from source_document_classification
    <where>
        <if test="id == null">
            parent_id is null
        </if>
        <if test="id != null and id != null">
            parent_id = #{id}
        </if>
    </where>
</select>

结果接口调用出现问题:
Error querying database. Cause: java.sql.SQLException: Operation not allowed after ResultSet closed

错误提示不能在ResultSet被关闭后继续操作, 说明这次级连查询出现在同一个statement中,并且一次查询的resultSet被关闭后还期望对其进行操作。

那找问题所在的思路就很清晰了: 我们的statement被复用了, 有印象的小伙伴可能记得mybatis中存在三种Executor:

  1. SimpleExecutor
  2. ReuseExecutor
  3. BatchExecutor

Reuse和Batch都有复用连接的功能, 问题可能就出现在这儿, 题主将项目配置中的REUSE改为了BATCH, 问题果然得到了解决, 本着打破沙锅问到底的精神,我们看看是为什么。

以下为ReuseExecutor部分源码:

  @Override
  public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
    Configuration configuration = ms.getConfiguration();
    StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
    Statement stmt = prepareStatement(handler, ms.getStatementLog());
    return handler.query(stmt, resultHandler);
  }

以下为BatchExecutor部分源码

  @Override
  public <E> List<E> doQuery(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql)
      throws SQLException {
    Statement stmt = null;
    try {
      flushStatements();
      Configuration configuration = ms.getConfiguration();
      StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameterObject, rowBounds, resultHandler, boundSql);
      Connection connection = getConnection(ms.getStatementLog());
      stmt = handler.prepare(connection, transaction.getTimeout());
      handler.parameterize(stmt);
      return handler.query(stmt, resultHandler);
    } finally {
      closeStatement(stmt);
    }
  }

他们的区别很简单: 他们都尝试对同一sql进行复用,不过不同的是: Reuse复用的是Statement, 而Batch复用的是Connection。 因此在Reuse情况下,会发生以上错误,而Batch中不会出现。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值