目录结构 数据库表结构 表数据 代码 运行结果 代码 运行结果 代码 (除此之外还有 运行结果 代码 运行结果 代码 运行结果 代码 运行结果 代码 (将 age = 18 的邮箱统一修改) 运行结果 代码 运行结果 代码(删除 运行结果 在上面的基础上新建 config 包,存放config配置文件 代码 注意 代码示例 运行结果展示 代码示例 运行结果展示SpringBoot 整合 Mybatis-plus 【二】
一、MP中简单的CRUD
1. 预先准备
User.java
实体类
2. 查询操作(select)
2.1 查询所有 selectAll
/** * 查询所有 */ @Test public void testSelectAll() { List<User> userList = userMapper.selectList(null); userList.forEach(user -> System.out.println("user: " + user)); }
2.2 通过ID查询 selectById
/** * 根据ID查询 */ @Test public void testSelectById(){ User user = userMapper.selectById("2"); System.out.println("user: " + user); }
2.3 条件查询
between
等方法) /** * 条件查询 */ @Test public void testSelect(){ QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("age", "18"); // 设置等值查询 age = 18 // queryWrapper.lt("age", "18"); // 设置小于查询 age < 18 // queryWrapper.le("age", "18"); // 设置小于等于查询 age <= 18 // queryWrapper.gt("age", "18"); // 设置大于查询 age > 18 // queryWrapper.ge("age", "18"); // 设置大于等于查询 age >= 18 List<User> users = userMapper.selectList(queryWrapper); users.forEach(user -> System.out.println("user: " + user)); }
2.4 模糊查询 like
/** * 模糊查询 */ @Test public void testSelectLike(){ QueryWrapper<User> queryWrapper = new QueryWrapper<>(); //注意: column 需要是数据库中的表字段,而不是实体类中的 queryWrapper.like("name", "李"); //like %?% //queryWrapper.likeLeft("name", "zh"); //like %?% 以 XXX 结尾 //queryWrapper.likeRight("name", "李"); //like ?% 以 XXX 开头 List<User> users = userMapper.selectList(queryWrapper); users.forEach(user -> System.out.println("user:" + user)); }
3. 插入操作(insert)
3.1 插入一条 insert
/** * 插入数据 */ @Test public void testInsertUser(){ User user = new User(); user.setName("小天儿").setAge(18).setEmail("tt@test.com"); userMapper.insert(user); }
数据库中 新增一条数据
4. 修改操作(update)
4.1 通过id修改 updateById
/** * 根据 Id 修改 */ @Test public void testUpdateById(){ User user = userMapper.selectById("1"); user.setName("张三02"); userMapper.updateById(user); }
查看数据库 修改成功 (张三 – > 张三02)
4.2 批量修改(通过条件修改)
/** * 批量修改 * 基于条件修改 */ @Test public void testUpdate(){ User user = new User(); user.setEmail("age18@testUpdate.com"); QueryWrapper<User> updateWrapper = new QueryWrapper<>(); updateWrapper.eq("age", "18"); userMapper.update(user, updateWrapper); }
可看到数据库中 age=18
的 用户,邮箱已统一修改为 age18@testUpdate.com
5. 删除操作(delete)
5.1 根据id删除 deleteById
/** * 根据 Id 删除 */ @Test public void testDeleteById(){ userMapper.deleteById("4"); }
可查看到数据库中 id = 4
的用户已被删除
5.2 批量删除(根据条件删除)
age > 18
的所有用户) /** * 批量删除 * 基于条件删除 */ @Test public void testDelete(){ QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.gt("age", "18"); // 查询 年龄 大于 18 的用户 userMapper.delete(queryWrapper); }
数据库表中可以看到 age > 18
的 id 为5号、6号用户已被删除
二、分页插件使用
1. 预先配置
1.1 目录结构
1.2 预先config 配置
package com.demo.xiaotianer.mybatis.plus.config; import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.transaction.annotation.EnableTransactionManagement; @EnableTransactionManagement @Configuration @MapperScan("com.demo.xiaotianer.mybatis.plus.mapper") //Dao接口所在包 public class MybatisPlusConfig { /** * 分页拦截器对象 (目前只支持单表查询) * @return */ @Bean public PaginationInterceptor paginationInterceptor() { PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); return paginationInterceptor; } }
1、 使用分页查询必须设置mybatis-plus
提供的分页插件,才能实现分页效果。
2、目前分页插件仅仅支持 单表查询
。
3、注意引入的注解,不要引错了~@EnableTransactionManagement @Configuration @MapperScan("com.demo.xiaotianer.mybatis.plus.mapper") //Dao接口所在包
2、分页查询测试
2.1 非条件分页查询
/** * 分页查询(查询所有) */ @Test public void testSelectPage(){ //参数 1:当前页(默认值 1); 参数 2 : 每页展示条数(默认值 10) IPage<User> page = new Page<>(1, 2); page = userMapper.selectPage(page, null); long total = page.getTotal(); System.out.println("总条数:" + total); page.getRecords().forEach(user -> System.out.println("user: " + user)); }
2.2 带条件分页查询
/** * 按照条件分页查询 */ @Test public void testFindPage(){ QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("age", "18"); // age = 18 //参数 1:当前页(默认值 1); 参数 2 : 每页展示条数(默认值 10) IPage<User> page = new Page<>(1, 2); page = userMapper.selectPage(page, queryWrapper); page.getRecords().forEach(user -> System.out.println("user: " + user)); }
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算