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模型] 五.构造查询 [ ...
随机推荐
- FastAPI:一个测试人员视角的教程
前言 教程肯定谈不上了,主要还是就自己的理解分享内容而已 内容是连官方文档的基础教程都没涵盖起的 建议直接看官方文档 以个人视角来分享,希望各位通过这个可以写接口了 需要自取 完整视频链接:https ...
- 缩减Azure上Linux虚拟机系统盘容量
[话在前头] 这么些年微软 Azure 创建虚拟机一直不能修改系统盘大小,但很多时候实际又用不了这么大的操作系统磁盘.微软自己甚至还针对 Windows 服务器镜像推出一个 smalldisk 的镜像 ...
- Linux 数据库操作(一)
我们可以将用于数据服务的数据库分为关系型数据库和非关系型数据库,关系型数据库最典型的就是Mysql,以及和他同源的MariaDB数据库,oracle等,非关系型数据库则有redis数据库,mongod ...
- git分支merge冲突 error: you need to resolve your current index first
问题: 执行切换代码分支 git checkout featrue_2019-06-24 ,报错如下: error: you need to resolve your current index fi ...
- Python中比较运算符连用的语法规则
在Python中,比较运用符<.>.<=.>=.== .!=可以连用,但语法规则和其它编程语言不一样 以 == 为例,具体语法规则是: a == b == c == d 等价于 ...
- springboot整合javafx
原文(原码)参考地址: https://github.com/roskenet/springboot-javafx-support https://github.com/spartajet/javaf ...
- pthread_cleanup_push与pthread_cleanup_pop的理解
一.为什么会有pthread_cleanup_push与pthread_cleanup_pop: 一般来说,Posix的线程终止有两种情况:正常终止和非正常终止.线程主动调用pthread_exit( ...
- 1016 Phone Bills (25分)
复建的第一题 理解题意 读懂题目就是一个活,所以我们用观察输出法,可以看出来月份,以及时间和费用之间的关系. 定义过程 然后时间要用什么来记录呢?day hour minute 好麻烦呀..用字符串吧 ...
- Asp.Net Core 使用 Sqlite 数据库
在Asp.Net Core 使用 Sqlite 数据库 在Asp.Net Core(5.0)项目中使用Sqlite数据库的设置, 1,创建新web项目 2,安装下面的依赖包 Install-Packa ...
- DLL劫持漏洞
写文章的契机还是看沙雕群友挖了十多个DLL劫持的漏洞交CNVD上去了... 就想起来搜集整理一下这部分 0x01 前言 DLL(Dynamic Link Library)文件为动态链接库文件,又称&q ...