Mybatis的强大条件构造器 QueryWrapper

2023-12-21 16:48

本文主要是介绍Mybatis的强大条件构造器 QueryWrapper,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Mybatis的高效开发

QueryWrapper的使用代码
官网解释

package com.lqf.crud;import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lqf.crud.bean.crm.User;
import com.lqf.crud.dao.crm.UserMapper;
import com.sun.org.apache.xerces.internal.util.EntityResolverWrapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.RowBounds;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.jsf.el.WebApplicationContextFacesELResolver;import javax.naming.Name;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;@RunWith(SpringRunner.class)
@SpringBootTest
public class QueryWrapperTests {@Autowiredprivate UserMapper mapper;/*** <p>* 根据根据 entity 条件,删除记录,QueryWrapper实体对象封装操作类(可以为 null)* 下方获取到queryWrapper后删除的查询条件为name字段为null的and年龄大于等于12的and email字段不为null的* 同理写法条件添加的方式就不做过多介绍了。* </p>*/@Testpublic void delete() {QueryWrapper<User> queryWrapper = new QueryWrapper<>();queryWrapper.isNull("name").ge("age", 12).isNotNull("email");int delete = mapper.delete(queryWrapper);System.out.println("delete return count = " + delete);}/*** <p>* 根据 entity 条件,查询一条记录,* 这里和上方删除构造条件一样,只是seletOne返回的是一条实体记录,当出现多条时会报错* </p>*/@Testpublic void selectOne() {QueryWrapper<User> queryWrapper = new QueryWrapper<>();queryWrapper.eq("name", "lqf");User user = mapper.selectOne(queryWrapper);System.out.println(user);}/*** <p>* 根据 Wrapper 条件,查询总记录数* </p>** @param queryWrapper 实体对象*/@Testpublic void selectCount() {QueryWrapper<User> queryWrapper = new QueryWrapper<>();queryWrapper.eq("name", "lqf");Integer count = mapper.selectCount(queryWrapper);System.out.println(count);}/*** <p>* 根据 entity 条件,查询全部记录* </p>** @param queryWrapper 实体对象封装操作类(可以为 null)为null查询全部*/@Testpublic void selectList() {List<User> list = mapper.selectList(null);System.out.println(list);}/*** <p>* 根据 Wrapper 条件,查询全部记录* </p>** @param queryWrapper 实体对象封装操作类(可以为 null)*/@Testpublic void selectMaps() {QueryWrapper<User> queryWrapper = new QueryWrapper<>();queryWrapper.isNotNull("name");List<Map<String, Object>> maps = mapper.selectMaps(queryWrapper);for (Map<String, Object> map : maps) {System.out.println(map);}}/*** 打印结果* {name=lqf, id=1046282328366391406, age=12, email=lqf@163.com, status=false}* {name=lqf, id=1046282328366391407, age=12, email=lqf@163.com, status=false}* {name=lqf, id=1046282328366391408, age=12, email=lqf@163.com, status=false}* {name=lqf, id=1046282328366391409, age=12, email=lqf@163.com, status=false}* {name=lqf, id=1046282328366391410, age=12, email=lqf@163.com, status=false}* {name=lqf, id=1046282328366391411, age=12, email=lqf@163.com, status=false}* {name=lqf, id=1046282328366391412, age=12, email=lqf@163.com, status=false}* {name=lqf, id=1046282328366391413, age=12, email=lqf@163.com, status=false}* {name=lqf, id=1046282328366391414, age=12, email=lqf@163.com, status=false}* {name=lqf, id=1046282328366391415, age=12, email=lqf@163.com, status=false}* {name=lqf, id=1046282328366391416, age=12, email=lqf@163.com, status=false}* {name=lqf, id=1046282328366391417, age=12, email=lqf@163.com, status=false}* {name=lqf, id=1046282328366391418, age=12, email=lqf@163.com, status=false}* json类型的键值对模式*//*** <p>* 根据 entity 条件,查询全部记录(并翻页)* </p>** @param page         分页查询条件(可以为 RowBounds.DEFAULT)* @param queryWrapper 实体对象封装操作类(可以为 null)*/@Testpublic void selectPage() {Page<User> page = new Page<>(1, 5);QueryWrapper<User> queryWrapper = new QueryWrapper<>();IPage<User> userIPage = mapper.selectPage(page, queryWrapper);System.out.println(userIPage);}/*** 打印结果* ==>  Preparing: SELECT COUNT(1) FROM user* ==> Parameters:* <==    Columns: COUNT(1)* <==        Row: 100* ==>  Preparing: SELECT id,name,age,email,status FROM user LIMIT 0,5* ==> Parameters:* <==    Columns: id, name, age, email, status* <==        Row: 1046282328366391319, lqf, 12, lqf@163.com, 0* <==        Row: 1046282328366391320, lqf, 12, lqf@163.com, 0* <==        Row: 1046282328366391321, lqf, 12, lqf@163.com, 0* <==        Row: 1046282328366391322, lqf, 12, lqf@163.com, 0* <==        Row: 1046282328366391323, lqf, 12, lqf@163.com, 0* <==      Total: 5*** 这里需要在项目中加入分页插件*   @Bean*     public PaginationInterceptor paginationInterceptor() {*         return new PaginationInterceptor();*     }*//*** <p>* 根据 Wrapper 条件,查询全部记录(并翻页)* </p>** @param page         分页查询条件* @param queryWrapper 实体对象封装操作类*/@Testpublic void selectMapsPage() {Page<User> page = new Page<>(1, 5);QueryWrapper<User> queryWrapper = new QueryWrapper<>();IPage<Map<String, Object>> mapIPage = mapper.selectMapsPage(page, queryWrapper);System.out.println(mapIPage);}/*** 和上个分页同理只是返回类型不同*//*** <p>* 根据 whereEntity 条件,更新记录* </p>** @param entity        实体对象 (set 条件值,不能为 null)* @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)*/@Testpublic void update() {//修改值User user = new User();user.setStatus(true);user.setName("zhangsan");//修改条件sUpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();userUpdateWrapper.eq("name", "lqf");int update = mapper.update(user, userUpdateWrapper);System.out.println(update);}/*** 打印结果* ==>  Preparing: UPDATE user SET name=?, status=? WHERE name = ?* ==> Parameters: zhangsan(String), true(Boolean), lqf(String)* <==    Updates: 100* Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@56a4f272]* 100* 2018-10-02 15:08:03.928  INFO 7972 --- [       Thread-2] o.s.w.c.s.GenericWebApplicationContext   : Closing org.springframework.web.context.support.GenericWebApplicationContext@37313c65: startup date [Tue Oct 02 15:08:00 CST 2018]; root of context hierarchy* 2018-10-02 15:08:03.937  INFO 7972 --- [       Thread-2] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...* 2018-10-02 15:08:04.053  INFO 7972 --- [       Thread-2] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.** Process finished with exit code 0*/}

项目中的使用方式

    @Overridepublic List<User> getAllAdminUsers(){QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();userQueryWrapper.eq(User.Fields.admin, "1");return list(userQueryWrapper);}

参考链接
参考链接2

这篇关于Mybatis的强大条件构造器 QueryWrapper的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/520753

相关文章

破茧 JDBC:MyBatis 在 Spring Boot 中的轻量实践指南

《破茧JDBC:MyBatis在SpringBoot中的轻量实践指南》MyBatis是持久层框架,简化JDBC开发,通过接口+XML/注解实现数据访问,动态代理生成实现类,支持增删改查及参数... 目录一、什么是 MyBATis二、 MyBatis 入门2.1、创建项目2.2、配置数据库连接字符串2.3、入

MyBatis-Plus 自动赋值实体字段最佳实践指南

《MyBatis-Plus自动赋值实体字段最佳实践指南》MyBatis-Plus通过@TableField注解与填充策略,实现时间戳、用户信息、逻辑删除等字段的自动填充,减少手动赋值,提升开发效率与... 目录1. MyBATis-Plus 自动赋值概述1.1 适用场景1.2 自动填充的原理1.3 填充策略

mybatis中resultMap的association及collectio的使用详解

《mybatis中resultMap的association及collectio的使用详解》MyBatis的resultMap定义数据库结果到Java对象的映射规则,包含id、type等属性,子元素需... 目录1.reusltmap的说明2.association的使用3.collection的使用4.总

mybatis-plus QueryWrapper中or,and的使用及说明

《mybatis-plusQueryWrapper中or,and的使用及说明》使用MyBatisPlusQueryWrapper时,因同时添加角色权限固定条件和多字段模糊查询导致数据异常展示,排查发... 目录QueryWrapper中or,and使用列表中还要同时模糊查询多个字段经过排查这就导致只要whe

SpringBoot集成MyBatis实现SQL拦截器的实战指南

《SpringBoot集成MyBatis实现SQL拦截器的实战指南》这篇文章主要为大家详细介绍了SpringBoot集成MyBatis实现SQL拦截器的相关知识,文中的示例代码讲解详细,有需要的小伙伴... 目录一、为什么需要SQL拦截器?二、MyBATis拦截器基础2.1 核心接口:Interceptor

MySQL 8 中的一个强大功能 JSON_TABLE示例详解

《MySQL8中的一个强大功能JSON_TABLE示例详解》JSON_TABLE是MySQL8中引入的一个强大功能,它允许用户将JSON数据转换为关系表格式,从而可以更方便地在SQL查询中处理J... 目录基本语法示例示例查询解释应用场景不适用场景1. ‌jsON 数据结构过于复杂或动态变化‌2. ‌性能要

MyBatis-Plus通用中等、大量数据分批查询和处理方法

《MyBatis-Plus通用中等、大量数据分批查询和处理方法》文章介绍MyBatis-Plus分页查询处理,通过函数式接口与Lambda表达式实现通用逻辑,方法抽象但功能强大,建议扩展分批处理及流式... 目录函数式接口获取分页数据接口数据处理接口通用逻辑工具类使用方法简单查询自定义查询方法总结函数式接口

MyBatis中$与#的区别解析

《MyBatis中$与#的区别解析》文章浏览阅读314次,点赞4次,收藏6次。MyBatis使用#{}作为参数占位符时,会创建预处理语句(PreparedStatement),并将参数值作为预处理语句... 目录一、介绍二、sql注入风险实例一、介绍#(井号):MyBATis使用#{}作为参数占位符时,会

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推

MyBatis-Plus 中 nested() 与 and() 方法详解(最佳实践场景)

《MyBatis-Plus中nested()与and()方法详解(最佳实践场景)》在MyBatis-Plus的条件构造器中,nested()和and()都是用于构建复杂查询条件的关键方法,但... 目录MyBATis-Plus 中nested()与and()方法详解一、核心区别对比二、方法详解1.and()