来源:网络
版权申明:内容来源网络,版权归原创者所有。除非无法确认,我们都会标明作者及出处,如有侵权烦请告知,我们会立即删除并表示歉意。谢谢!
往期惊喜:
接了个变态需求:生成 Excel + PDF 导出,用 Java 怎么实现?
扫码关注我们的Java架构师技术
带你全面深入Java
大家好,我是Java架构师
cursor.forEach(rowObject -> {...});
@Mapper
publicinterfaceFooMapper{
@Select("select * from foo limit #{limit}")
Cursor<Foo> scan(@Param("limit") int limit);
}
@GetMapping("foo/scan/0/{limit}")
publicvoid scanFoo0(@PathVariable("limit") int limit) throwsException{
try(Cursor<Foo> cursor = fooMapper.scan(limit)) { // 1
cursor.forEach(foo -> {}); // 2
}
}
java.lang.IllegalStateException: A Cursoris already closed.
@GetMapping("foo/scan/1/{limit}")
publicvoid scanFoo1(@PathVariable("limit") int limit) throwsException{
try(
SqlSession sqlSession = sqlSessionFactory.openSession(); // 1
Cursor<Foo> cursor =
sqlSession.getMapper(FooMapper.class).scan(limit) // 2
) {
cursor.forEach(foo -> { });
}
}
@GetMapping("foo/scan/2/{limit}")
publicvoid scanFoo2(@PathVariable("limit") int limit) throwsException{
TransactionTemplate transactionTemplate =
newTransactionTemplate(transactionManager); // 1
transactionTemplate.execute(status -> { // 2
try(Cursor<Foo> cursor = fooMapper.scan(limit)) {
cursor.forEach(foo -> { });
} catch(IOException e) {
e.printStackTrace();
}
returnnull;
});
}
@GetMapping("foo/scan/3/{limit}")
@Transactional
publicvoid scanFoo3(@PathVariable("limit") int limit) throwsException{
try(Cursor<Foo> cursor = fooMapper.scan(limit)) {
cursor.forEach(foo -> { });
}
}
最后,整理了100多套项目,赠送读者。扫码下方二维码,后台回复【赚钱】即可获取。
--END--