<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.5.2version>
dependency>
mybatis-plus:
configuration:
map-underscore-to-camel-case: true
package com.liu.susu.pojo.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@TableName("dog")
@Data
public class DogEntity {
@TableId(type = IdType.ASSIGN_ID)
private String id;
private String dogName;
private String dogSex;
private String kennelsNum;
}
extends BaseMapper
,后面简单的sql就不用写了,页不用写xml文件了)package com.liu.susu.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.liu.susu.pojo.entity.DogEntity;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface DogMapper extends BaseMapper<DogEntity> {
}
.
出来,我们这里就提一嘴:DogProvide.java
DogMapper.java
DogServiceImpl.java
看效果:
where
关键字package com.liu.susu.common;
/**
* description
* @author susu
**/
public enum ResponseCode {
SUCCESS_000000("000000","操作成功"),
INSERT_SUCCESS("000000","新增成功"),
UPDATE_SUCCESS("000000","修改成功"),
DELETE_SUCCESS("000000","删除成功"),
SELECT_SUCCESS("000000","查询成功"),
SELECT_SUCCESS_NO_DATA("000001","查无数据"),
INSERT_FAIL("100000","新增失败"),
UPDATE_FAIL("100000","修改失败"),
DELETE_FAIL("100000","删除失败"),
SELECT_FAIL("100000","查询失败"),
FAIL_NO_PARAM("200001","参数不能为空"),
FAIL_999998("999998","操作失败"),
FAIL_999999("999999","系统异常");
private final String code;
private final String message;
ResponseCode(String code, String message){
this.code = code;
this.message = message;
}
public String getCode(){
return code;
}
public String getMessage(){
return message;
}
}
package com.liu.susu.common;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang.StringUtils;
/**
* description
* @author susu
**/
@NoArgsConstructor
@AllArgsConstructor
@Data
public class ResponseData<T> {
private String code;
private String message;
private T data;
public static<T> ResponseData<T> returnResult(){
return returnResult(ResponseCode.SUCCESS_000000);//默认成功
}
public static<T> ResponseData<T> returnResult(ResponseCode responseCode) {
ResponseData<T> responseData = new ResponseData<>();
responseData.setCode(responseCode.getCode());
responseData.setMessage(responseData.getMessage());
return responseData;
}
public static<T> ResponseData<T> returnResult(ResponseCode responseCode, String message) {
ResponseData<T> responseData = new ResponseData<>();
responseData.setCode(responseCode.getCode());
message = StringUtils.isNotEmpty(message)?message: responseCode.getMessage();
responseData.setMessage(message);
return responseData;
}
public static<T> ResponseData<T> returnResult(ResponseCode responseCode, T data) {
ResponseData<T> responseData = returnResult(responseCode);
responseData.setData(data);
return responseData;
}
public static <T> ResponseData<T> returnResult(T data) {
return returnResult(ResponseCode.SUCCESS_000000,data);
}
}
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
package com.liu.susu.common.config;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan("com.liu.susu.mapper")
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
// mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.ORACLE));
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor()); // 分页插件
return mybatisPlusInterceptor;
}
}
代码如下:
public IPage<DogEntity> getDogPageList_1(int pageNo,int pageSize){
Page<DogEntity> page = new Page(pageNo,pageSize);
IPage<DogEntity> pageResult = dogMapper.selectPage(page, null);
return pageResult;
}
效果:
MyPageParam.java
package com.liu.susu.common.page;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
@ApiModel("分页参数")
@Data
public class MyPageParam implements Serializable {
private static final Integer PAGE_NO = 1;
private static final Integer PAGE_SIZE = 10;
@ApiModelProperty(value = "页码", required = true,example = "1")
private Integer pageNo = PAGE_NO;
@ApiModelProperty(value = "每页条数", required = true, example = "10")
private Integer pageSize = PAGE_SIZE;
}
MyServletUtils.java
package com.liu.susu.common.utils;
import cn.hutool.core.convert.Convert;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
public class MyServletUtils {
public static Integer getParameterToInt(String name) {
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) attributes;
HttpServletRequest request = servletRequestAttributes.getRequest();//获取request
Object value = request.getParameter(name);
return Convert.toInt(value);
}
}
MyPageResult.java
package com.liu.susu.common.page;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@NoArgsConstructor
@Data
public final class MyPageResult<T> implements Serializable {
@ApiModelProperty(value = "数据", required = true)
private List<T> list;
@ApiModelProperty(value = "总量", required = true)
private Long total;
public MyPageResult(List<T> list, Long total) {
this.list = list;
this.total = total;
}
public MyPageResult(Long total) {
this.list = new ArrayList<>();
this.total = total;
}
public static <T> MyPageResult<T> empty() {
return new MyPageResult<>(0L);
}
public static <T> MyPageResult<T> empty(Long total) {
return new MyPageResult<>(total);
}
}
MyPageUtils.java
package com.liu.susu.common.utils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liu.susu.common.page.MyPageParam;
import com.liu.susu.common.page.MyPageResult;
import java.util.List;
/**
* description 分页工具类
* @author susu
**/
public class MyPageUtils {
public static final String PAGE_NO = "pageNo";
public static final String PAGE_SIZE = "pageSize";
public static MyPageParam getMyPageParam() {
MyPageParam myPageParam = new MyPageParam();
myPageParam.setPageNo(MyServletUtils.getParameterToInt(PAGE_NO));
myPageParam.setPageSize(MyServletUtils.getParameterToInt(PAGE_SIZE));
return myPageParam;
}
public static <T> Page<T> getMyPage(){
MyPageParam myPageParam = getMyPageParam();
Integer pageNo = myPageParam.getPageNo();
Integer pageSize = myPageParam.getPageSize();
if (pageNo!=null && pageSize!=null){
Page<T> page = new Page<>(myPageParam.getPageNo(), myPageParam.getPageSize());
return page;
}
return null;
}
public static <T> MyPageResult<T> getMyPageResult(IPage<T> pageResult){
if (pageResult != null) {
return new MyPageResult<>(pageResult.getRecords(), pageResult.getTotal());
}
return new MyPageResult<>(null);//没有分页
}
/**
* description :获取分页结果
* @param page
* @param list
* @author susu
*/
public static <T> MyPageResult<T> getMyPageResult(Page page, List<T> list){
if (page != null) {
page.setRecords(list);
return new MyPageResult<>(page.getRecords(), page.getTotal());
}
return new MyPageResult<>(list, null);//没有分页
}
}
太简单,直接给图:
效果:
package com.liu.susu.pojo.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
@ApiModel("狗狗信息vo")
public class DogInfoVo {
@ApiModelProperty("id")
private String id;
@ApiModelProperty("狗狗姓名")
private String dogName;
@ApiModelProperty("所在犬舍名")
private String kennelsName;
}
@ApiOperationSupport(author = "浪花们",order = 1)
@ApiOperation(value = "查询狗狗")
@ResponseBody
@GetMapping("/selectDogs")
public ResponseData<MyPageResult<DogInfoVo>> selectDogs(@RequestParam("kennelsName") String kennelsName){
Page<DogInfoVo> myPage = MyPageUtils.getMyPage();
List<DogInfoVo> dogInfoList = dogMapper2.selectDogs(myPage,kennelsName);
return ResponseData.returnResult(MyPageUtils.getMyPageResult(myPage,dogInfoList));
}
5.3.2
已经实现,可以再点回去看看,还要自定义sql的其实都可以实现多表联查实现分页的,在此不多说了,自己试试吧。