It is said that it is the best use, it is definitely not a prank, but many people don’t understand why they use this plugin. Isn’t it good to use the sql by handwriting and pagination of SQL...
So I specially wrote an example like this to explain why it is the best.
Suppose we have written the Mapper interface and xml, as follows:
public interface SysLoginLogMapper { /** * Query the login log based on query conditions* @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>Although it is a simple example, the XML here is not that simple.
If you already have some ready-made Mybatis methods like the above, and I want to paginate this query now, what should I do?
If it is handwritten SQL, I need to add two interfaces, one querying the total count, and the other changing to pagination. It doesn't seem difficult to copy and paste in XML, and then change the statement. Did you do this?
If using this plugin, what do I need to do? ? ?
For these methods that Mybatis has written, I don’t need to change anything.
But the Service layer may need to be moved. Specific example above. Look at the Service layer call code below.
Code when paging is not 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(); } In comparison:
The return value has been changed from List<SysLoginLog> to PageHelper.Page<SysLoginLog>
Two parameters have been added, pageNumber and pageSize
Then in the process code, it is called first
PageHelper.startPage(pageNumber,pageSize);
startPage tells the interceptor that I am going to start paging. The paged parameters are these two.
Then call the original Mybatis code:
sysLoginLogMapper.findSysLoginLog(loginIp, username, loginDate, exitDate, logerr);
Is it strange that there is no return value received here? In fact, PageHelper has automatically received the return value. The return value can be retrieved through the following code:
PageHelper.endPage();
At the same time, endPage told the interceptor that I have ended the paging and I don't need you anymore.
Do you think such code is simple or handwritten SQL is simple?
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.