MybatisPlus简单使用与自定义sql以及通过自定义sql实现多表联查的分页查询
- 前言
- 1. mybatis的简单使用
- 2. MybatisPlus 的简单使用
- 2.1 入门
- 2.1.1 简单配置
- 2.1.2 入门例子
- 2.1.3 测试
- 2.2 MybatisPlus自带封装的增删改查
- 2.2.1 傻瓜式使用
- 2.2.2 批量添加数据
- 3 MybatisPlus 动态查询sql
- 3.1 QueryWrapper 或 LambdaQueryWrapper 条件构造器
- 3.1.1 QueryWrapper
- 3.1.2 LambdaQueryWrapper
- 3.2 自定义 sql 语句
- 3.2.1 xml方式
- 3.2.2 @Select 注解
- 3.2.3 LambdaQueryWrapperX 拼接sql
- 3.2.4 @SelectProvider 注解
- 3.2.4.1 直接 @SelectProvider
- 3.2.4.2 QueryWrapper + @SelectProvider
- 1. ${ew.customSqlSegment}
- 2. ${ew.sqlSegment}
- 4. 优化代码->统一一下返回格式
- 5.MybatisPlus 实现分页( Page 与 IPage)
- 5.1 对分页插件进行初始化配置
- 5.2 使用分页例子
- 5.2.1 简单免写sql分页
- 5.2.2 优化-Page分页传参和返回结果统一格式
- 5.2.3 简单分页测试->统一分页格式之后
- 5.3 自定义sql实现分页
- 5.3.1 @Select+ Page 实现分页查询
- 5.3.2 @SelectProvider + Page 实现分页查询
- 5.3.2.1 基本数据类型作为参数情况
- 5.3.2.2 实体对象作为参数情况(注意)
- 5.4 关于多表联查实现分页
前言
- 之前介绍过mybatis,但是感觉缺少点什么,所以想想还是周末抽空写了点把mybatisplus给补上
1. mybatis的简单使用
- 关于mybatis自定义枚举类型的转换器相关的点,可以看下面的文章
mybatis自定义枚举类型的转换器以及各种使用场景. - 关于mybatis使用插件pagehelper分页的,这个看文章:
springboot项目前后端分离:统一返回数据格式+mybatis 分页.
2. MybatisPlus 的简单使用
2.1 入门
- 下面是以spring boot项目为例简单做简单的例子介绍
2.1.1 简单配置
- 如下
- pom 依赖
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.2</version> </dependency>
- 配置,入门配置所以配的很简单
mybatis-plus: configuration: map-underscore-to-camel-case: true
- pom 依赖
2.1.2 入门例子
- 实体:DogEntity.java
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; }
- DogMapper.java(直接
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> { }
- DogService.java
- DogServiceImpl.java
- DogController.java
2.1.3 测试
- 如下:
2.2 MybatisPlus自带封装的增删改查
2.2.1 傻瓜式使用
- 上面我们已经介绍了一个查询,其实没啥可说的,想用什么方法直接就能
.
出来,我们这里就提一嘴:
2.2.2 批量添加数据
- 主要向想说的是另外一个,批量添加,怎么用自带的方法实现批量插入数据,看下面介绍:
- ① 修改DogService.java,如下:
- ② 修改DogServiceImpl.java,如下:
- ① 修改DogService.java,如下:
- 修改完上述代码怎么用,下面来个测试看看
- 代码:
- 效果:
- 代码:
- 注意:
- 上述例子插入的数据量少,如果批量插入数据量多的话,可以换个方法控制每次插入的条数:
3 MybatisPlus 动态查询sql
3.1 QueryWrapper 或 LambdaQueryWrapper 条件构造器
3.1.1 QueryWrapper
- 傻瓜式使用操作,直接给图看吧:
3.1.2 LambdaQueryWrapper
- 如图:
3.2 自定义 sql 语句
3.2.1 xml方式
- 太常用了,不介绍了,看mybatis
3.2.2 @Select 注解
- 直接看图:
3.2.3 LambdaQueryWrapperX 拼接sql
- 如图:
3.2.4 @SelectProvider 注解
3.2.4.1 直接 @SelectProvider
-
DogProvide.java
-
DogMapper.java
-
DogServiceImpl.java
-
看效果:
3.2.4.2 QueryWrapper + @SelectProvider
1. ${ew.customSqlSegment}
- ${ew.customSqlSegment} 自动拼接上
where
关键字 - DogProvide.java
- DogMapper.java
- DogServiceImpl.java
- 看效果:
2. ${ew.sqlSegment}
- ${ew.sqlSegment} 是不拼接 where 关键字的条件sql
4. 优化代码->统一一下返回格式
- ResponseCode.java
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; } }
- ResponseData.java
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); } }
5.MybatisPlus 实现分页( Page 与 IPage)
5.1 对分页插件进行初始化配置
- 使用 Page 与 IPage进行分页时,需要先配置
先配置,否则分页不生效,配置如下: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; } }
5.2 使用分页例子
5.2.1 简单免写sql分页
-
代码如下:
- DogServiceImpl.java
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; }
- DogController.java
- DogServiceImpl.java
-
效果:
5.2.2 优化-Page分页传参和返回结果统一格式
-
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);//没有分页 } }
5.2.3 简单分页测试->统一分页格式之后
- 代码如下:
- 效果:
5.3 自定义sql实现分页
5.3.1 @Select+ Page 实现分页查询
-
太简单,直接给图:
- DogMapper.java(备注:Page page 可以不用加注解@Param)
- DogServiceImpl.java
- DogController.java
- DogMapper.java(备注:Page page 可以不用加注解@Param)
-
效果:
5.3.2 @SelectProvider + Page 实现分页查询
- 首先先来个vo–>DogInfoVo.java
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; }
5.3.2.1 基本数据类型作为参数情况
- 代码如下:
- DogProvide2.java + DogMapper2.java
- DogController2.java
@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)); }
- DogProvide2.java + DogMapper2.java
- 效果如下:
5.3.2.2 实体对象作为参数情况(注意)
- 代码如下:
- DogProvide2.java
- DogMapper2.java
- DogProvide2.java
- 效果如下:
5.4 关于多表联查实现分页
- 上面
5.3.2
已经实现,可以再点回去看看,还要自定义sql的其实都可以实现多表联查实现分页的,在此不多说了,自己试试吧。 - 好了,愿每个周末是充实的而不是毫无意义的!