Mybtis-Plus常用的内置方法

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的配置请查看以下博客。

Spring Boot +MyBatis-Plus配置

  • 3
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值