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模型] 五.构造查询 [ ...
随机推荐
- Rancher监控指标一文干到底
一.工作负载指标 直接截取一个生产环境的rancher的web管理端-工作负载指标模块的图(这里没有汉化,直接英文)如下: 共5个大指标: CPU使用 内存使用 网络包 网络IO 磁盘IO 自学入口: ...
- Linux 基础指令初识
Linux 基础指令初识 01. ls 指令 语法: ls [选项] [目录或文件] 功能:对于目录,该命令列出该目录下的所有子目录与文件.对于文件,将列出文件名以及其他信息 -a 列出目录下的所有文 ...
- 优化SQL 查询性能
为什么查询会很慢 如果把查询看作是一个任务,那么它由一系列子任务组成,每个子任务都会消耗一定的时间.要优化查询,实际上是要优化其子任务,要么消除其中一些子任务,要么减少子任务的执行次数,要么让子任务运 ...
- Bugku-login1(SKCTF)(SQL约束攻击)
原因 sql语句中insert和select对长度和空格的处理方式差异造成漏洞. select对参数后面的空格的处理方式是删除,insert只是取规定的最大长度的字符串. 逻辑 1.用 select ...
- 冲击BATZ!GitHub近8.3K+的Android进阶指南,面试再也不愁了
过去十年是移动互联网蓬勃发展的黄金期,相信每个人也都享受到了移动互联网红利,在此期间,移动互联网经历了曙光期.成长期.成熟期.现在来说已经进入饱和期. 依然记得在 2010-2013 年期间,从事移动 ...
- Android源码解析——Handler、Looper与MessageQueue
本文的目的是来分析下 Android 系统中以 Handler.Looper.MessageQueue 组成的异步消息处理机制,通过源码来了解整个消息处理流程的走向以及相关三者之间的关系 需要先了解以 ...
- openssl常用命令行汇总
openssl常用命令行汇总 随机数 openssl rand -out rand.dat -base64 32 摘要 直接做摘要 openssl dgst -sha1 -out dgst.dat p ...
- ABP框架使用Oracle数据库,并实现从SQLServer中进行数据迁移的处理
ABP框架的数据访问底层是基于EFCore(Entity Framework Core)的,是微软标志性且成熟的ORM,因此它本身是支持多种主流数据库MySQL,SqlServer,Oracle,SQ ...
- Java-Collection、Map及Array之间的转换
1 List -> Map 设个User类: public class User { private String userName; private String userId; privat ...
- Dll文件的创建与测试C#
创建Dll文件 首先使用VS 2019创建Dll项目,创建项目时选择"类库",如下图 在项目中创建类文件,添加测试代码: namespace PlantSim_C_Interfac ...