如果沒有流式查詢,我們想要從數(shù)據(jù)庫取 1000 萬條記錄而又沒有足夠的內(nèi)存時(shí),就不得不分頁查詢,而分頁查詢效率取決于表設(shè)計(jì),如果設(shè)計(jì)的不好,就無法執(zhí)行高效的分頁查詢。因此流式查詢是一個(gè)數(shù)據(jù)庫訪問框架必須具備的功能。
流式查詢的過程當(dāng)中,數(shù)據(jù)庫連接是保持打開狀態(tài)的,因此要注意的是:執(zhí)行一個(gè)流式查詢后,數(shù)據(jù)庫訪問框架就不負(fù)責(zé)關(guān)閉數(shù)據(jù)庫連接了,需要應(yīng)用在取完數(shù)據(jù)后自己關(guān)閉。
MyBatis 流式查詢接口
MyBatis 提供了一個(gè)叫 org.apache.ibatis.cursor.Cursor 的接口類用于流式查詢,這個(gè)接口繼承了 java.io.Closeable 和 java.lang.Iterable 接口,由此可知:
1、Cursor 是可關(guān)閉的;
2、Cursor 是可遍歷的。
除此之外,Cursor 還提供了三個(gè)方法:
1、isOpen():用于在取數(shù)據(jù)之前判斷 Cursor 對(duì)象是否是打開狀態(tài)。只有當(dāng)打開時(shí) Cursor 才能取數(shù)據(jù);
2、isConsumed():用于判斷查詢結(jié)果是否全部取完。
3、getCurrentIndex():返回已經(jīng)獲取了多少條數(shù)據(jù)
因?yàn)?Cursor 實(shí)現(xiàn)了迭代器接口,因此在實(shí)際使用當(dāng)中,從 Cursor 取數(shù)據(jù)非常簡單:
cursor.forEach(rowObject -> {...});
但構(gòu)建 Cursor 的過程不簡單
我們舉個(gè)實(shí)際例子。下面是一個(gè) Mapper 類:
1
2
3
4
5
@Mapper
public interface FooMapper {
? ? @Select("select * from foo limit #{limit}")
? ? Cursor<Foo> scan(@Param("limit") int limit);
}
方法 scan() 是一個(gè)非常簡單的查詢。通過指定 Mapper 方法的返回值為 Cursor 類型,MyBatis 就知道這個(gè)查詢方法一個(gè)流式查詢。
然后我們?cè)賹懸粋€(gè) SpringMVC Controller 方法來調(diào)用 Mapper(無關(guān)的代碼已經(jīng)省略):
@GetMapping("foo/scan/0/{limit}")
public void scanFoo0(@PathVariable("limit") int limit) throws Exception {
? ? try (Cursor<Foo> cursor = fooMapper.scan(limit)) {
? ? ? ? cursor.forEach(foo -> {});? ? ? ? ? ? ? ? ? ?
? ? }
}
上面的代碼中,fooMapper 是 @Autowired 進(jìn)來的。注釋 1 處調(diào)用 scan 方法,得到 Cursor 對(duì)象并保證它能最后關(guān)閉;2 處則是從 cursor 中取數(shù)據(jù)。
上面的代碼看上去沒什么問題,但是執(zhí)行 scanFoo0() 時(shí)會(huì)報(bào)錯(cuò):
java.lang.IllegalStateException: A Cursor is already closed.
這是因?yàn)槲覀兦懊嬲f了在取數(shù)據(jù)的過程中需要保持?jǐn)?shù)據(jù)庫連接,而 Mapper 方法通常在執(zhí)行完后連接就關(guān)閉了,因此 Cusor 也一并關(guān)閉了。
所以,解決這個(gè)問題的思路不復(fù)雜,保持?jǐn)?shù)據(jù)庫連接打開即可。我們至少有三種方案可選。關(guān)注公眾號(hào)Java技術(shù)棧獲取 Mybatis 及更多面試題帶答案。
方案一:SqlSessionFactory
我們可以用 SqlSessionFactory 來手工打開數(shù)據(jù)庫連接,將 Controller 方法修改如下:
1
@GetMapping("foo/scan/1/{limit}")
public void scanFoo1(@PathVariable("limit") int limit) throws Exception {
? ? try (
? ? ? ? SqlSession sqlSession = sqlSessionFactory.openSession();
? ? ? ? Cursor<Foo> cursor =
? ? ? ? ? ? ? sqlSession.getMapper(FooMapper.class).scan(limit)?
? ? ) {
? ? ? ? cursor.forEach(foo -> { });
? ? }
}
上面的代碼中,1 處我們開啟了一個(gè) SqlSession (實(shí)際上也代表了一個(gè)數(shù)據(jù)庫連接),并保證它最后能關(guān)閉;2 處我們使用 SqlSession 來獲得 Mapper 對(duì)象。這樣才能保證得到的 Cursor 對(duì)象是打開狀態(tài)的。
方案二:TransactionTemplate
在 Spring 中,我們可以用 TransactionTemplate 來執(zhí)行一個(gè)數(shù)據(jù)庫事務(wù),這個(gè)過程中數(shù)據(jù)庫連接同樣是打開的。代碼如下:
14
@GetMapping("foo/scan/2/{limit}")
public void scanFoo2(@PathVariable("limit") int limit) throws Exception {
? ? TransactionTemplate transactionTemplate =
? ? ? ? ? ? new TransactionTemplate(transactionManager);
? ? transactionTemplate.execute(status -> {? ? ? ? ? ? ?
? ? ? ? try (Cursor<Foo> cursor = fooMapper.scan(limit)) {
? ? ? ? ? ? cursor.forEach(foo -> { });
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? return null;
? ? });
}
上面的代碼中,1 處我們創(chuàng)建了一個(gè) TransactionTemplate 對(duì)象(此處 transactionManager 是怎么來的不用多解釋,本文假設(shè)讀者對(duì) Spring 數(shù)據(jù)庫事務(wù)的使用比較熟悉了),2 處執(zhí)行數(shù)據(jù)庫事務(wù),而數(shù)據(jù)庫事務(wù)的內(nèi)容則是調(diào)用 Mapper 對(duì)象的流式查詢。注意這里的 Mapper 對(duì)象無需通過 SqlSession 創(chuàng)建。
方案三:@Transactional 注解
這個(gè)本質(zhì)上和方案二一樣,代碼如下:
@GetMapping("foo/scan/3/{limit}")
@Transactional
public void scanFoo3(@PathVariable("limit") int limit) throws Exception {
? ? try (Cursor<Foo> cursor = fooMapper.scan(limit)) {
? ? ? ? cursor.forEach(foo -> { });
? ? }
}
USB Microphone https://www.soft-voice.com/
Wooden Speakers? https://www.zeshuiplatform.com/
亞馬遜測(cè)評(píng) www.yisuping.cn
深圳網(wǎng)站建設(shè)www.sz886.com