MyBatis-plus 通过 wrapper.and方法给sql添加额外的查询条件
MyBatis-plus方法:
public Children and(boolean condition, Consumer<Children> consumer) {
return this.and(condition).addNestedCondition(condition, consumer);
}
一、需求:如果用户穿插了查询参数,就给sql附加条件(当然,这个条件可以是复杂的条件),否则查询不用带上条件。
示例:
@org.junit.Test
public void test() {
String name = "狗哥";
QueryWrapper<Animal> wrapper = new QueryWrapper<>();
wrapper.and(StringUtils.isNotBlank(name),item->item.apply(
"FIND_IN_SET({0},name)",name));
List<Animal> animalList = animalMapper.selectList(wrapper);
log.info("总记录:{}",animalList.size());
animalList.forEach(System.out::println);
}
一、需求:如果用户穿插了查询参数,就给sql附加条件(当然,这个条件可以是复杂的条件),否则查询其他的条件。
default Children and(Consumer<Param> consumer) {
return this.and(true, consumer);
}
@org.junit.Test
public void test() {
String name = "";
QueryWrapper<Animal> wrapper = new QueryWrapper<>();
wrapper.and(item->item.apply(
"FIND_IN_SET({0},name)",name).or().eq("name","猫哥"));
List<Animal> animalList = animalMapper.selectList(wrapper);
log.info("总记录:{}",animalList.size());
animalList.forEach(System.out::println);
}
wrapper.apply("(receipt_no Like '%"+incomeCollectionReceipt.getSearchValue()+"%' or customer Like '%"+incomeCollectionReceipt.getSearchValue()+"%' or matter Like '%"+incomeCollectionReceipt.getSearchValue()+"%' or bill_unit Like '%"+incomeCollectionReceipt.getSearchValue()+"%' or receipt_money Like '%"+incomeCollectionReceipt.getSearchValue()+"%' or item_name Like '%"+incomeCollectionReceipt.getSearchValue()+"%')");