Mybatis-Plus 多表联查分页
生活随笔
收集整理的這篇文章主要介紹了
Mybatis-Plus 多表联查分页
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
分析
使用的工程,依舊是 spring-boot,關于分頁,官網給出了一個單表的 demo,其實多表分頁實現原理相同,都是通過 mybatis 的攔截器
(攔截器做了什么?他會在你的 sql 執行之前,為你做一些事情,例如分頁,我們使用了 MP 不用關心 limit,攔截器為我們拼接。我們也不用關心總條數,攔截器獲取到我們 sql 后,拼接 select count(*) 為我們查詢總條數,添加到參數對象中)。
實現
1. 配置攔截器
?
@EnableTransactionManagement @Configuration @MapperScan("com.web.member.mapper") public class MybatisPlusConfig {/*** mybatis-plus SQL執行效率插件【生產環境可以關閉】*/@Beanpublic PerformanceInterceptor performanceInterceptor() {return new PerformanceInterceptor();}/** 分頁插件,自動識別數據庫類型 多租戶,請參考官網【插件擴展】*/@Beanpublic PaginationInterceptor paginationInterceptor() {return new PaginationInterceptor();} }2. mapper 接口以及 xml
?
/*** <p>* 用戶表 Mapper 接口* </p>** @author 殷天文* @since 2018-06-01*/ public interface UserMapper extends BaseMapper<User> {List<UserListModel> selectUserListPage(Pagination page ,@Param("user") UserListBean user);}這里要注意的是,這個 Pagination page 是必須要有的,否則 MP 無法為你實現分頁。
?
<select id="selectUserListPage" resultType="com.web.member.model.UserListModel">SELECT*FROMftms_user uLEFT JOIN ftms_user_level l ON u.level_id = l.idWHERE 1=1<if test="user.nickname != null">and u.nickname like "%"#{user.nickname}"%" </if></select>3. service 實現
?
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>* 用戶表 服務實現類* </p>** @author 殷天文* @since 2018-06-01*/ @Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {@Transactional(readOnly=true)@Overridepublic Page<UserListModel> selectUserListPage(UserListBean user) {Page<UserListModel> page = new Page<>(user.getCurr(), user.getNums());// 當前頁,總條數 構造 page 對象return page.setRecords(this.baseMapper.selectUserListPage(page, user));}}最后將結果集 set 到 page 對象中,page 對象的 json 結構如下
?
{"total": 48,//總記錄"size": 10,//每頁顯示多少條"current": 1,//當前頁"records": [//結果集 數組{...},{...},{...},...],"pages": 5 // 總頁數 }
?
總結
以上是生活随笔為你收集整理的Mybatis-Plus 多表联查分页的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 贪心算法之——喷水装置一(nyoj6)
- 下一篇: 贪心算法之——会场安排(nyoj14)