上一篇 SpringBoot集成Mysql、Mybatis、Mybatis-Plus,实现增删改查
一、前言
Mybatis-Plus封装了分页操作,简单配置一下就可以用。我这里还简单做了分页请求参数的封装和响应的封装。
二、Sql语句
CREATE TABLE `user_info` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT '用户id',
`username` VARCHAR(20) NOT NULL DEFAULT '' COMMENT '用户名' COLLATE 'utf8mb4_general_ci',
`password` VARCHAR(100) NOT NULL DEFAULT '' COMMENT '密码' COLLATE 'utf8mb4_general_ci',
`is_deleted` INT(2) NOT NULL DEFAULT '0' COMMENT '是否删除 0-未删除 1-已删除',
`create_time` DATETIME NOT NULL COMMENT '创建时间',
`update_time` DATETIME NOT NULL COMMENT '更新时间',
PRIMARY KEY (`id`) USING BTREE
)
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=3
;
三、配置分页
@Configuration
public class MyConfiguration {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 数据库分页
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
- 这里分页类型
DbType.MYSQL
四、分页请求参数和响应的封装
BasePageParam
分页参数类
package com.llh.springbootpage.param;
/**
* @author llh
*/
public class BasePageParam {
private Long pageNum;
private Long pageSize;
public Long getPageNum() {
return pageNum;
}
public void setPageNum(Long pageNum) {
this.pageNum = pageNum;
}
public Long getPageSize() {
return pageSize;
}
public void setPageSize(Long pageSize) {
this.pageSize = pageSize;
}
@Override
public String toString() {
return "BasePageParam{" +
"pageNum=" + pageNum +
", pageSize=" + pageSize +
'}';
}
}
BasePageResult
分页响应类
package com.llh.springbootpage.result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;
/**
* @author llh
*/
public class BasePageResult<T> {
private List<T> data;
private Long totalNum;
public BasePageResult(List<T> data, Long totalNum) {
this.data = data;
this.totalNum = totalNum;
}
public static <E> BasePageResult<E> newInstance(IPage<E> page) {
return new BasePageResult<E>(page.getRecords(), page.getTotal());
}
public List<T> getData() {
return data;
}
public void setData(List<T> data) {
this.data = data;
}
public Long getTotalNum() {
return totalNum;
}
public void setTotalNum(Long totalNum) {
this.totalNum = totalNum;
}
@Override
public String toString() {
return "BasePageResult{" +
"data=" + data +
", totalNum=" + totalNum +
'}';
}
}
五、分页模糊查询
package com.llh.springbootpage.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.llh.springbootpage.config.CommonResult;
import com.llh.springbootpage.entity.UserInfo;
import com.llh.springbootpage.param.UserInfoPageParam;
import com.llh.springbootpage.result.BasePageResult;
import com.llh.springbootpage.service.UserInfoService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @author llh
*/
@RestController
@RequestMapping("/userInfo")
public class UserInfoController {
@Resource
private UserInfoService userInfoService;
@PostMapping("/page")
public CommonResult<BasePageResult<UserInfo>> page(@RequestBody UserInfoPageParam param) {
IPage<UserInfo> pageParam = new Page<>(param.getPageNum(), param.getPageSize());
IPage<UserInfo> pageResult = userInfoService.page(pageParam, new LambdaQueryWrapper<UserInfo>()
.like(UserInfo::getUsername, param.getKeyword()));
return CommonResult.success(BasePageResult.newInstance(pageResult));
}
}
- 根据keyword 模糊匹配用户名,
LambdaQueryWrapper
的like
-
UserInfoPageParam
继承了BasePageParam
UserInfoPageParam
请求参数类
package com.llh.springbootpage.param;
/**
* @author admin
*/
public class UserInfoPageParam extends BasePageParam {
private String keyword;
public String getKeyword() {
return keyword;
}
public void setKeyword(String keyword) {
this.keyword = keyword;
}
@Override
public String toString() {
return "UserInfoPageParam{" +
"keyword='" + keyword + '\'' +
"} " + super.toString();
}
}
六、测试
QQ截图20210517142502.png七、结语
- 代码地址:https://github.com/tigerleeli/xiaohuge-blog/tree/master/spring-boot-page
- Mybatis-Plus的基本使用请阅读我之前的文章
网友评论