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 {@TableIdprivate 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 {@Autowiredprivate 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 {@Autowiredprivate 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 {@Autowiredprivate 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 {@Autowiredprivate 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 {@Autowiredprivate UserService userService;public static void main(String[] args) {SpringApplication.run(Application.class, args);}@Overridepublic 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 中常用复杂查询的使用方法,你可以根据实际需求进行调整。