使用add()方法
官方给出示例:
and(i -> i.eq("name", "李白").ne("status", "活着"))
---> sql输出结果:and (name = '李白' and status <> '活着')
QueryWrapper无条件时
QueryWrapper<Demo> queryWrapper = new QueryWrapper<>(); queryWrapper.and(query -> query.like(StringUtils.isNotBlank(demo.getName()), "demo.name", demo.getName()).or(). like(StringUtils.isNotBlank(demo.getFrontId()), "demo.frontId", demo.getFrontId()));
------> sql输出结果:
select id, name, frontId from demo where ( turbine.name LIKE '%测%' OR turbine.frontId LIKE '%测%')
QueryWrapper有条件时
QueryWrapper<Demo> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("id",demo.getId); queryWrapper.and(query -> query.like(StringUtils.isNotBlank(demo.getName()), "demo.name", demo.getName()).or(). like(StringUtils.isNotBlank(demo.getFrontId()), "demo.frontId", demo.getFrontId()));
------> sql输出结果:
select id, name, frontId from demo where id = 1 and ( turbine.name LIKE '%测%' OR turbine.frontId LIKE '%测%')
理解MyBatis Plus的add()方法
如果QueryWrapper对象没有查询条件,则只会在where语句中添加 ()
如果QueryWrapper对象中有查询条件了,会在where语句中自动添加 and(查询条件)