mybatis plus条件构造器中updateWrapper和queryWrapper的使用方法 您所在的位置:网站首页 lambdaupdatewrapper更新 mybatis plus条件构造器中updateWrapper和queryWrapper的使用方法

mybatis plus条件构造器中updateWrapper和queryWrapper的使用方法

2023-04-14 03:16| 来源: 网络整理| 查看: 265

mybatis plus条件构造器中updateWrapper和queryWrapper的使用方法 发布时间:2020-11-05 17:31:01 来源:亿速云 阅读:4049 作者:Leah 栏目:开发技术

这篇文章运用简单易懂的例子给大家介绍mybatis plus条件构造器中updateWrapper和queryWrapper的使用方法,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

一、条件构造器关系介绍

mybatis plus条件构造器中updateWrapper和queryWrapper的使用方法

介绍 :

1.上图绿色框为抽象类abstract2.蓝色框为正常class类,可new对象3.黄色箭头指向为父子类关系,箭头指向为父类

wapper介绍 :

1.Wrapper : 条件构造抽象类,最顶端父类,抽象类中提供4个方法西面贴源码展示2.AbstractWrapper : 用于查询条件封装,生成 sql 的 where 条件3.AbstractLambdaWrapper : Lambda 语法使用 Wrapper统一处理解析 lambda 获取 column。4.LambdaQueryWrapper :看名称也能明白就是用于Lambda语法使用的查询Wrapper5.LambdaUpdateWrapper : Lambda 更新封装Wrapper6.QueryWrapper : Entity 对象封装操作类,不是用lambda语法7.UpdateWrapper : Update 条件封装,用于Entity对象更新操作

二、项目实例

在这里我以QueryWrapper和UpdateWrapper为例,进行测试讲解。我会在上篇博客原有的基础上进行测试,如果不喜欢搭建项目的可直接下载我上个项目,上个项目的博客对应上个项目的讲解

mybatis plus条件构造器中updateWrapper和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 { @Autowired private UserMapper mapper; /** *

* 根据根据 entity 条件,删除记录,QueryWrapper实体对象封装操作类(可以为 null) * 下方获取到queryWrapper后删除的查询条件为name字段为null的and年龄大于等于12的and email字段不为null的 * 同理写法条件添加的方式就不做过多介绍了。 *

*/ @Test public void delete() { QueryWrapper queryWrapper = new QueryWrapper(); queryWrapper .isNull("name") .ge("age", 12) .isNotNull("email"); int delete = mapper.delete(queryWrapper); System.out.println("delete return count = " + delete); } /** *

* 根据 entity 条件,查询一条记录, * 这里和上方删除构造条件一样,只是seletOne返回的是一条实体记录,当出现多条时会报错 *

*/ @Test public void selectOne() { QueryWrapper queryWrapper = new QueryWrapper(); queryWrapper.eq("name", "lqf"); User user = mapper.selectOne(queryWrapper); System.out.println(user); } /** *

* 根据 Wrapper 条件,查询总记录数 *

* * @param queryWrapper 实体对象 */ @Test public void selectCount() { QueryWrapper queryWrapper = new QueryWrapper(); queryWrapper.eq("name", "lqf"); Integer count = mapper.selectCount(queryWrapper); System.out.println(count); } /** *

* 根据 entity 条件,查询全部记录 *

* * @param queryWrapper 实体对象封装操作类(可以为 null)为null查询全部 */ @Test public void selectList() { List list = mapper.selectList(null); System.out.println(list); } /** *

* 根据 Wrapper 条件,查询全部记录 *

* * @param queryWrapper 实体对象封装操作类(可以为 null) */ @Test public void selectMaps() { QueryWrapper queryWrapper = new QueryWrapper(); queryWrapper.isNotNull("name"); List maps = mapper.selectMaps(queryWrapper); for (Map map : maps) { System.out.println(map); } } /** * 打印结果 * {name=lqf, id=1046282328366391406, age=12, [email protected], status=false} * {name=lqf, id=1046282328366391407, age=12, [email protected], status=false} * {name=lqf, id=1046282328366391408, age=12, [email protected], status=false} * {name=lqf, id=1046282328366391409, age=12, [email protected], status=false} * {name=lqf, id=1046282328366391410, age=12, [email protected], status=false} * {name=lqf, id=1046282328366391411, age=12, [email protected], status=false} * {name=lqf, id=1046282328366391412, age=12, [email protected], status=false} * {name=lqf, id=1046282328366391413, age=12, [email protected], status=false} * {name=lqf, id=1046282328366391414, age=12, [email protected], status=false} * {name=lqf, id=1046282328366391415, age=12, [email protected], status=false} * {name=lqf, id=1046282328366391416, age=12, [email protected], status=false} * {name=lqf, id=1046282328366391417, age=12, [email protected], status=false} * {name=lqf, id=1046282328366391418, age=12, [email protected], status=false} * json类型的键值对模式 */ /** *

* 根据 entity 条件,查询全部记录(并翻页) *

* * @param page 分页查询条件(可以为 RowBounds.DEFAULT) * @param queryWrapper 实体对象封装操作类(可以为 null) */ @Test public void selectPage() { Page page = new Page(1, 5); QueryWrapper queryWrapper = new QueryWrapper(); IPage userIPage = mapper.selectPage(page, queryWrapper); System.out.println(userIPage); } /** * 打印结果 * ==> Preparing: SELECT COUNT(1) FROM user * ==> Parameters: * Parameters: *


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有