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

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

王林
王林forward
2023-05-19 10:16:19990browse

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<User> findPage(int startIndex,int pageSize);

2. UserMapper.xml mapping file new Add tag

<!-- 分页查询-顺序传参 -->
    <select id="findPage" resultType="com.mybatisstudy.pojo.User">
        select * from user limit #{param1},#{param2}
    </select>

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<User> users = userMapper.findPage(0,3);
        users.forEach(System.out::println);
    }

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<User> findPage1(@Param("startIndex")int startIndex,@Param("pageSize")int pageSize);

2. New tag for UserMapper.xml mapping file

<!-- 分页查询-@param传参 -->
    <select id="findPage1" resultType="com.mybatisstudy.pojo.User">
        select * from user limit #{startIndex},#{pageSize}
    </select>

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<User> users = userMapper.findPage1(3,3);
        users.forEach(System.out::println);
    }

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;
    }
}

2. Persistence layer interface method

/**
     * 分页查询 -- POJO传参
     * @param PageQuery
     * @return
     */
    List<User> findPage2(PageQuery pageQuery);

3. New tag for UserMapper.xml mapping file

<!-- 分页查询-POJO传参 -->
    <select id="findPage2" resultType="com.mybatisstudy.pojo.User" parameterType="com.mybatisstudy.pojo.PageQuery">
        select * from user limit #{startIndex},#{pageSize}
    </select>

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<User> users = userMapper.findPage2(pageQuery);
        users.forEach(System.out::println);
    }

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<User> findPage3(Map<String,Object> params);

2. New tag for UserMapper.xml mapping file

<!-- 分页查询-Map传参 -->
    <select id="findPage3" resultType="com.mybatisstudy.pojo.User" parameterType="map">
        select * from user limit #{startIndex},#{pageSize}
    </select>

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<String,Object> params = new HashMap<>();
        params.put("startIndex",0);
        params.put("pageSize",4);
        List<User> users = userMapper.findPage3(params);
        users.forEach(System.out::println);
    }

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete