在线作图网站,wordpress 登陆链接,阿里有做网站,东莞专业的网站推广价格MyBatis-Plus 是一个 MyBatis 的增强工具#xff0c;在 MyBatis 的基础上只做增强不做改变#xff0c;为简化开发、提高效率而生。以下是 MyBatis-Plus 中常用复杂查询#xff08;如 LIMIT、ORDER BY、GROUP BY、HAVING、LIKE 等#xff09;的引例#xff1a;
1. 环境准备…MyBatis-Plus 是一个 MyBatis 的增强工具在 MyBatis 的基础上只做增强不做改变为简化开发、提高效率而生。以下是 MyBatis-Plus 中常用复杂查询如 LIMIT、ORDER BY、GROUP BY、HAVING、LIKE 等的引例
1. 环境准备 首先确保你已经在项目中添加了 MyBatis-Plus 的依赖。以 Maven 为例 dependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-boot-starter/artifactIdversion3.5.3.1/version
/dependency2. 实体类和 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 BaseMapperUser {
}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 ListUser getUsersWithLimit(int pageNum, int pageSize) {PageUser page new Page(pageNum, pageSize);IPageUser 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 ListUser getUsersOrderByAgeDesc() {QueryWrapperUser 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 ListMapString, Object getUsersGroupByAgeHavingCount() {QueryWrapperUser 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 ListUser getUsersLikeName(String keyword) {QueryWrapperUser 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 查询ListUser usersWithLimit userService.getUsersWithLimit(1, 10);System.out.println(LIMIT 查询结果 usersWithLimit);// ORDER BY 查询ListUser usersOrderByAgeDesc userService.getUsersOrderByAgeDesc();System.out.println(ORDER BY 查询结果 usersOrderByAgeDesc);// GROUP BY 和 HAVING 查询ListMapString, Object usersGroupByAgeHavingCount userService.getUsersGroupByAgeHavingCount();System.out.println(GROUP BY 和 HAVING 查询结果 usersGroupByAgeHavingCount);// LIKE 查询ListUser usersLikeName userService.getUsersLikeName(张);System.out.println(LIKE 查询结果 usersLikeName);}
}以上示例展示了 MyBatis-Plus 中常用复杂查询的使用方法你可以根据实际需求进行调整。