Mybatis-plus<二>通用CRUD,分页

与博客Mybatis-plus<一>为同一个Springboot项目。

Demo GitHub下载地址:https://github.com/RJvon/Mybatis_plus_demo(包含数据库建表sql,数据库数据与源代码)

Mybatis-plus官网: https://mp.baomidou.com/

未经作者同意请勿转载

Service CRUD 接口说明

说明:

  • 通用 Service CRUD 封装IService接口,进一步封装 CRUD 采用 get 查询单行 remove 删除 list 查询集合 page 分页 前缀命名方式区分 Mapper 层避免混淆,
  • 泛型 T 为任意实体对象
  • 对象 Wrapper条件构造器

CRUD接口:

Insert

// @param entity 实体对象
// 插入一条记录
int insert(T entity);
@Test
public void insertTest1() {
Employees employees = new Employees();
employees.setId(109);
employees.setBirth(LocalDateTime.of(2019, Month.SEPTEMBER, 10, 14, 46, 56));
employees.setName("冯北航");
employees.setSex(false);
employees.setSchool("北京航空航天大学");
employees.setTel("12345678910");
int insert = employeesMapper.insert(employees);
System.out.println(employees);
System.out.println(insert);
}

Delete

// 根据 entity 条件,删除记录
int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper);
employees.setTel("12345676910");
employees.setName("caocao");
QueryWrapper<Employees> wrapper = new QueryWrapper<>(Employees);
employeesMapper.delete(wrapper);
// 删除(根据ID 批量删除)
int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
int result = this.employeesMapper.deleteBatchIds(Arrays.asList(105,106));
// 根据 ID 删除
int deleteById(Serializable id);
// 根据 columnMap 条件,删除记录,多个条件之间是and关系
int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
比如 map.put("name")="陈中山"
map.put("tel")="12345676910"
删除"name"="陈中山";"tel"="12345676910"的对象
    @Test
public void testDeleteById(){
// 根据id删除数据
int result = this.employeesMapper.deleteById(101);
} @Test
public void testDeleteByMap(){
Map<String,Object> map = new HashMap<>();
map.put("name", "钱北大");
map.put("sex", true);
// 根据map删除数据,多条件之间是and关系
int result = this.employeesMapper.deleteByMap(map);
} @Test
public void testDelete(){
//用法一:
QueryWrapper<Employees> wrapper = new QueryWrapper<Employees>();
wrapper.eq("name", "李交通")
.eq("sex", false); /* //用法二:
Employees employees = new Employees();
employees.setName("孙复旦");
employees.setSex(true);
QueryWrapper<Employees> wrapper = new QueryWrapper<>(employees);*/
// 根据包装条件做删除
int result = this.employeesMapper.delete(wrapper);
} @Test
public void testDeleteBatchIds(){
// 根据id批量删除数据
int result = this.employeesMapper.deleteBatchIds(Arrays.asList(105,106));
System.out.println("result => " + result);
} @Test
public void testSelectBatchIds(){
// 根据id批量查询数据
List<Employees> employees = this.employeesMapper.selectBatchIds(Arrays.asList(2L, 3L, 4L, 100L));
for (Employees e : employees) {
System.out.println(employees);
}
}

Update

// 根据 whereWrapper 条件,更新记录
int update(@Param(Constants.ENTITY) T updateEntity, @Param(Constants.WRAPPER) Wrapper<T> whereWrapper);
// 根据 ID 修改
int updateById(@Param(Constants.ENTITY) T entity);
@Test
public void updateTest(){
Employees employees = new Employees();
employees.setId(101);
employees.setTel("00000000000");
int i = employeesMapper.updateById(employees);
System.out.println(i);
}

Select

// 根据 ID 查询
T selectById(Serializable id);
// 根据 entity 条件,查询一条记录
T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 查询(根据ID 批量查询)
List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
// 根据 entity 条件,查询全部记录
List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 查询(根据 columnMap 条件)
List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
// 根据 Wrapper 条件,查询全部记录
List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询全部记录。注意: 只返回第一个字段的值
List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 entity 条件,查询全部记录(并翻页)
IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询全部记录(并翻页)
IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询总记录数
Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
@Test
public void getSelect1() {
System.out.println(employeesMapper.selectById(101));
}
@Test
public void getSelect2() {
List<Employees> employees = employeesMapper.selectBatchIds(Arrays.asList(101, 102, 103));
for (Employees employee : employees) {
System.out.println(employee);
}
} @Test
public void testSelectPage(){
Page<Employees> employeesPage = new Page<>(3, 2);
QueryWrapper wrapper = new QueryWrapper();
wrapper.eq("sex",true);
Page page = employeesMapper.selectPage(employeesPage, wrapper);
System.out.println("数据总条数: " + page.getTotal());
System.out.println("数据总页数: " + page.getPages());
System.out.println("当前页数: " + page.getCurrent());
List records = page.getRecords();
records.forEach(System.out::println);
}

分页——Springboot中添加

@Configuration
@MapperScan("scan.your.mapper.package")
public class MybatisPlusConfig { /**
* 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
return interceptor;
} @Bean
public ConfigurationCustomizer configurationCustomizer() {
return configuration -> configuration.setUseDeprecatedExecutor(false);
}
}

Mybatis-plus<二>通用CRUD,分页的更多相关文章

  1. Spring Boot mybatis HashMap +layui 通用分页

    背景: mybatis 常用数据查询的方法都是先建实体类,再建Mapper,最后写Service,如果只是单纯的去查询数据显示,这样操作太麻烦.本文就以mybatis +layui创建通用分页类,简化 ...

  2. MyBatis(二):基础CRUD

    本文是按照狂神说的教学视频学习的笔记,强力推荐,教学深入浅出1便就懂!b站搜索狂神说即可 https://space.bilibili.com/95256449?spm_id_from=333.788 ...

  3. Mybatis系列二-快速开发

    mybatis学习系列第二篇 分页 在网页中常常用到,在查询数据库内容并想将其输出的时候,因为有时有多组数据,一页展示过于突兀,所以会用到分页操作. 在sql用limit来分页. 首先是UserMap ...

  4. Spring-Data-JPA尝鲜:快速搭建CRUD+分页后台实例

    前言:由于之前没有接触过Hibernate框架,但是最近看一些博客深深被它的"效率"所吸引,所以这就来跟大家一起就着一个简单的例子来尝尝Spring全家桶里自带的JPA的鲜 Spr ...

  5. Spring Boot CRUD+分页(基于JPA规范)

    步骤一:JPA概念 JPA(Java Persistence API)是Sun官方提出的Java持久化规范,用来方便大家操作数据库. 真正干活的可能是Hibernate,TopLink等等实现了JPA ...

  6. SSM框架之Mybatis(2)CRUD操作

    Mybatis(2)CRUD 1.基于代理Dao实现CRUD操作 使用要求: 1.持久层接口(src\main\java\dao\IUserDao.java)和持久层接口的映射配置(src\main\ ...

  7. mybatis升级案例之CRUD操作

    mybatis升级案例之CRUD操作 一.准备工作 1.新建maven工程,和入门案例一样 主要步骤如下,可参考mybatis入门实例 a.配置pom.xml文件 b.新建实例类User.DAO接口类 ...

  8. 如此高效通用的分页存储过程是带有sql注入漏洞的

    原文:如此高效通用的分页存储过程是带有sql注入漏洞的 在google中搜索“分页存储过程”会出来好多结果,是大家常用的分页存储过程,今天我却要说它是有漏洞的,而且漏洞无法通过修改存储过程进行补救,如 ...

  9. [Beego模型] 二、CRUD 操作

    [Beego模型] 一.ORM 使用方法 [Beego模型] 二.CRUD 操作 [Beego模型] 三.高级查询 [Beego模型] 四.使用SQL语句进行查询 [Beego模型] 五.构造查询 [ ...

随机推荐

  1. 基于pygame框架的打飞机小游戏

    import pygame from pygame.locals import * import time import random class Base(object): "" ...

  2. Dubbo 实现一个Route Factory(用于灰度发布)

    Dubbo 可以实现的扩展很多, 官方文档在这: https://dubbo.apache.org/zh/docs/v2.7/dev/impls/ (太简单了....) 下面我们实现一个Route F ...

  3. HCNA Routing&Switching之GVRP

    前文我们了解了不同vlan间路由相关话题,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/15110336.html,今天我们来聊一聊vlan动态注册相关话题: ...

  4. 树莓派3B/3B+/4B 刷机装系统烧录镜像教程

    树莓派3B/3B+/4B 刷机装系统烧录镜像教程 树莓派 背景故事 刚拿到树莓派的第一件事,应该就是要装系统了,那么应该怎么操作呢?下面就给大家介绍一下吧. 硬件准备 树莓派:3B/3B+/4B,本教 ...

  5. 以TiDB热点问题来谈Region的调度流程

    什么是热点问题 说这个话题之前我们先回顾一下TiDB的主要结构和概念. TiDB的核心架构分为TiDB.TiKV.PD三个部分,其中TiKV是一个分布式数据存储引擎用来存储真实的数据,在TiKV中又对 ...

  6. 实战爬取拷背漫画-Python

    ​  一.抓包获取链接 以爬取<前科者>为例 获取搜索链接 https://api.copymanga.com/api/v3/search/comic?limit=5&q=前科者 ...

  7. 用VirtualBox搭建虚拟局域网

    用 Oracle VM VirtualBox 安装虚拟机,我在Windows 7上安装了ubuntu 11.10和xubuntu12.04两个虚拟机: 将这两个虚拟机的"网络"属性 ...

  8. WPF基础:Dispatcher介绍

    Disaptcher作用 不管是WinForm应用程序还是WPF应用程序,实际上都是一个进程,一个进程可以包含多个线程,其中有一个是主线程,其余的是子线程.在WPF或WinForm应用程序中,主线程负 ...

  9. Vue CLI安装报错 npm ERR! Exit handler never called!

    安装Vue CLI时报错: npm install –g vue-cli 试了四种办法 1.把全局安装-g放到后面 npm install @vue/cli –g 2.命令行输入 npm 缓存清理命令 ...

  10. Kali 2.0 安装教程

    本文适合KALI初学者,将详细介绍Kali Linux 2.0的安装过程. 首先我们到KALI的官网下载镜像,大家可以自己选择下载32或64位的KALI 2.0系统. KALI 官网:https:// ...