为什么我的Cursor为空?

时间:2018-01-05 01:34:16

标签: spring cursor mybatis transactional

我正在尝试使用MyBatis的Cursor。

Mapper XML

<select id="selectCursor51" resultMap="someResultMap" resultOrdered="true">
    SELECT
        ...
    FROM
        ...
    <where>
        ...
    </where>
    ORDER BY
        ..., <!-- is this part can be wrong? -->
        some_id ASC
</select>

Mapper界面,

@Mapper
public interface SomeMapper {
    List<Some> selectCursor51(...);
}

Spring Service,

@Service
public class SomeService {

    @Transactional
    public <R> R some(..., final Function<Cursor<Some>, R> function) {
        ...
        final Cursor<Some> cursor = someMapper.selectCursor51(...);
        return function.apply(cursor);
    }

    @Autowired
    private SomeMapper someMapper;
}

实际查询会产生非空结果。 但是光标是空的。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我正在回答我自己对更新的追求。

该方法确实有效。

问题是有更多方法直接或间接调用该方法。

@Transactional
public <R> R some(..., final Function<Cursor<Some>, R> function) {
    return function.apply(someMapper.selectCursor51(...));
}

// not annotated with @Transactional
public void some(..., final Consumer<Some> consumer) {
    some(
        ...,
        cursor -> {
            for(Some some: cursor) {
                consumer.accept(some);
            }
        }
    );
}

当一个控制器调用一些带有@Transactional注释功能的控件时,它可以工作。

当它向消费者调用某种方法时,它没有。

我找到@Transactional method calling another method without @Transactional anotation?并解决了我的问题。

相关问题