还在用分页?太Low !试试 MyBatis 流式查询,真心强大!
MyBatis 流式查询接口
cursor.forEach(rowObject -> {...});
但构建 Cursor 的过程不简单
public interface FooMapper {
Cursor
scan( int limit);}
"foo/scan/0/{limit}") (
public void scanFoo0(@PathVariable("limit") int limit) throws Exception {
try (Cursor
cursor = fooMapper.scan(limit)) { // 1 cursor.forEach(foo -> {}); // 2
}
}
java.lang.IllegalStateException: A Cursor is already closed.
方案一:SqlSessionFactory
"foo/scan/1/{limit}") (
public void scanFoo1(@PathVariable("limit") int limit) throws Exception {
try (
SqlSession sqlSession = sqlSessionFactory.openSession(); // 1
Cursor
cursor = sqlSession.getMapper(FooMapper.class).scan(limit) // 2
) {
cursor.forEach(foo -> { });
}
}
方案二:TransactionTemplate
"foo/scan/2/{limit}") (
public void scanFoo2(@PathVariable("limit") int limit) throws Exception {
TransactionTemplate transactionTemplate =
new TransactionTemplate(transactionManager); // 1
transactionTemplate.execute(status -> { // 2
try (Cursor
cursor = fooMapper.scan(limit)) { cursor.forEach(foo -> { });
} catch (IOException e) {
e.printStackTrace();
}
return null;
});
}
方案三:@Transactional 注解
"foo/scan/3/{limit}") (
public void scanFoo3(@PathVariable("limit") int limit) throws Exception {
try (Cursor
cursor = fooMapper.scan(limit)) { cursor.forEach(foo -> { });
}
}
END
推荐阅读 一键生成Springboot & Vue项目!【毕设神器】
Java可视化编程工具系列(一)
Java可视化编程工具系列(二)
顺便给大家推荐一个GitHub项目,这个 GitHub 整理了上千本常用技术PDF,绝大部分核心的技术书籍都可以在这里找到,
GitHub地址:https://github.com/javadevbooks/books
Gitee地址:https://gitee.com/javadevbooks/books
电子书已经更新好了,你们需要的可以自行下载了,记得点一个star,持续更新中..
评论