(七)mybatis批量操作,分页插件

首先使用方式很简单:

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,

很简单,下载jar包或者在maven之中直接导入,

查询时加入这句话,

PageHelper.startPage(pageNum,pageSize);   

之后正常查询,就会返回相应页数的信息,当然还有其他很多细节的,看官网就行:https://pagehelper.github.io/docs/howtouse/

常用这两种:

 1.比如获取当前页,总数据量,总页码等,

获取PageHelper.startPage(pageNum,pageSize);   的返回值,Page<Object> 之后通过各种get方法即可获取,

2.当然也可以获取第一页,最后一页,使用pageinfo

PageInfo<classname> xx = new PageInfo<>(list);

 

 

 

 

posted @ 2019-08-16 16:06  _Ennio  阅读(352)  评论(0编辑  收藏  举报