Mybatis-plus<二>通用CRUD,分页
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,分页的更多相关文章
- Spring Boot mybatis HashMap +layui 通用分页
背景: mybatis 常用数据查询的方法都是先建实体类,再建Mapper,最后写Service,如果只是单纯的去查询数据显示,这样操作太麻烦.本文就以mybatis +layui创建通用分页类,简化 ...
- MyBatis(二):基础CRUD
本文是按照狂神说的教学视频学习的笔记,强力推荐,教学深入浅出1便就懂!b站搜索狂神说即可 https://space.bilibili.com/95256449?spm_id_from=333.788 ...
- Mybatis系列二-快速开发
mybatis学习系列第二篇 分页 在网页中常常用到,在查询数据库内容并想将其输出的时候,因为有时有多组数据,一页展示过于突兀,所以会用到分页操作. 在sql用limit来分页. 首先是UserMap ...
- Spring-Data-JPA尝鲜:快速搭建CRUD+分页后台实例
前言:由于之前没有接触过Hibernate框架,但是最近看一些博客深深被它的"效率"所吸引,所以这就来跟大家一起就着一个简单的例子来尝尝Spring全家桶里自带的JPA的鲜 Spr ...
- Spring Boot CRUD+分页(基于JPA规范)
步骤一:JPA概念 JPA(Java Persistence API)是Sun官方提出的Java持久化规范,用来方便大家操作数据库. 真正干活的可能是Hibernate,TopLink等等实现了JPA ...
- SSM框架之Mybatis(2)CRUD操作
Mybatis(2)CRUD 1.基于代理Dao实现CRUD操作 使用要求: 1.持久层接口(src\main\java\dao\IUserDao.java)和持久层接口的映射配置(src\main\ ...
- mybatis升级案例之CRUD操作
mybatis升级案例之CRUD操作 一.准备工作 1.新建maven工程,和入门案例一样 主要步骤如下,可参考mybatis入门实例 a.配置pom.xml文件 b.新建实例类User.DAO接口类 ...
- 如此高效通用的分页存储过程是带有sql注入漏洞的
原文:如此高效通用的分页存储过程是带有sql注入漏洞的 在google中搜索“分页存储过程”会出来好多结果,是大家常用的分页存储过程,今天我却要说它是有漏洞的,而且漏洞无法通过修改存储过程进行补救,如 ...
- [Beego模型] 二、CRUD 操作
[Beego模型] 一.ORM 使用方法 [Beego模型] 二.CRUD 操作 [Beego模型] 三.高级查询 [Beego模型] 四.使用SQL语句进行查询 [Beego模型] 五.构造查询 [ ...
随机推荐
- c++ 父类 子类 虚表占用内存空间情况
#include <iostream> using namespace std; class C {}; class A:public C { private: long a; long ...
- 构建后端第5篇之---Idea 查看继承 实现关系图
first question: how to show a class children class : move mousrmark to class name , Ctrl + H how t ...
- react native踩坑记录
一 .安装 1.Python2 和Java SE Development Kit (JDK)可以直接通过腾讯电脑关键安装, Android SDK安装的时候路径里不能有中文和空格 2.配置java环境 ...
- 【LeetCode】98. 验证二叉搜索树
98. 验证二叉搜索树 知识点:二叉树:递归 题目描述 给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大 ...
- Verilog function的使用
function的用法 function的标准写法如下: function <返回值的类型或是范围> (函数名): <端口说明语句> //input xxx <变量类 ...
- GoAhead 远程命令执行漏洞(CVE-2017-17562)
poc地址 https://github.com/ivanitlearning/CVE-2017-17562 执行 msfvenom -a x64 --platform Linux -p linux/ ...
- LeetCode通关:哈希表六连,这个还真有点简单
精品刷题路线参考: https://github.com/youngyangyang04/leetcode-master https://github.com/chefyuan/algorithm-b ...
- gRPC学习之一:在CentOS7部署和设置GO
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- 7年老Android收到阿里offer,跟领导提离职被怼:为年薪百万不做兄弟?
在当今社会,钱就是衡量一个人价值的标准,如果你在一家公司,领导再怎么重用你,但是薪资待遇却很低,这样根本是很难留住人,毕竟工作就是为了赚钱,要是连工资都满足不了,谈其他根本就是扯淡. 最近在职业论坛看 ...
- [数据结构-平衡树]普通 FHQ_Treap从入门到精通(注释比代码多系列)
普通 FHQ_Treap从入门到精通(注释比代码多系列) 前提说明,作者写注释太累了,文章里的部分讲解来源于Oi-wiki,并根据代码,有部分增改.本文仅仅发布于博客园,其他地方出现本文,均是未经许可 ...