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. SpringMVC中文乱码踩坑

    问题 使用SpringMVC在返回一个字符串时发生了中文乱码问题.produces属性无效 @RequestMapping(value = "/nihao", produces = ...

  2. 几百行代码写个Mybatis,原理搞的透透的!

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 Mybatis 最核心的原理也是它最便于使用的体现,为什么这说? 因为我们在使用 M ...

  3. SQL语句(三)分组函数和分组查询

    目录 一.分组函数 特点 1. 各函数的简单使用 2. 搭配distinct的使用 3. COUNT 统计行数 4. 和分组函数一同查询的字段要求是group by后的字段 二.分组查询 1. 简单应 ...

  4. 《MySQL实战45讲》(1-7)笔记

    <MySQL实战45讲>笔记 目录 <MySQL实战45讲>笔记 第一节: 基础架构:一条SQL查询语句是如何执行的? 连接器 查询缓存 分析器 优化器 执行器 第二节:日志系 ...

  5. AlarmManager定时提醒的那些坑

    https://blog.csdn.net/zackratos/article/details/53243595 https://blog.csdn.net/bingshushu/article/de ...

  6. 洛谷P2504题解

    题目 瓶颈生成树的裸题.可以查看这个来获取更多信息. 他问的是能够在所有树上自由穿梭的猴子个数,那我只需要算出这张图上最小生成树中权值最大的边,和每个猴子的最大跳跃长度进行比较即可. 因为我用的是 \ ...

  7. a href="tel" 拨打电话

    电话号码是固定的: <a href="'tel:10086">10086</a> 电话号码是动态获取时: 走默认的方式失败 <a href=" ...

  8. LeetCode通关:哈希表六连,这个还真有点简单

    精品刷题路线参考: https://github.com/youngyangyang04/leetcode-master https://github.com/chefyuan/algorithm-b ...

  9. 线程休眠_sleep

    线程休眠_sleep sleep(时间)指定当前线程阻塞的毫秒数: sleep存在异常InterruptedException: sleep时间到达后线程进入就绪状态: sleep可以模拟网络延时,倒 ...

  10. Visio操作【未完】

    Visio 1.如何操作文档 新建基本框图和空白框图 单击基本框图打开后有模具 空白框图打开之后并没有形状 左下角发现有 更改纸张方向大小 自动调整大小: 如果我们选择形状进入到我们的页面,如果放到边 ...