Home > Java > Java Tutorial > body text

What are the parameter passing methods for Mybatis paging query in Java?

王林
Release: 2023-05-19 10:16:19
forward
890 people have browsed it

1. Passing parameters in order

In SQL, there are two ways to express the order of parameters, namely using arg0, arg1... and using param1, param2.... This method is less readable and is not recommended for use in development. In my test, the definition of parameters is not limited to the above format, you can define it at will

1. Persistence layer interface method

/**
     * 分页查询 -- 顺序传参
     * @param startIndex 开始索引
     * @param pageSize 每页条数
     * @return
     */
    List findPage(int startIndex,int pageSize);
Copy after login

2. UserMapper.xml mapping file new Add tag


    
Copy after login

Here I found that it is possible to define the parameter type without defining the type

3. Add new test method

// 测试分页查询方法 -- 顺序传参
    @Test
    public void testFindPage(){
        List users = userMapper.findPage(0,3);
        users.forEach(System.out::println);
    }
Copy after login

4. Running results

What are the parameter passing methods for Mybatis paging query in Java?

Since the starting number here starts from 0 and the data ID starts from 1, so don’t be surprised

2. @param parameter passing

Define the parameter name through @Param in the parameter list of the interface method, and specify the parameter position through the parameter name defined in the annotation in the Sql statement. The parameters of this method are relatively intuitive and are recommended.

1. Persistence layer interface method

/**
     * 分页查询 -- @param传参
     * @param startIndex 开始索引
     * @param pageSize 每页条数
     * @return
     */
    List findPage1(@Param("startIndex")int startIndex,@Param("pageSize")int pageSize);
Copy after login

2. New tag for UserMapper.xml mapping file


    
Copy after login

Note, here The parameters must be consistent with the parameters of the persistence layer interface, otherwise an error will be reported

3. New test method

// 测试分页查询方法 -- @param传参
    @Test
    public void testFindPage1(){
        List users = userMapper.findPage1(3,3);
        users.forEach(System.out::println);
    }
Copy after login

4. Running results

What are the parameter passing methods for Mybatis paging query in Java?

3. Custom POJO class parameter passing

Custom POJO class, the attributes of this class are the parameters to be passed, when binding parameters in SQL statements Just use the POJO property name as the parameter name. This method is recommended.

1. Custom POJO class

Since we need two parameters here, one is the starting number of queries, and the other is the number of entries per page, so this The pojo class only requires two parameters

package com.mybatisstudy.pojo;
 
public class PageQuery {
    private int startIndex;
    private int pageSize;
 
    public PageQuery(int i, int i1) {
        this.startIndex = i;
        this.pageSize = i1;
    }
 
    public int getStartIndex() {
        return startIndex;
    }
 
    public void setStartIndex(int startIndex) {
        this.startIndex = startIndex;
    }
 
    public int getPageSize() {
        return pageSize;
    }
 
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
}
Copy after login

2. Persistence layer interface method

/**
     * 分页查询 -- POJO传参
     * @param PageQuery
     * @return
     */
    List findPage2(PageQuery pageQuery);
Copy after login

3. New tag for UserMapper.xml mapping file


    
Copy after login

It is also important to note here that the parameter name must be consistent with the member variable name of the custom POJO class, otherwise an error will be reported

4. Add a new test method

// 测试分页查询方法 -- POJO传参
    @Test
    public void testFindPage2(){
        PageQuery pageQuery = new PageQuery(2,3);
        List users = userMapper.findPage2(pageQuery);
        users.forEach(System.out::println);
    }
Copy after login

5. Running results

What are the parameter passing methods for Mybatis paging query in Java?

4. Map parameter passing

If you don’t want to customize POJO, you can use Map as The carrier for passing parameters, just use the Map Key as the parameter name when binding parameters in the SQL statement. This method is recommended

1. Persistence layer interface method

/**
     * 分页查询 -- Map传参
     * @param Map
     * @return
     */
    List findPage3(Map params);
Copy after login

2. New tag for UserMapper.xml mapping file


    
Copy after login

Here It is also important to note that the number of parameters must be consistent with the number of your map collection, and the parameter name must be consistent with the name of the key in the map collection, otherwise an error will be reported

3. New test method

// 测试分页查询方法 -- Map传参
    @Test
    public void testFindPage3(){
        Map params = new HashMap<>();
        params.put("startIndex",0);
        params.put("pageSize",4);
        List users = userMapper.findPage3(params);
        users.forEach(System.out::println);
    }
Copy after login

4. Running results

What are the parameter passing methods for Mybatis paging query in Java?

The above is the detailed content of What are the parameter passing methods for Mybatis paging query in Java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!