插件定义:
package com.iwhalecloud.aiFactory.common.autoconfigure;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.plugins.PerformanceInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnClass(name = "com.baomidou.mybatisplus.spring.boot.starter.MybatisPlusAutoConfiguration")
public class MybatisCommonConfig {
@Bean
@ConditionalOnMissingBean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(name = "mybatis-plus.show-sql", havingValue = "true")
public PerformanceInterceptor performanceInterceptor() {
return new PerformanceInterceptor();
}
}
分页Mapper格式
public interface VmInstanceApplicationMapper extends BaseMapper<VmInstanceApplication> {
/**
* 申请列表
* @param page
* @param params
* @return
*/
List<VmInstanceApplicationResponse> applicationList(Page page, VmInstanceApplicationRequest params);
}
#SQL无需手动分页了
<select id="applicationList" parameterType="com.iwhalecloud.aiFactory.aiDevCenter.application.vo.VmInstanceApplicationRequest" resultType="com.iwhalecloud.aiFactory.aiDevCenter.application.vo.VmInstanceApplicationResponse">
SELECT
vm.name vmName,
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
FROM
vm_instance_application t
LEFT JOIN vm_instance vm ON t.vm_instance = vm.id
WHERE
1 = 1
<if test="createUserId != null">
AND t.create_user_id = #{createUserId}
</if>
<if test="applicationStatus != null and applicationStatus != ''">
AND t.application_status = #{applicationStatus}
</if>
<if test="vmName != null and vmName != ''">
AND vm.name LIKE concat('%', #{vmName}, '%')
</if>
<if test="vmInstance != null">
AND t.vm_instance = #{vmInstance}
</if>
<if test="comAcctId != null">
AND t.com_acct_id = #{comAcctId}
</if>
<if test="applicationStatusList != null and applicationStatusList.size() > 0">
AND t.application_status in
<foreach collection="applicationStatusList" item="one" open="(" close=")" separator=",">
#{one}
</foreach>
</if>
ORDER BY
create_time DESC
</select>
Service接口格式
public interface VmInstanceApplicationService {
/**
* 申请列表
* @param params
* @return
*/
PageInfo<VmInstanceApplicationResponse> applicationList(VmInstanceApplicationRequest params);
}
Service实现格式
其中Page
是Plus框架类PageInfo
是自定义返回封装与Page
无继承关系
public PageInfo<VmInstanceApplicationResponse> applicationList(VmInstanceApplicationRequest params) {
Page page = new Page<>((params.getPageIndex() == null ? 1 : params.getPageIndex()), (params.getPageSize() == null ? 10 : params.getPageSize()));
//这是一个单页的列表,但可以被plus插件拦截取到total值
List<VmInstanceApplicationResponse> applicationList = vmInstanceApplicationMapper.applicationList(page, params);
if (applicationList == null || applicationList.isEmpty()) {
return new PageInfo<>(page.getCurrent(), page.getSize(), page.getTotal(), Collections.emptyList());
}
// 翻译状态VM_APPLICATION_STATUS
taiDcService.addDictListNameInfo(applicationList, ResourceConst.SYS_RESOURCE,
new DictFieldList().addField(AiDevCenterConsts.VM_APPLICATION_STATUS_KEY, "applicationStatus", "applicationStatusName"));
// 翻译申请人和审核人
List<Long> userIds = applicationList.stream().map(VmInstanceApplicationResponse::getCreateUserId).filter(Objects::nonNull).collect(Collectors.toList());
userIds.addAll(applicationList.stream().map(VmInstanceApplicationResponse::getAuditor).filter(Objects::nonNull).collect(Collectors.toList()));
userIds = userIds.stream().distinct().collect(Collectors.toList());
try {
List<UserInfoDto> userList = userService.getByIdList(userIds); // 查询用户列表
if (userList != null && !userList.isEmpty()) {
Map<Long, String> userNameMap = userList.stream().collect(Collectors.toMap(UserInfoDto::getUserId, UserInfoDto::getUserName));
applicationList.forEach(one -> {
one.setUserName(userNameMap.get(one.getCreateUserId()));
one.setAuditorName(userNameMap.get(one.getAuditor()));
});
}
}
catch (Exception e) {
LoggerUtil.error("fail to translate userName", e);
}
return new PageInfo<>(page.getCurrent(), page.getSize(), page.getTotal(), applicationList);
}
其中
Page page = new Page<>((params.getPageIndex() == null ? 1 : params.getPageIndex()), (params.getPageSize() == null ? 10 : params.getPageSize()));
//这是一个单页的列表,但可以被plus插件拦截取到total值
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 {
private Integer pageIndex;
private Integer pageSize;
private String vmName; // 虚拟机名称(即notebook的名称)
private List<String> applicationStatusList; // 申请状态列表
private String userName;
@Override
public String toString() {
final StringBuffer sb = new StringBuffer("VmInstanceApplicationRequest{");
sb.append("pageIndex=").append(pageIndex);
sb.append(", pageSize=").append(pageSize);
sb.append(", vmName='").append(vmName).append('\'');
sb.append(", applicationStatusList=").append(applicationStatusList);
sb.append(", userName='").append(userName).append('\'');
sb.append('}');
return sb.toString();
}
}
<a name="jOrQx"></a>
# 分页类数据结构
<a name="nFaNV"></a>
## Page
```java
/**
* Copyright (c) 2011-2014, hubin (jobob@qq.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.baomidou.mybatisplus.plugins;
import java.beans.Transient;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
/**
* <p>
* 实现分页辅助类
* </p>
*
* @author hubin
* @Date 2016-03-01
*/
public class Page<T> extends Pagination {
private static final long serialVersionUID = 1L;
/**
* 查询数据列表
*/
private List<T> records = Collections.emptyList();
/**
* 查询参数( 不会传入到 xml 层,这里是 Controller 层与 service 层传递参数预留 )
*/
private Map<String, Object> condition;
public Page() {
/* 注意,传入翻页参数 */
}
public Page(int current, int size) {
super(current, size);
}
public Page(int current, int size, String orderByField) {
super(current, size);
this.setOrderByField(orderByField);
}
public Page(int current, int size, String orderByField, boolean isAsc) {
this(current, size, orderByField);
this.setAsc(isAsc);
}
public List<T> getRecords() {
return records;
}
public Page<T> setRecords(List<T> records) {
this.records = records;
return this;
}
@Transient
public Map<String, Object> getCondition() {
return condition;
}
public Page<T> setCondition(Map<String, Object> condition) {
this.condition = condition;
return this;
}
@Override
public String toString() {
StringBuilder pg = new StringBuilder();
pg.append(" Page:{ [").append(super.toString()).append("], ");
if (records != null) {
pg.append("records-size:").append(records.size());
} else {
pg.append("records is null");
}
return pg.append(" }").toString();
}
}
PageInfo
package com.iwhalecloud.aiFactory.common.dto;
import lombok.ToString;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;
@ToString
public class PageInfo<T> implements Serializable {
/**
* 总数
*/
private int total = 0;
/**
* 每页显示条数,默认 10
*/
private int size = 10;
/**
* 总页数
*/
private int pages = 0;
/**
* 当前页
*/
private int current = 1;
private int pageCount; // 总页数
/**
* 查询数据列表
*/
private List<T> list = Collections.emptyList();
public PageInfo() {
}
public PageInfo(int current, int size) {
this.size = size;
this.current = current;
}
public PageInfo(int current, int size, int total, List<T> list) {
this.total = total;
this.size = size;
this.current = current;
this.list = list;
this.pages = (total + size - 1) / size;
}
public int getTotal() {
return total;
}
public PageInfo<T> setTotal(int total) {
this.total = total;
pages = (total + size - 1) / size;
return this;
}
public int getSize() {
return size;
}
public PageInfo<T> setSize(int size) {
this.size = size;
pages = (total + size - 1) / size;
return this;
}
public int getPages() {
return pages;
}
public int getCurrent() {
return current;
}
public PageInfo<T> setCurrent(int current) {
this.current = current;
return this;
}
public List<T> getList() {
return list;
}
public PageInfo<T> setList(List<T> list) {
this.list = list;
return this;
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
}