Spring, Mybatis, Mysql realizes paging through stored procedures. There are three parts to the blog.
The first part: the stored procedure implementation
of stored procedure dynamic paging. The second part: the Mybatis implementation of stored procedure dynamic paging. The third part: the
actual project demo of stored procedure dynamic paging.
This article is about the
second part: Mybatis implementation of dynamic paging of stored procedures Mybatis implements
paging for any query by calling dynamic_paging stored procedures.
Mybatis can call stored procedures. For example, in Mybatis' mapper file:
<select id="get***" resultMap="**Map" parameterMap="procMap" statementType="CALLABLE"> CALL proc(?,?) </select> <parameterMap type="java.util.Map" id="procMap"> <parameter property="param1" mode="IN" jdbcType="INTEGER" /> <parameter property="param1" mode="IN" jdbcType="INTEGER" /> </parameterMap>
Paginated stored procedure
CREATE PROCEDURE `dynamic_paging`(sql varchar(500),page_begin int,size int)
The problem is that the first parameter (sql) of the dynamic_paging stored procedure needs to be dynamically generated before the call.
E.g:
select * from tableA,tableB where tableA.id=tableB.uid and id=10
The sql when Mybatis calls is:
select * from tableA,tableB where tableA.id=tableB.uid and id= ?
id=10 This is passed in by the program. It is a specific business data.
And this part is the first parameter of calling dynamic_paging.
The specific solution is: MyBatis Velocity, link http://www.mybatis.org/velocity-scripting/index.html
to dynamically fill the business logic value in the configuration file, and then pass it to the stored procedure
for example:
<select id="get***" resultMap="***Map" parameterMap="procMap" statementType="CALLABLE" lang="velocity" > #set( $sql = 'select * from tableA,tableB where tableA.id=tableB.uid and id='+$_parameter.id ) #set( $begin=$_parameter.pageBegin) #set( $size=$_parameter.fetchSize) CALL dynamic_paging(@{sql},@{begin},@{size}) </select> <parameterMap type="java.util.Map" id="procMap"> <parameter property="id" /> <parameter property="pageBegin" /> <parameter property="fetchSize"/> </parameterMap>