MybatisPlus较全常用复杂查询引例(limit、orderby、groupby、having、like...)

MyBatis-Plus 是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。以下是 MyBatis-Plus 中常用复杂查询(如 LIMIT、ORDER BY、GROUP BY、HAVING、LIKE 等)的引例:

1. 环境准备

首先,确保你已经在项目中添加了 MyBatis-Plus 的依赖。以 Maven 为例:

复制代码
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.3.1</version>
</dependency>

2. 实体类和 Mapper 接口

假设我们有一个 User 实体类和对应的 UserMapper 接口:

复制代码
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

@Data
@TableName("user")
public class User {
    @TableId
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserMapper extends BaseMapper<User> {
}

3. 常用复杂查询示例

3.1 LIMIT 查询

LIMIT 用于限制查询结果的数量。在 MyBatis-Plus 中,可以使用 Page 类来实现类似 LIMIT 的功能。

复制代码
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public List<User> getUsersWithLimit(int pageNum, int pageSize) {
        Page<User> page = new Page<>(pageNum, pageSize);
        IPage<User> userPage = userMapper.selectPage(page, null);
        return userPage.getRecords();
    }
}
3.2 ORDER BY 查询

ORDER BY 用于对查询结果进行排序。在 MyBatis-Plus 中,可以使用 QueryWrapper 来实现排序。

复制代码
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public List<User> getUsersOrderByAgeDesc() {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("age");
        return userMapper.selectList(wrapper);
    }
}
3.3 GROUP BY 和 HAVING 查询

GROUP BY 用于对查询结果进行分组,HAVING 用于筛选分组后的结果。在 MyBatis-Plus 中,可以使用 QueryWrapper 结合 groupBy 和 having 方法来实现。

复制代码
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public List<Map<String, Object>> getUsersGroupByAgeHavingCount() {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.select("age", "COUNT(*) as count")
               .groupBy("age")
               .having("COUNT(*) > 1");
        return userMapper.selectMaps(wrapper);
    }
}
3.4 LIKE 查询

LIKE 用于模糊查询。在 MyBatis-Plus 中,可以使用 QueryWrapper 的 like 方法来实现。

复制代码
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public List<User> getUsersLikeName(String keyword) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like("name", keyword);
        return userMapper.selectList(wrapper);
    }
}

4. 测试代码

复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.List;
import java.util.Map;

@SpringBootApplication
public class Application implements CommandLineRunner {
    @Autowired
    private UserService userService;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        // LIMIT 查询
        List<User> usersWithLimit = userService.getUsersWithLimit(1, 10);
        System.out.println("LIMIT 查询结果:" + usersWithLimit);

        // ORDER BY 查询
        List<User> usersOrderByAgeDesc = userService.getUsersOrderByAgeDesc();
        System.out.println("ORDER BY 查询结果:" + usersOrderByAgeDesc);

        // GROUP BY 和 HAVING 查询
        List<Map<String, Object>> usersGroupByAgeHavingCount = userService.getUsersGroupByAgeHavingCount();
        System.out.println("GROUP BY 和 HAVING 查询结果:" + usersGroupByAgeHavingCount);

        // LIKE 查询
        List<User> usersLikeName = userService.getUsersLikeName("张");
        System.out.println("LIKE 查询结果:" + usersLikeName);
    }
}

以上示例展示了 MyBatis-Plus 中常用复杂查询的使用方法,你可以根据实际需求进行调整。

相关推荐
两点王爷2 小时前
Java 面试高频:JWT、Cookie、Session、Token 鉴权详解
java·开发语言·面试
悟道书生4 小时前
hashMap/hashSet/hashCode详解
java
nice_lcj5205 小时前
依赖注入的三种方式:构造器、Setter、字段怎么选
java·spring·面试·依赖注入·字段注入·构造器注入
噢,我明白了5 小时前
实际项目导出方式应用
java·状态模式·导出
جيون داد ناالام ميづ7 小时前
七、MyBatis 源码剖析:核心流程拆解(初始化 + SQL 执行)
java·sql·mybatis
ClkLog-开源埋点用户分析7 小时前
【产品说明】【埋点分析】ClkLog 商业版 Java 运行环境安全更新说明
java·开发语言
秋饼7 小时前
Spring Framework 7 内置弹性实战:@Retryable 语义翻转、虚拟线程流量放大与 LLM 网关治理
java·ai·技术分享·后端开发
RuoyiOffice7 小时前
Flowable只是起点:一套OA背后的单据、资源与协作设计
java·vue.js·spring boot·flowable·企业管理·oa·ruoyi office
yuzhiboyouye8 小时前
《阿里巴巴 Java 开发手册》的 MySQL 核心规范
java·开发语言·mysql