Plus分页插件单表/连接查询都默认支持,很强大!!!

插件定义:

  1. package com.iwhalecloud.aiFactory.common.autoconfigure;
  2. import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
  3. import com.baomidou.mybatisplus.plugins.PerformanceInterceptor;
  4. import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
  5. import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
  6. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. @Configuration
  10. @ConditionalOnClass(name = "com.baomidou.mybatisplus.spring.boot.starter.MybatisPlusAutoConfiguration")
  11. public class MybatisCommonConfig {
  12. @Bean
  13. @ConditionalOnMissingBean
  14. public PaginationInterceptor paginationInterceptor() {
  15. return new PaginationInterceptor();
  16. }
  17. @Bean
  18. @ConditionalOnMissingBean
  19. @ConditionalOnProperty(name = "mybatis-plus.show-sql", havingValue = "true")
  20. public PerformanceInterceptor performanceInterceptor() {
  21. return new PerformanceInterceptor();
  22. }
  23. }

分页Mapper格式

  1. public interface VmInstanceApplicationMapper extends BaseMapper<VmInstanceApplication> {
  2. /**
  3. * 申请列表
  4. * @param page
  5. * @param params
  6. * @return
  7. */
  8. List<VmInstanceApplicationResponse> applicationList(Page page, VmInstanceApplicationRequest params);
  9. }
  10. #SQL无需手动分页了
  11. <select id="applicationList" parameterType="com.iwhalecloud.aiFactory.aiDevCenter.application.vo.VmInstanceApplicationRequest" resultType="com.iwhalecloud.aiFactory.aiDevCenter.application.vo.VmInstanceApplicationResponse">
  12. SELECT
  13. vm.name vmName,
  14. t.id, t.vm_instance, t.duration, t.application_desc, t.create_user_id, t.create_time, t.auditor, t.audit_desc, t.audit_time, t.com_acct_id, t.application_status
  15. FROM
  16. vm_instance_application t
  17. LEFT JOIN vm_instance vm ON t.vm_instance = vm.id
  18. WHERE
  19. 1 = 1
  20. <if test="createUserId != null">
  21. AND t.create_user_id = #{createUserId}
  22. </if>
  23. <if test="applicationStatus != null and applicationStatus != ''">
  24. AND t.application_status = #{applicationStatus}
  25. </if>
  26. <if test="vmName != null and vmName != ''">
  27. AND vm.name LIKE concat('%', #{vmName}, '%')
  28. </if>
  29. <if test="vmInstance != null">
  30. AND t.vm_instance = #{vmInstance}
  31. </if>
  32. <if test="comAcctId != null">
  33. AND t.com_acct_id = #{comAcctId}
  34. </if>
  35. <if test="applicationStatusList != null and applicationStatusList.size() > 0">
  36. AND t.application_status in
  37. <foreach collection="applicationStatusList" item="one" open="(" close=")" separator=",">
  38. #{one}
  39. </foreach>
  40. </if>
  41. ORDER BY
  42. create_time DESC
  43. </select>

Service接口格式

  1. public interface VmInstanceApplicationService {
  2. /**
  3. * 申请列表
  4. * @param params
  5. * @return
  6. */
  7. PageInfo<VmInstanceApplicationResponse> applicationList(VmInstanceApplicationRequest params);
  8. }

Service实现格式

其中Page是Plus框架类
PageInfo是自定义返回封装与Page无继承关系

  1. public PageInfo<VmInstanceApplicationResponse> applicationList(VmInstanceApplicationRequest params) {
  2. Page page = new Page<>((params.getPageIndex() == null ? 1 : params.getPageIndex()), (params.getPageSize() == null ? 10 : params.getPageSize()));
  3. //这是一个单页的列表,但可以被plus插件拦截取到total值
  4. List<VmInstanceApplicationResponse> applicationList = vmInstanceApplicationMapper.applicationList(page, params);
  5. if (applicationList == null || applicationList.isEmpty()) {
  6. return new PageInfo<>(page.getCurrent(), page.getSize(), page.getTotal(), Collections.emptyList());
  7. }
  8. // 翻译状态VM_APPLICATION_STATUS
  9. taiDcService.addDictListNameInfo(applicationList, ResourceConst.SYS_RESOURCE,
  10. new DictFieldList().addField(AiDevCenterConsts.VM_APPLICATION_STATUS_KEY, "applicationStatus", "applicationStatusName"));
  11. // 翻译申请人和审核人
  12. List<Long> userIds = applicationList.stream().map(VmInstanceApplicationResponse::getCreateUserId).filter(Objects::nonNull).collect(Collectors.toList());
  13. userIds.addAll(applicationList.stream().map(VmInstanceApplicationResponse::getAuditor).filter(Objects::nonNull).collect(Collectors.toList()));
  14. userIds = userIds.stream().distinct().collect(Collectors.toList());
  15. try {
  16. List<UserInfoDto> userList = userService.getByIdList(userIds); // 查询用户列表
  17. if (userList != null && !userList.isEmpty()) {
  18. Map<Long, String> userNameMap = userList.stream().collect(Collectors.toMap(UserInfoDto::getUserId, UserInfoDto::getUserName));
  19. applicationList.forEach(one -> {
  20. one.setUserName(userNameMap.get(one.getCreateUserId()));
  21. one.setAuditorName(userNameMap.get(one.getAuditor()));
  22. });
  23. }
  24. }
  25. catch (Exception e) {
  26. LoggerUtil.error("fail to translate userName", e);
  27. }
  28. return new PageInfo<>(page.getCurrent(), page.getSize(), page.getTotal(), applicationList);
  29. }

其中

  1. Page page = new Page<>((params.getPageIndex() == null ? 1 : params.getPageIndex()), (params.getPageSize() == null ? 10 : params.getPageSize()));
  2. //这是一个单页的列表,但可以被plus插件拦截取到total值
  3. List<VmInstanceApplicationResponse> applicationList = vmInstanceApplicationMapper.applicationList(page, params);

Vo请求类格式

定义分页字段:

  • private Integer pageIndex;
  • private Integer pageSize; ```java package com.iwhalecloud.aiFactory.aiDevCenter.application.vo;

import com.iwhalecloud.aiFactory.aiDevCenter.application.dto.VmInstanceApplication;

import lombok.Data; import lombok.experimental.Accessors;

import java.util.List;

@Data @Accessors(chain = true) public class VmInstanceApplicationRequest extends VmInstanceApplication {

  1. private Integer pageIndex;
  2. private Integer pageSize;
  3. private String vmName; // 虚拟机名称(即notebook的名称)
  4. private List<String> applicationStatusList; // 申请状态列表
  5. private String userName;
  6. @Override
  7. public String toString() {
  8. final StringBuffer sb = new StringBuffer("VmInstanceApplicationRequest{");
  9. sb.append("pageIndex=").append(pageIndex);
  10. sb.append(", pageSize=").append(pageSize);
  11. sb.append(", vmName='").append(vmName).append('\'');
  12. sb.append(", applicationStatusList=").append(applicationStatusList);
  13. sb.append(", userName='").append(userName).append('\'');
  14. sb.append('}');
  15. return sb.toString();
  16. }

}

  1. <a name="jOrQx"></a>
  2. # 分页类数据结构
  3. <a name="nFaNV"></a>
  4. ## Page
  5. ```java
  6. /**
  7. * Copyright (c) 2011-2014, hubin (jobob@qq.com).
  8. * <p>
  9. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  10. * use this file except in compliance with the License. You may obtain a copy of
  11. * the License at
  12. * <p>
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. * <p>
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  17. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  18. * License for the specific language governing permissions and limitations under
  19. * the License.
  20. */
  21. package com.baomidou.mybatisplus.plugins;
  22. import java.beans.Transient;
  23. import java.util.Collections;
  24. import java.util.List;
  25. import java.util.Map;
  26. import com.baomidou.mybatisplus.plugins.pagination.Pagination;
  27. /**
  28. * <p>
  29. * 实现分页辅助类
  30. * </p>
  31. *
  32. * @author hubin
  33. * @Date 2016-03-01
  34. */
  35. public class Page<T> extends Pagination {
  36. private static final long serialVersionUID = 1L;
  37. /**
  38. * 查询数据列表
  39. */
  40. private List<T> records = Collections.emptyList();
  41. /**
  42. * 查询参数( 不会传入到 xml 层,这里是 Controller 层与 service 层传递参数预留 )
  43. */
  44. private Map<String, Object> condition;
  45. public Page() {
  46. /* 注意,传入翻页参数 */
  47. }
  48. public Page(int current, int size) {
  49. super(current, size);
  50. }
  51. public Page(int current, int size, String orderByField) {
  52. super(current, size);
  53. this.setOrderByField(orderByField);
  54. }
  55. public Page(int current, int size, String orderByField, boolean isAsc) {
  56. this(current, size, orderByField);
  57. this.setAsc(isAsc);
  58. }
  59. public List<T> getRecords() {
  60. return records;
  61. }
  62. public Page<T> setRecords(List<T> records) {
  63. this.records = records;
  64. return this;
  65. }
  66. @Transient
  67. public Map<String, Object> getCondition() {
  68. return condition;
  69. }
  70. public Page<T> setCondition(Map<String, Object> condition) {
  71. this.condition = condition;
  72. return this;
  73. }
  74. @Override
  75. public String toString() {
  76. StringBuilder pg = new StringBuilder();
  77. pg.append(" Page:{ [").append(super.toString()).append("], ");
  78. if (records != null) {
  79. pg.append("records-size:").append(records.size());
  80. } else {
  81. pg.append("records is null");
  82. }
  83. return pg.append(" }").toString();
  84. }
  85. }

PageInfo

  1. package com.iwhalecloud.aiFactory.common.dto;
  2. import lombok.ToString;
  3. import java.io.Serializable;
  4. import java.util.Collections;
  5. import java.util.List;
  6. @ToString
  7. public class PageInfo<T> implements Serializable {
  8. /**
  9. * 总数
  10. */
  11. private int total = 0;
  12. /**
  13. * 每页显示条数,默认 10
  14. */
  15. private int size = 10;
  16. /**
  17. * 总页数
  18. */
  19. private int pages = 0;
  20. /**
  21. * 当前页
  22. */
  23. private int current = 1;
  24. private int pageCount; // 总页数
  25. /**
  26. * 查询数据列表
  27. */
  28. private List<T> list = Collections.emptyList();
  29. public PageInfo() {
  30. }
  31. public PageInfo(int current, int size) {
  32. this.size = size;
  33. this.current = current;
  34. }
  35. public PageInfo(int current, int size, int total, List<T> list) {
  36. this.total = total;
  37. this.size = size;
  38. this.current = current;
  39. this.list = list;
  40. this.pages = (total + size - 1) / size;
  41. }
  42. public int getTotal() {
  43. return total;
  44. }
  45. public PageInfo<T> setTotal(int total) {
  46. this.total = total;
  47. pages = (total + size - 1) / size;
  48. return this;
  49. }
  50. public int getSize() {
  51. return size;
  52. }
  53. public PageInfo<T> setSize(int size) {
  54. this.size = size;
  55. pages = (total + size - 1) / size;
  56. return this;
  57. }
  58. public int getPages() {
  59. return pages;
  60. }
  61. public int getCurrent() {
  62. return current;
  63. }
  64. public PageInfo<T> setCurrent(int current) {
  65. this.current = current;
  66. return this;
  67. }
  68. public List<T> getList() {
  69. return list;
  70. }
  71. public PageInfo<T> setList(List<T> list) {
  72. this.list = list;
  73. return this;
  74. }
  75. public int getPageCount() {
  76. return pageCount;
  77. }
  78. public void setPageCount(int pageCount) {
  79. this.pageCount = pageCount;
  80. }
  81. }