MyBatis Paging plug-in pagehelper simple use _ibaties

Source: Internet
Author: User

For the use of MyBatis, the most headache is to write pagination, you need to write a query count of the SELECT statement, and then write a real page query statement, when the query conditions are many, you will find that really do not want to spend twice the time to write count and select,

So I simply wrote a test project.

1 Configuration: Sqlmap-config.xml

<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE configuration Public "-//mybatis.org//dtd Config 3.0//en" "Http://mybatis.org/dtd/mybatis-3-config.dtd" >
<configuration>
    <!--debug mode to open SQL print-->
    <settings>
        <setting name= "Logimpl" Value= "stdout_logging"/>
        <setting name= "Mapunderscoretocamelcase" value= "true"/>
    </settings >
    <plugins>
        <plugin interceptor= " Com.zhongan.tech.cashier.api.intercepter.MybatisLocalCacheInterceptor "></plugin>
        <plugin Interceptor= "Com.github.pagehelper.PageInterceptor" ></plugin>
    </plugins>

</ Configuration>


2, write Mapper.xml file

Test engineering is not complicated, simple query a table, no conditions

<select id= "selectbypageandselections" resultmap= "Baseresultmap" >
        select * from Doc ORDER by
        DOC_ Abstract
    </select>

Then write the corresponding interface in the Mapper.java

Public list<doc> selectbypageandselections ();
3, paging
@Service public
class Docserviceimpl implements Idocservice {
    @Autowired
    private docmapper docmapper;

    @Override public
    pageinfo<doc> selectDocByPage1 (int currentpage, int pageSize) {
        pagehelper.startpage ( CurrentPage, pageSize);
        list<doc> docs = docmapper.selectbypageandselections ();
        pageinfo<doc> PageInfo = new pageinfo<> (docs);
        Return PageInfo
    }
}

The reference documentation explains that I used Pagehelper.startpage (currentpage, pageSize);

I think this way does not invade mapper code.

In fact, the first time I saw this code, I think it should be memory paging. In fact, the plug-in has enhanced the MyBatis execution process, adding limit and count queries, which are physical paging

Then paste a paragraph in the document description

4. When will lead to unsafe paging.

The Pagehelper method uses a static ThreadLocal parameter, and the paging parameter and thread are bound. This is safe as long as you can guarantee that the MyBatis query method is immediately followed by the Pagehelper method call.

Because Pagehelper automatically clears the ThreadLocal stored object in the finally code snippet. If an exception occurs before the code enters the Executor, it causes the thread to become unavailable, which is an artificial Bug (such as a mismatch in the interface method and XML, causing the mappedstatement to be found), which is not caused by the thread being unavailable and does not cause the ThreadLocal

The parameter was incorrectly used.
But if you write the following code, it is unsafe to use: pagehelper.startpage (1, 10);
List<country> list; if (param1!= null) {list = Countrymapper.selectif (param1);} else {list = new arraylist<country> ();} Because of the existence of NULL in param1, it will cause pagehelper to produce a paging parameter, but without being consumed, this parameter will remain on this thread.

When this thread is used again, it may cause a page-splitting method to consume the paging parameter, creating a baffling paging.
The above code should be written in the following way: List<country> List;
    if (param1!= null) {pagehelper.startpage (1, 10);
List = Countrymapper.selectif (param1);

else {list = new arraylist<country> ();} This is guaranteed to be safe.
If you are not comfortable with this, you can manually clean the ThreadLocal stored paging parameters, you can use as follows: List<country> List;
    if (param1!= null) {pagehelper.startpage (1, 10); try{list = Countrymapper.sElectall ();
    finally {pagehelper.clearpage (); } else {list = new arraylist<country> ()} This is not easy to read and is not necessary.

4, the result

The simple call in the controller layer then returns the JSON string as follows: As you can see, the results are based on the doc_abstract sort and return 1-10 data.

Automatic generation of Count
//pagehelper.startpage (Pagenum, pageSize);
 Pagehelper.startpage (1);
 list<cashierrecondetail> list = Cashierrecondetailmapper.querylistofday (date);
 The returned PageInfo contains all the paging information
pageinfo<cashierrecondetail> PageInfo = new pageinfo<> (list);
Executes two sql:
/lect count (1)
from Cashier_recon_detail///If the first returns to 0, the second article does not perform
/lect ... from Cashier_recon_ Detail Limit 0,10
Only limit is required, no count is written
//The last parameter indicates whether the "SELECT count (*) from ..."
//false is not executed, and true indicates execution. Now the configuration is the default execution Count
//First parameter Pagenum parameter represents the current page,
//The second pagesize indicates how many
//The last parameter is selectable per page.
pagehelper.startpage (2, 10,false);
List List = Cashierrecondetailpomapper.getlist (VO);
Only a single SQL Select is executed ... from 
cashier_recon_detail limit 0,10




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.