洛阳市河阳建设工程有限公司网站,咨询类网站建设方案书,上海论坛社区,天气模块MybatisPlus------条件构造器Wapper#xff08;七#xff09; Wrapper:条件构造器抽象类#xff0c;最顶端父类 AbstarctWrapper#xff1a;用于查询条件封装#xff0c;生成sql的where条件。 QueryWrapper#xff1a;查询条件封装#xff08;可以用于查询、删除#x…MybatisPlus------条件构造器Wapper七 Wrapper:条件构造器抽象类最顶端父类 AbstarctWrapper用于查询条件封装生成sql的where条件。 QueryWrapper查询条件封装可以用于查询、删除更新 UpdateWrapperUpdate条件封装 AbstractLambdaWrapper使用Lambda语法 LambdaUpdateWrapper用于Lambda语法使用的查询Wrapper LambdaQueryWrapper用于Lambda语法使用的更新Wrapper
使用QueryWrapper进行更新 Testpublic void testUpdate(){// 将(年龄大于20并且用户名中包含有a)或邮箱为null的信息修改//where条件QueryWrapperBaseProcedure baseProcedureQueryWrapper new QueryWrapper();baseProcedureQueryWrapper.gt(age,20).like(procedure_name,a).or().isNull(eamil);//更新值BaseProcedure baseProcedure new BaseProcedure();baseProcedure.setProcedureName(asdasd);int update baseProcedureMapper.update(baseProcedure, baseProcedureQueryWrapper);System.out.println(updateupdate);}涉及到条件优先级怎么处理
涉及到where子句后判断的优先级时lambda中的条件优先执行 例如 将用户名中包含有a并且(年龄大于20或邮箱为null)的信息修改
Testpublic void testUpdate2(){// 将用户名中包含有a并且(年龄大于20或邮箱为null)的信息修改//where条件//涉及到lambda表达式,lambda中的条件优先执行QueryWrapperBaseProcedure baseProcedureQueryWrapper new QueryWrapper();baseProcedureQueryWrapper.like(procedure_name,a).and(i -{i.gt(age,20).or().isNull(eamil);});//更新值BaseProcedure baseProcedure new BaseProcedure();baseProcedure.setProcedureName(asdasd);int update baseProcedureMapper.update(baseProcedure, baseProcedureQueryWrapper);System.out.println(updateupdate);}and()和or()中的Consumer怎么理解
and()方法源码
/*** 查询条件封装* p嵌套/p* li泛型 Param 是具体需要运行函数的类(也是 wrapper 的子类)/li** author hubin miemie HCL* since 2017-05-26*/
public interface NestedParam, Children extends Serializable {/*** ignore*/default Children and(ConsumerParam consumer) {return and(true, consumer);}Param是泛型是具体需要运行函数的类也就是 wrapper 的子类就是条件构造器因此可以将条件构造器写入and方法中。 而Consumer是Lambda表达式中的消费者接口消费者接口中一定是由参数的方法中的内容就是我们对参数的操作方式 仅需要查询部分字段如何写
通过调用QueryWrapper的select()方法在该方法里传入需要查询的字段即可。 Testpublic void testSelect(){//查询用户用户名,和用户编码QueryWrapperBaseProcedure baseProcedureQueryWrapper new QueryWrapper();baseProcedureQueryWrapper.select(procedure_code,procedure_name);ListMapString, Object maps baseProcedureMapper.selectMaps(baseProcedureQueryWrapper);maps.forEach(System.out::println);}子查询如何操作
通过inSql方法传入字段以及子查询sql即可。 案例比较简单仅仅用来测试无实际意义 Testpublic void testSelect02(){//子查询//SELECT * from base_procedure//WHERE id (SELECT id from base_procedure WHERE id ef5397a26b7a469c6fe1de0e)QueryWrapperBaseProcedure baseProcedureQueryWrapper new QueryWrapper();baseProcedureQueryWrapper.inSql(id,SELECT id from base_procedure WHERE id ef5397a26b7a469c6fe1de0e);ListMapString, Object maps baseProcedureMapper.selectMaps(baseProcedureQueryWrapper);maps.forEach(System.out::println);}