In the previous section, it briefly talks about the construction and use of Mybatis-Plus. In this section, it briefly talks about how to use MP to implement multi-table pagination.
analyze
The project used is still spring-boot. Regarding paging, the official website gives a demo of a single table. In fact, the implementation principle of multi-table paging is the same, and they are all through mybatis interceptor
(What does the interceptor do? It will do something for you before your sql is executed, such as pagination. We use MP and don’t care about limit. The interceptor splices it for us. We don’t care about the total number of characters. After the interceptor gets our sql, splices select count(*) for us and adds it to the parameter object).
accomplish
1. Configure the interceptor
@EnableTransactionManagement@Configuration@MapperScan("com.web.member.mapper")public class MybatisPlusConfig { /** * mybatis-plus SQL execution efficiency plug-in [can be turned off in production environment] */ @Bean public PerformanceInterceptor performanceInterceptor() { return new PerformanceInterceptor(); } /* * Pagination plug-in, automatically identifying multi-tenants in database type, please refer to the official website [Plugin Extension] */ @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); }} 2. Mapper interface and xml
/** * <p> * User table Mapper interface* </p> * * @author Yin Tianwen* @since 2018-06-01 */public interface UserMapper extends BaseMapper<User> { List<UserListModel> selectUserListPage(Pagination page,@Param("user") UserListBean user); }It should be noted here that this Pagination page is necessary, otherwise MP will not be able to implement pagination for you.
<select id="selectUserListPage" resultType="com.web.member.model.UserListModel"> SELECT * FROM ftms_user u LEFT JOIN ftms_user_level l ON u.level_id = l.id WHERE 1=1 <if test="user.nickname != null"> and u.nickname like "%"#{user.nickname}"%" </if> </select> 3. Service implementation
import com.web.member.beans.admin.UserListBean;import com.web.member.entity.User;import com.web.member.mapper.UserMapper;import com.web.member.model.UserListModel;import com.web.member.service.UserService;import com.baomidou.mybatisplus.plugins.Page;import com.baomidou.mybatisplus.service.impl.ServiceImpl;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;/** * <p> * User table service implementation class* </p> * * @author Yin Tianwen* @since 2018-06-01 */@Servicepublic class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { @Transactional(readOnly=true) @Override public Page<UserListModel> selectUserListPage(UserListBean user) { Page<UserListModel> page = new Page<>(user.getCurr(), user.getNums());// The current page, the total number of characters is constructed. The page object is returned page.setRecords(this.baseMapper.selectUserListPage(page, user)); } }Finally, set the result set into the page object. The json structure of the page object is as follows
{ "total": 48,//Total records "size": 10,//How many "current" are displayed per page: 1,//Current page "records": [//Result set array {...}, {...}, {...}, ... ], "pages": 5 // Total pages}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.