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 中常用复杂查询的使用方法,你可以根据实际需求进行调整。

相关推荐
文丑颜不良啊1 小时前
LeetCode2221~2240题目解析
java·leetcode
秋饼1 小时前
Computer Use Agent Java 落地实战:感知-行动循环、执行器与安全围栏生产级实现
java·ai·技术分享·后端开发
文丑颜不良啊3 小时前
LeetCode2161~2180题目解析
java·leetcode
文丑颜不良啊5 小时前
LeetCode2141~2160题目解析
java·leetcode
LabVIEW开发5 小时前
LabVIEW 连接板:28 个端子怎么排,优先级怎么定
java·算法·labview
天天被压力5 小时前
【A股分钟级高频数据完全手册 #07】北交所历史分钟K线接口:京市Pro版分时批量
java·人工智能·python
天天被压力6 小时前
【A股分钟级高频数据完全手册 #08】股票日、分时数据接口对比:普通分时与Pro版差异实测
java·人工智能·python
特立独行的猫A6 小时前
AI 时代,我为什么推荐用 cj-tauri 开发桌面应用
java·后端·python
金銀銅鐵6 小时前
从一个类名到一张类图:用反射自动生成 PlantUML
java·后端·uml
nice_lcj5206 小时前
Java 并发容器入门:ConcurrentHashMap、CopyOnWriteArrayList、阻塞队列
java·面试·并发编程·阻塞队列·concurrenthashmap·copyonwritearraylist