1 Service CRUD 接口

  1. 通用 Service CRUD 封装IService (opens new window)接口,进一步封装 CRUD 采用 get 查询单行 remove 删除 list 查询集合 page 分页 前缀命名方式区分 Mapper 层避免混淆。
  2. 泛型 T 为任意实体对象

1.1 Save

// 插入一条记录(选择字段,策略插入)
boolean save(T entity);

User afanda = new User(100L, "afanda", 25, "123456789@qq.com");
boolean res = userService.save(afanda);

// 插入(批量)
boolean saveBatch(Collection entityList);

User afanda = new User(102L, "afanda", 25, "123456789@qq.com");
User dafenqi = new User(103L, "dafenqi", 25, "123456789@qq.com");
User lining = new User(104L, "lining", 25, "123456789@qq.com");
User alidasi = new User(105L, "alidasi", 25, "123456789@qq.com");
List<User> users = Arrays.asList(afanda, dafenqi, lining, alidasi);

// 插入(批量)
boolean saveBatch(Collection entityList, int batchSize);

        User afanda = new User(102L, "afanda", 25, "123456789@qq.com");User dafenqi = new User(103L, "dafenqi", 25, "123456789@qq.com");User lining = new User(104L, "lining", 25, "123456789@qq.com");User alidasi = new User(105L, "alidasi", 25, "123456789@qq.com");List<User> users = Arrays.asList(afanda, dafenqi, lining, alidasi);boolean res = userService.saveBatch(users, 2);

1.2 SaveOrUpdate

// TableId 注解存在更新记录,否插入一条记录
boolean saveOrUpdate(T entity);

        User afanda = new User(102L, "Lenin", 25, "123456789@qq.com");boolean res = userService.saveOrUpdate(afanda);



// 根据updateWrapper尝试更新,否继续执行saveOrUpdate(T)方法
boolean saveOrUpdate(T entity, Wrapper updateWrapper);

        UpdateWrapper<User> wrapper = new UpdateWrapper<User>().eq("name", "Stalin");User stalin = new User(106L, "Stalin", 25, "stalin@qq.com");boolean res = userService.saveOrUpdate(stalin, wrapper);


// 批量修改插入
boolean saveOrUpdateBatch(Collection entityList);
// 批量修改插入
boolean saveOrUpdateBatch(Collection entityList, int batchSize);

1.3 Remove

// 根据 entity 条件,删除记录
boolean remove(Wrapper queryWrapper);

        QueryWrapper<User> queryWrapper = new QueryWrapper<User>().eq("name", "Jone");boolean res = userService.remove(queryWrapper);


// 根据 ID 删除
boolean removeById(Serializable id);
// 根据 columnMap 条件,删除记录
boolean removeByMap(Map<String, Object> columnMap);

        Map<String, Object> map = new HashMap<>();map.put("name", "Jack");boolean res = userService.removeByMap(map);


// 删除(根据ID 批量删除)
boolean removeByIds(Collection<? extends Serializable> idList);

1.4 Update

// 根据 UpdateWrapper 条件,更新记录 需要设置sqlset
boolean update(Wrapper updateWrapper);

        boolean res = userService.update(new UpdateWrapper<User>().set("name", "Zhukov").eq("id", 3));

// 根据 whereWrapper 条件,更新记录
boolean update(T updateEntity, Wrapper whereWrapper);

        User user = new User(106L, "Mark Huafei", 25, "Mark Huafei@qq.com");UpdateWrapper<User> wrapper = new UpdateWrapper<User>().eq("id", 106L);

// 根据 ID 选择修改
boolean updateById(T entity);

        User user = new User(106L, "Jack Jones", 25, "Jack Jones@qq.com");boolean res = userService.updateById(user);


// 根据ID 批量更新
boolean updateBatchById(Collection entityList);

// 根据ID 批量更新
boolean updateBatchById(Collection entityList, int batchSize);

1.5 Get

// 根据 ID 查询
T getById(Serializable id);

        User user = userService.getById(106L);

// 根据 Wrapper,查询一条记录。结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last(“LIMIT 1”)
T getOne(Wrapper queryWrapper);

        User user = userService.getOne(new QueryWrapper<User>().eq("email", "123456789@qq.com"));


报错信息:Expected one result (or null) to be returned by selectOne(), but found: 4

// 根据 Wrapper,查询一条记录
T getOne(Wrapper queryWrapper, boolean throwEx);

// 根据 Wrapper,查询一条记录
Map<String, Object> getMap(Wrapper queryWrapper);

        Map<String, Object> userMap = userService.getMap(new QueryWrapper<User>().eq("email", "123456789@qq.com"));


// 根据 Wrapper,查询一条记录
V getObj(Wrapper queryWrapper, Function<? super Object, V> mapper);

1.6 List


// 查询所有
List list();

        List<User> users = userService.list();


// 查询列表
List list(Wrapper queryWrapper);

        List<User> users = userService.list(new QueryWrapper<User>().eq("email", "123456789@qq.com"));


// 查询(根据ID 批量查询)
Collection listByIds(Collection<? extends Serializable> idList);

        List<User> users = userService.listByIds(Arrays.asList(3, 4, 5));


// 查询(根据 columnMap 条件)
Collection listByMap(Map<String, Object> columnMap);

// 查询所有列表
List<Map<String, Object>> listMaps();

// 查询列表
List<Map<String, Object>> listMaps(Wrapper queryWrapper);

// 查询全部记录
List listObjs();

// 查询全部记录
List listObjs(Function<? super Object, V> mapper);

// 根据 Wrapper 条件,查询全部记录
List listObjs(Wrapper queryWrapper);

// 根据 Wrapper 条件,查询全部记录
List listObjs(Wrapper queryWrapper, Function<? super Object, V> mapper);

1.7 Page

package com.zs.config;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableTransactionManagement
@Configuration
public class MybatisPlusConfig {@Beanpublic PaginationInterceptor paginationInterceptor() {PaginationInterceptor paginationInterceptor = new PaginationInterceptor();paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));return paginationInterceptor;}
}

// 无条件分页查询
IPage page(IPage page);

 /*** Params:* current – 当前页* size – 每页显示条数*/Page<User> page = new Page<>(2, 2);Page<User> userPage = userService.page(page);System.out.println("getRecords :" + userPage.getRecords());System.out.println("getCurrent :" + userPage.getCurrent());System.out.println("getOrders :" + userPage.getOrders());System.out.println("getSize :" + userPage.getSize());System.out.println("getTotal :" + userPage.getTotal());System.out.println("getPages :" + userPage.getPages());


// 条件分页查询
IPage page(IPage page, Wrapper queryWrapper);

// 无条件分页查询
IPage<Map<String, Object>> pageMaps(IPage page);

// 条件分页查询
IPage<Map<String, Object>> pageMaps(IPage page, Wrapper queryWrapper);

1.8 Count

// 查询总记录数
int count();

        int res = userService.count();


// 根据 Wrapper 条件,查询总记录数
int count(Wrapper queryWrapper);

        int res = userService.count(new QueryWrapper<User>().eq("email", "123456789@qq.com"));

1.9 Chain

1.9.1 query

// 链式查询 普通
QueryChainWrapper query();

        Integer res = new QueryChainWrapper<User>(userMapper).eq("email", "123456789@qq.com").count();System.out.println(res);

        List<User> res = new QueryChainWrapper<User>(userMapper).eq("email", "123456789@qq.com").list();System.out.println(res);


// 链式查询 lambda 式。
LambdaQueryChainWrapper lambdaQuery();

        List<User> res = new LambdaQueryChainWrapper<User>(userMapper).eq(User::getAge, 25).list();System.out.println(res);

2 Mapper CRUD 接口

2.1 Insert

// 插入一条记录
int insert(T entity);

        User user = new User(9L, "Martyn Davies", 25, "Martyn Davies@qq.com");int res = userMapper.insert(user);

2.2 Delete

// 根据 entity 条件,删除记录
int delete(@Param(Constants.WRAPPER) Wrapper wrapper);

        int res = userMapper.delete(new QueryWrapper<User>().eq("id", 9L));


// 删除(根据ID 批量删除)
int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);

// 根据 ID 删除
int deleteById(Serializable id);

// 根据 columnMap 条件,删除记录
int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);

2.3 Update

// 根据 whereWrapper 条件,更新记录
int update(@Param(Constants.ENTITY) T updateEntity, @Param(Constants.WRAPPER) Wrapper whereWrapper);

        User user = new User(1L, "Maria cangai", 25, "Maria cangai@qq.com");int res = userMapper.update(user, new UpdateWrapper<User>().eq("id", 1L));


// 根据 ID 修改
int updateById(@Param(Constants.ENTITY) T entity);

2.4 Select

// 根据 ID 查询
T selectById(Serializable id);

// 根据 entity 条件,查询一条记录
T selectOne(@Param(Constants.WRAPPER) Wrapper queryWrapper);

        User user = userMapper.selectOne(new QueryWrapper<User>().eq("email", "cangai@qq.com"));

// 查询(根据ID 批量查询)
List selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);

// 根据 entity 条件,查询全部记录
List selectList(@Param(Constants.WRAPPER) Wrapper queryWrapper);

// 查询(根据 columnMap 条件)
List selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);

// 根据 Wrapper 条件,查询全部记录
List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper queryWrapper);

// 根据 Wrapper 条件,查询全部记录。注意: 只返回第一个字段的值
List selectObjs(@Param(Constants.WRAPPER) Wrapper queryWrapper);

        List<Object> users = userMapper.selectObjs(new QueryWrapper<User>().eq("email", "123456789@qq.com"));


// 根据 entity 条件,查询全部记录(并翻页)
IPage selectPage(IPage page, @Param(Constants.WRAPPER) Wrapper queryWrapper);

        Page<User> page = userMapper.selectPage(new Page<User>(1, 2), new QueryWrapper<User>().eq("email", "123456789@qq.com"));


// 根据 Wrapper 条件,查询全部记录(并翻页)
IPage<Map<String, Object>> selectMapsPage(IPage page, @Param(Constants.WRAPPER) Wrapper queryWrapper);
// 根据 Wrapper 条件,查询总记录数
Integer selectCount(@Param(Constants.WRAPPER) Wrapper queryWrapper);

02_MybatisPlus—CRUD 接口相关推荐

  1. Node+express+mongoose前端实现简单crud接口

    Node+express+mongoose前端实现建单crud接口 准备工作:需要安装依赖如下:node,express,mongoose,cors,requier-all 入口文件:index.js ...

  2. MybatisPlu自动生成CRUD接口(二)

    作为一个有一定经验开发人员,一定不希望自己每天都在做简单CRUD操作,这样其实只是在浪费自己的时间,那么有没有什么工具让我们自动生成对数据库的CRUD操作啦?答案是肯定的,下面我们就介绍下如何通过My ...

  3. 技能学习:学习使用php(tp6框架) + vue.js,开发前端全栈网站-7.分类的模型关联和通用CRUD接口

    技能学习:学习使用php(tp6框架) + vue.js,开发前端全栈网站-7.分类的模型关联和通用CRUD接口 技能学习:学习使用php(tp6框架) + vue.js,开发前端全栈网站-1.工具和 ...

  4. MyBatis-Plus(八)Mapper的CRUD接口5:增删改操作

    Mapper的CRUD接口5:增删改操作 1.新增数据 insert 方法可以将一个实体对象插入到对应的数据表中: @RestController public class HelloControll ...

  5. 通用 CRUD 接口

    假如 有两个模板 功能都是增删改查 接口都是相似的操作 就可以使用共享接口 实现不同的接口,在不同的数据库模型中实现增删改查功能 通过params参数引用数据库模型:(rest是接口的前缀,防止接口命 ...

  6. MybatisPlus学习〖三〗crud接口实现

    MybatisPlus学习[三] 我们继续学习mp的增删改查接口 代码具体的实现 条件构造器实现 1. 查询user表中user_name字段模糊查询'张',或者user_age年龄大于20岁,按照u ...

  7. 快速生成CRUD接口的神器-IDEA插件EasyCode

    1.IDEA使用EasyCode 本文使用的模板代码根据自己的需求进行了一部分调整,生成的controller只实现单表的CRUD,生成的代码基本用于后台管理.插件直接搜索下载即可 使用步骤: 对应生 ...

  8. Mybatis-Plus 05 CRUD接口-删除和逻辑删除

    一.删除 1.根据id删除记录 @Test public void testDeleteById(){int result = userMapper.deleteById(5L);System.out ...

  9. 手把手教你用nestjs框架7分钟生成crud风格接口

    Tobenew  ,感谢! node.js mongodbnestjs 1.安装nest.js框架 nest.js官方开发文档 全局安装nest框架 npm i -g @nestjs/cli 2. 新 ...

最新文章

  1. gzip压缩後的javascript在ie下不加载
  2. oracle账户锁定解决方法
  3. python控制结构是_Python之控制结构(if、while、for)
  4. 深圳网络推广提醒大家做好URL标准化,网站排名“事半功倍”!
  5. [INS-08109] Unexpected error occurred while validating inputs at state 'getOCMDetails'.
  6. 下载Nacos源码并运行
  7. spark submit参数及调优
  8. AssertJ的SoftAssertions –我们需要它们吗?
  9. poj2976 Dropping tests
  10. 【interview17.10】 主要元素:详解
  11. ThinkPHP6项目基操(8.多应用模式)
  12. 树形结构 —— 并查集
  13. 2014电池测试软件,【技术】关于锂电池GB31241-2014洗涤测试要求
  14. 快捷关闭win8中的应用页面---解决方法
  15. 教你6步定制你的Ubuntu桌面
  16. JavaScript的类型转换(字符转数字,数字转字符)
  17. 2012/10/31的工作总结——潘学
  18. 【SOEM主站】一、SOEM主站环境搭建及连接板子测试
  19. PSP超强自制系统3.52 M33最新傻瓜安装指南(Dark Alex-OE系统接班人)
  20. python求不规则图形面积_python计算不规则图形面积算法

热门文章

  1. 2019/9/2 日常学习的一天
  2. quartus模块图中模块文字显示不全
  3. 题解 | #求小球落地5次后所经历的路程和第5次反弹的高度#
  4. 浅析过滤敏感词过滤算法(C++)
  5. 有出租高性能服务器的么,租用高性能的美国服务器有怎样配置呢?
  6. 离谱!入职BYD一个月,心态崩了。。。
  7. ConEmu设置当前目录打开右键菜单
  8. Pointnet以及Pointnet++论文笔记
  9. CSS控制ul缩进间距和去掉li点的方法
  10. windows系统下如何删除带有两点的文件夹