In-depth analysis of MyBatisPlus: Manually write mapper and mapper.xml layer to achieve IPage<YourEntity> return type

foreword

When using MyBatisPlus for data access, it usually involves writing mapper interfaces and mapper.xml files to define CRUD operations. In some scenarios, we need to perform paging queries and return a data set containing pagination information. In this case, we can use the IPage interface provided by MyBatisPlus to implement pagination query, and set its return type to IPage<YourEntity>.

example

The following is an example that demonstrates how to manually write mapper and mapper.xml layers to support paging queries.

First, we need to create a mapper interface, eg YourMapper. Define the pagination query method we need in the interface:

public interface YourMapper extends BaseMapper<YourEntity> {
    
    
    IPage<YourEntity> selectYourEntities(com.baomidou.mybatisplus.core.metadata.IPage<YourEntity> page, @Param("param1") String param1, @Param("param2") String param2);
}

In the above code, we inherit BaseMapperthe interface of MyBatisPlus and pass in our entity class YourEntityas a generic parameter. Then, we define a selectYourEntitiesmethod named , which receives an com.baomidou.mybatisplus.core.metadata.IPage<YourEntity>object and two query parameters, for paging queries.

Next, we need to write SQL statements in the corresponding mapper.xml file to complete the specific paging query logic. Please note that it needs to be consistent with the method name in the mapper interface, and use <select>the label to define the query statement and set the return type to IPage<YourEntity>.

<select id="selectYourEntities" parameterType="com.baomidou.mybatisplus.core.metadata.IPage" resultMap="yourEntityResultMap">
    SELECT * FROM your_table
    WHERE param1 = #{param1}
      AND param2 = #{param2}
</select>

In the above code, we write the concrete query statement and map the result to our entity class YourEntity.

Now, we have completed the writing of mapper interface and mapper.xml file. In our business code, we can directly call selectYourEntitiesthe method to perform pagination query:

IPage<WorkLogDto> page = new Page<>(1, 10);  // 第一页,每页10条记录
IPage<YourEntity> yourEntitiesPage = yourMapper.selectYourEntities(page, "param1", "param2");
List<YourEntity> yourEntities = yourEntitiesPage.getRecords();
// 其他操作...

In the above code, we created an com.baomidou.mybatisplus.core.metadata.IPageobject to specify the page number and the number of records per page for pagination query. Then, we call selectYourEntitiesthe method to do the paged query and store the result in IPage<YourEntity>the object. Finally, we can getRecordsobtain specific data sets through methods.

Summarize

Through the above method, we can easily manually define the mapper and mapper.xml layers to support the return type of IPage<YourEntity>pagination query.
In the mapper interface layer method , just Ipage参数add the parameter type in mapper.xmlparameterType="com.baomidou.mybatisplus.core.metadata.IPage"

Guess you like

Origin blog.csdn.net/Da_zhenzai/article/details/131593251