(七)mybatis批量操作,分頁外掛程式

2022-05-06 23:48:10 字數 4253 閱讀 3896

首先使用方式很簡單:

sqlsession sqlsession = sessionfactory.opensession(executortype.batch);

批量操作核心:改變執行sql的方式,改為batch

那麼原因是什麼呢?

首先普通方式是插入一條資料,就搭建乙個sql語句,然後傳入引數,執行sql,但是batch方式,預編譯一次,我們將sql語句的框架先搭建好,之後每次執行的時候,只需要給裡面填,並不需要再次搭建sql語句。

通過控制台日誌輸出也可以看的出來:

這是batch方式的批量插入的:

ebug [main] - ==>  preparing: insert into student ( name, loves ) values ( ?, ?) 

debug [main] - ==>parameters: 溫鴻飛(string), dsadsa(string)

debug [main] - ==>parameters: 溫鴻飛(string), dsadsa(string)

debug [main] - ==>parameters: 溫鴻飛(string), dsadsa(string)

debug [main] - ==>parameters: 溫鴻飛(string), dsadsa(string)

debug [main] - ==>parameters: 溫鴻飛(string), dsadsa(string)

debug [main] - ==>parameters: 溫鴻飛(string), dsadsa(string)

debug [main] - ==>parameters: 溫鴻飛(string), dsadsa(string)

debug [main] - ==>parameters: 溫鴻飛(string), dsadsa(string)

debug [main] - ==>parameters: 溫鴻飛(string), dsadsa(string)

debug [main] - ==>parameters: 溫鴻飛(string), dsadsa(string)

debug [main] -committing jdbc connection [com.mysql.cj.jdbc.connectionimpl@52851b44]

debug [main] - resetting autocommit to true

on jdbc connection [com.mysql.cj.jdbc.connectionimpl@52851b44]

debug [main] -closing jdbc connection [com.mysql.cj.jdbc.connectionimpl@52851b44]

debug [main] - returned connection 1384454980

to pool.

process finished with exit code

0

這是不使用batch

ebug [main] - ==>  preparing: insert into student ( name, loves ) values ( ?, ?) 

debug [main] - ==>parameters: 溫鴻飛(string), dsadsa(string)

debug [main] - <== updates: 1

debug [main] - ==> preparing: insert into student ( name, loves ) values ( ?, ?)

debug [main] - ==>parameters: 溫鴻飛(string), dsadsa(string)

debug [main] - <== updates: 1

debug [main] - ==> preparing: insert into student ( name, loves ) values ( ?, ?)

debug [main] - ==>parameters: 溫鴻飛(string), dsadsa(string)

debug [main] - <== updates: 1

debug [main] - ==> preparing: insert into student ( name, loves ) values ( ?, ?)

debug [main] - ==>parameters: 溫鴻飛(string), dsadsa(string)

debug [main] - <== updates: 1

debug [main] - ==> preparing: insert into student ( name, loves ) values ( ?, ?)

debug [main] - ==>parameters: 溫鴻飛(string), dsadsa(string)

debug [main] - <== updates: 1

debug [main] - ==> preparing: insert into student ( name, loves ) values ( ?, ?)

debug [main] - ==>parameters: 溫鴻飛(string), dsadsa(string)

debug [main] - <== updates: 1

debug [main] - ==> preparing: insert into student ( name, loves ) values ( ?, ?)

debug [main] - ==>parameters: 溫鴻飛(string), dsadsa(string)

debug [main] - <== updates: 1

debug [main] - ==> preparing: insert into student ( name, loves ) values ( ?, ?)

debug [main] - ==>parameters: 溫鴻飛(string), dsadsa(string)

debug [main] - <== updates: 1

debug [main] - ==> preparing: insert into student ( name, loves ) values ( ?, ?)

debug [main] - ==>parameters: 溫鴻飛(string), dsadsa(string)

debug [main] - <== updates: 1

debug [main] - ==> preparing: insert into student ( name, loves ) values ( ?, ?)

debug [main] - ==>parameters: 溫鴻飛(string), dsadsa(string)

debug [main] - <== updates: 1

debug [main] -committing jdbc connection [com.mysql.cj.jdbc.connectionimpl@1852a3ff]

debug [main] - resetting autocommit to true

on jdbc connection [com.mysql.cj.jdbc.connectionimpl@1852a3ff]

debug [main] -closing jdbc connection [com.mysql.cj.jdbc.connectionimpl@1852a3ff]

debug [main] - returned connection 408069119

to pool.

process finished with exit code

0

可以看到預編譯了很多次。

分頁外掛程式:pagehelper,

查詢時加入這句話,

pagehelper.startpage(pagenum,pagesize);   

之後正常查詢,就會返回相應頁數的資訊,當然還有其他很多細節的,看官網就行:

常用這兩種:

1.比如獲取當前頁,總資料量,總頁碼等,

獲取pagehelper.startpage(pagenum,pagesize);   的返回值,page之後通過各種get方法即可獲取,

2.當然也可以獲取第一頁,最後一頁,使用pageinfo

pageinfoxx = new pageinfo<>(list);

(七)mybatis批量操作,分頁外掛程式

首先使用方式很簡單 sqlsession sqlsession sessionfactory.opensession executortype.batch 批量操作核心 改變執行sql的方式,改為batch 那麼原因是什麼呢?首先普通方式是插入一條資料,就搭建乙個sql語句,然後傳入引數,執行sql...

mybatis 批量操作

foreach屬性 屬性描述 item 迴圈體中的具體物件。支援屬性的點路徑訪問,如item.age,item.info.details。具體說明 在list和陣列中是其中的物件,在map中是value。該引數為必選。collection 要做foreach的物件,作為入參時,list 物件預設用l...

mybatis批量操作

查詢id在in裡面的測試 list測試 listselectidin param idlist listidlist 查詢id在in裡面的測試 map測試,就是id在乙個map中 listselectinmap param idmap hashmapidmap 修改所有id在list中的測試 int...