当前位置:网站首页>Mybtis-Plus常用的内置方法
Mybtis-Plus常用的内置方法
2022-07-27 16:11:00 【程序员柒七】
1 插入方法
1.1 save()
插入单条数据。
/**
* 使用内置方法save(插入)
*
* @return
*/
@PostMapping("/save")
public boolean save(@RequestBody UserEntity userEntity) {
return userService.save(userEntity);
}1.2 saveBatch()
批量插入数据。
/**
* 使用内置方法saveBatch(插入)
*
* @return
*/
@PostMapping("/saveBatch")
public boolean saveBatch(@RequestBody List<UserEntity> userList) {
return userService.saveBatch(userList);
}2 更新方法
2.1 updateById()
更新单条数据。
/**
* 使用内置方法updateById(更新)
*
* @return
*/
@PostMapping("/updateById")
public boolean updateById(@RequestBody UserEntity userEntity) {
return userService.updateById(userEntity);
}2.2 updateBatchById()
批量更新数据。
/**
* 使用内置方法updateBatchById(更新)
*
* @return
*/
@PostMapping("/updateBatchById")
public boolean updateBatchById(@RequestBody List<UserEntity> userList) {
return userService.updateBatchById(userList);
}3 插入或更新方法
3.1 saveOrUpdate()
插入或更新单条数据。
/**
* 使用内置方法saveOrUpdate(插入或更新)
*
* @return
*/
@PostMapping("/saveOrUpdate")
public boolean saveOrUpdate(@RequestBody UserEntity userEntity) {
return userService.saveOrUpdate(userEntity);
}3.2 saveOrUpdateBatch()
批量插入或更新数据。
/**
* 使用内置方法saveOrUpdateBatch(插入或更新)
*
* @return
*/
@PostMapping("/saveOrUpdateBatch")
public boolean saveOrUpdateBatch(@RequestBody List<UserEntity> userList) {
return userService.saveOrUpdateBatch(userList);
}4 删除方法
4.1 removeById()
删除单条数据。
/**
* 使用内置方法removeById(删除)
*
* @return
*/
@DeleteMapping("/removeById/{username}")
public boolean removeById(@PathVariable("username") String username) {
return userService.removeById(username);
}4.2 removeByIds()
批量删除数据。
/**
* 使用内置方法removeByIds(删除)
*
* @return
*/
@DeleteMapping("removeByIds")
public boolean removeByIds(@RequestBody List<String> usernameList) {
return userService.removeByIds(usernameList);
}5 查询数据
5.1 list()
根据指定条件查询列表数据,返回数据类型为实体类类型。
/**
* 使用内置方法list(查询)
*
* @return
*/
@GetMapping("list")
public List<UserEntity> list() {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.lambda().eq(UserEntity::getUsername, "123");
return userService.list(queryWrapper);
}5.2 listMaps()
根据指定条件查询列表数据,返回数据类型为Map类型。
/**
* 使用内置方法listMaps(查询)
*
* @return
*/
@GetMapping("listMaps")
public List<Map<String, Object>> listMaps() {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.lambda().eq(UserEntity::getUsername, "123");
return userService.listMaps(queryWrapper);
}5.3 listObjs()
根据指定条件查询列表数据,返回数据类型为Object类型。
/**
* 使用内置方法listObjs(查询)
*
* @return
*/
@GetMapping("listObjs")
public List<Object> listObjs() {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.lambda().eq(UserEntity::getUsername, "123");
return userService.listObjs(queryWrapper);
}5.4 listByIds()
根据主键列表查询列表数据,返回数据类型为实体类类型。
/**
* 使用内置方法listByIds(查询)
*
* @return
*/
@GetMapping("listByIds")
public List<UserEntity> listByIds() {
return userService.listByIds(new ArrayList<>(Arrays.asList("123")));
}5.5 listByMap()
根据指定条件查询列表数据,返回数据类型为实体类类型。
/**
* 使用内置方法listByMap(查询)
*
* @return
*/
@GetMapping("listByMap")
public List<UserEntity> listByMap() {
Map<String, Object> columnMap = new HashMap<>();
columnMap.put("username", "123");
return userService.listByMap(columnMap);
}5.6 getById()
根据主键查询单条数据,返回数据类型为实体类类型。
/**
* 使用内置方法getById(查询)
*
* @return
*/
@GetMapping("getById")
public UserEntity getById() {
return userService.getById("123");
}5.7 getOne()
根据指定条件查询单条数据,返回数据类型为实体类类型。
/**
* 使用内置方法getOne(查询)
*
* @return
*/
@GetMapping("getOne")
public UserEntity getOne() {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.lambda().eq(UserEntity::getUsername, "123");
return userService.getOne(queryWrapper);
}5.8 getMap()
根据指定条件查询单条数据,返回数据类型为Map类型。
/**
* 使用内置方法getMap(查询)
*
* @return
*/
@GetMapping("getMap")
public Map<String, Object> getMap() {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.lambda().eq(UserEntity::getUsername, "123");
return userService.getMap(queryWrapper);
}5.9 count()
根据指定条件查询记录条数。
/**
* 使用内置方法count(查询)
*
* @return
*/
@GetMapping("count")
public int count() {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.lambda().eq(UserEntity::getUsername, "123");
return userService.count(queryWrapper);
}5.10 page()
根据指定条件查询数据并分页,返回数据类型为实体类类型。
/**
* 使用内置方法page(查询)
*
* @return
*/
@GetMapping("page")
public Page<UserEntity> page() {
Page<UserEntity> page = new Page<>(1, 10);
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.lambda().eq(UserEntity::getUsername, "123");
return userService.page(page, queryWrapper);
}5.11 page
/**
* 使用内置方法pageMaps(查询)
*
* @return
*/
@GetMapping("pageMaps")
public Page<Map<String, Object>> pageMaps() {
Page<Map<String, Object>> page = new Page<>(1, 10);
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.lambda().eq(UserEntity::getUsername, "123");
return userService.pageMaps(page, queryWrapper);
}Maps()
根据指定条件查询数据并分页,返回数据类型为Map类型。
/**
* 使用内置方法pageMaps(查询)
*
* @return
*/
@GetMapping("pageMaps")
public Page<Map<String, Object>> pageMaps() {
Page<Map<String, Object>> page = new Page<>(1, 10);
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
queryWrapper.lambda().eq(UserEntity::getUsername, "123");
return userService.pageMaps(page, queryWrapper);
}注:
有关MyBatis-Plus的配置请查看以下博客。
边栏推荐
- [MIT 6.S081] Lab 6: Copy-on-Write Fork for xv6
- 2021.8.1笔记 数据库设计
- 1. OpenCV image basic operation
- 微信小程序 wxacode.getUnlimited生成小程序码
- [MIT 6.S081] Lec 4: Page tables 笔记
- Deep learning: gat
- Deep learning: Gan case exercise -minst handwritten digits
- 输入框blur事件与click事件冲突问题
- 2. Change color space and color detection
- 记一次 .NET 某智慧物流 WCS系统 CPU 爆高分析
猜你喜欢
随机推荐
江苏华存首发PCIe 5.0 SSD主控:台积电12nm工艺,2020年量产
Error launching IDEA
Marvell announced the roadmap of its arm server chip, and the performance of the next generation will be twice that of thunderx2
[MIT 6.S081] Lab 5: xv6 lazy page allocation
[MIT 6.S081] Lab 7: Multithreading
微信小程序多文件上传
2021.7.19笔记 DML
Common commands of database 1
深度学习-论文阅读:动作结构性图卷积网络AS-GCN
Navicat 导出表生成PDM文件
Deep learning: Gan case exercise -minst handwritten digits
[mit 6.s081] LEC 5: calling conventions and stack frames risc-v notes
Linked list storage structure of dynamic linked list 2 stack (linkedstack Implementation)
Binary tree concept
2021.8.7笔记 servlet
邮件安全运营难?Coremail携手云商店打造企业邮箱办公新生态!
2021.7.18笔记 mysql数据类型
Press Google and NVIDIA! Alibaba optical 800 chip won the world's first authoritative test again
荣耀、小米发双十一战报:都称自己是冠军
rsa加解密(兼容微信小程序环境)
![[MIT 6.S081] Lab 11: networking](/img/9d/cca59a662412f3c3c57c26c5987a24.png)



![[mit 6.s081] LEC 9: interrupts notes](/img/b6/a8d39aa7ede4eb1c5a74e6d15b3b1c.png)

![[mit 6.s081] LEC 10: multiprocessors and locking notes](/img/62/ca6362830321feaf450865132cdea9.png)

