Mybatis paging plug in sample code

  • 2020-05-26 08:31:56
  • OfStack

Here said the best, definitely not blowing, but there are a lot of people do not understand why to use this plug-in, your own handwriting page sql is not good...

So let me give you an example of why this is the best thing to do.

Suppose we have written Mapper interface and xml, as follows:


public interface SysLoginLogMapper { 
  /** 
   *  Query the login log according to the query criteria  
   * @param logip 
   * @param username 
   * @param loginDate 
   * @param exitDate 
   * @return 
   */ 
  List<SysLoginLog> findSysLoginLog(@Param("logip") String logip, 
                   @Param("username") String username, 
                   @Param("loginDate") String loginDate, 
                   @Param("exitDate") String exitDate, 
                   @Param("logerr") String logerr); 
 
} 

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > 
<mapper namespace="com.easternie.sys.dao.SysLoginLogMapper"> 
 <select id="findSysLoginLog" resultType="com.easternie.sys.vo.model.SysLoginLog"> 
  select * from sys_login_log a 
  <if test="username != null and username != ''"> 
   left join sys_user b on a.userid = b.userid 
  </if> 
  <where> 
   <if test="logip!=null and logip != ''"> 
    a.logip like '%'||#{logip}||'%' 
   </if> 
   <if test="username != null and username != ''"> 
    and (b.username like '%'||#{username}||'%' or b.realname like '%'||#{username}||'%') 
   </if> 
   <if test="loginDate!=null and loginDate!=''"> 
    and to_date(substr(a.logindate,0,10),'yyyy-MM-dd') = to_date(#{loginDate},'yyyy-MM-dd') 
   </if> 
   <if test="exitDate!=null and exitDate!=''"> 
    and to_date(substr(a.EXITDATE,0,10),'yyyy-MM-dd') = to_date(#{exitDate},'yyyy-MM-dd') 
   </if> 
   <if test="logerr!=null and logerr!=''"> 
    and a.logerr like '%'||#{logerr}||'%' 
   </if> 
  </where> 
  order by logid desc 
 </select> 
</mapper> 

This is a simple example, but xml is not that simple.

What should I do if you already have some Mybatis methods like the one above and I want to page this query?

If it is handwritten SQL, I need to add two interfaces, 1 query count total, and 1 to change to pagination. I need to copy and paste in xml, and then change the statement, which doesn't seem too hard. Is that what you do?

What do I need to do to use this plugin??

For the methods that Mybatis has already written, I don't need to change anything.

But the Service layer may need to be moved one time. In the example above. See the Service layer call code below.

Code when no paging is required:


public List<SysLoginLog> findSysLoginLog(String loginIp, 
                     String username, 
                     String loginDate, 
                     String exitDate, 
                     String logerr) throws BusinessException { 
  return sysLoginLogMapper.findSysLoginLog(loginIp, username, loginDate, exitDate, logerr); 
} 

Code after adding paging function:


public PageHelper.Page<SysLoginLog> findSysLoginLog(String loginIp, 
                     String username, 
                     String loginDate, 
                     String exitDate, 
                     String logerr, 
                     int pageNumber, 
                     int pageSize) throws BusinessException { 
  PageHelper.startPage(pageNumber,pageSize); 
  sysLoginLogMapper.findSysLoginLog(loginIp, username, loginDate, exitDate, logerr); 
  return PageHelper.endPage(); 
} 

By comparison:

Return value from List < SysLoginLog > Changed to PageHelper Page < SysLoginLog >

Two more entries, pageNumber and pageSize

And then in the procedure code, it's called first


PageHelper.startPage(pageNumber,pageSize); 

startPage is to tell the interceptor that I'm going to start paging. The paging parameters are these two.

The original Mybatis code is then called:


sysLoginLogMapper.findSysLoginLog(loginIp, username, loginDate, exitDate, logerr); 

Does it surprise you that there is no return value received here? In fact, PageHelper has already received the return value automatically. The return value can be retrieved with the following code:


PageHelper.endPage(); 

At the same time, endPage tells the interceptor that I'm done paging and I don't need you anymore.

Do you think it's easier to write sql by hand or by hand?


Related articles: