溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用Mybatis-Plus怎么實現(xiàn)多表聯(lián)查分頁

發(fā)布時間:2021-05-27 17:10:17 來源:億速云 閱讀:670 作者:Leah 欄目:編程語言

今天就跟大家聊聊有關(guān)使用Mybatis-Plus怎么實現(xiàn)多表聯(lián)查分頁,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

實現(xiàn)

1. 配置攔截器

@EnableTransactionManagement
@Configuration
@MapperScan("com.web.member.mapper")
public class MybatisPlusConfig {
  /**
   * mybatis-plus SQL執(zhí)行效率插件【生產(chǎn)環(huán)境可以關(guān)閉】
   */
  @Bean
  public PerformanceInterceptor performanceInterceptor() {
    return new PerformanceInterceptor();
  }

  /*
   * 分頁插件,自動識別數(shù)據(jù)庫類型 多租戶,請參考官網(wǎng)【插件擴展】
   */
  @Bean
  public 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 無法為你實現(xiàn)分頁。

  <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 實現(xiàn)

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>
 * 用戶表 服務(wù)實現(xiàn)類
 * </p>
 *
 * @author 殷天文
 * @since 2018-06-01
 */
@Service
public 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());// 當前頁,總條數(shù) 構(gòu)造 page 對象
    return page.setRecords(this.baseMapper.selectUserListPage(page, user));
  }
  
}

最后將結(jié)果集 set 到 page 對象中,page 對象的 json 結(jié)構(gòu)如下

{
  "total": 48,//總記錄
  "size": 10,//每頁顯示多少條
  "current": 1,//當前頁
  "records": [//結(jié)果集 數(shù)組
    {...},
    {...},
    {...},
     ...
  ],
  "pages": 5 // 總頁數(shù)
}

看完上述內(nèi)容,你們對使用Mybatis-Plus怎么實現(xiàn)多表聯(lián)查分頁有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI