MybatisPlus--3.3 动态条件查询
程序员文章站
2024-03-06 20:54:32
...
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestCondition {
@Autowired
private EmployeeMapper employeeMapper;
/**
* 情景,多条件查询中,条件动态选择
*/
@Test
public void test1() {
String lastName = "J";
String email = "";
condition(lastName, email);
}
public void condition(String lastName, String email) {
QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
// if (StringUtils.isNotEmpty(lastName)) {
// queryWrapper.like("last_name", lastName);
// }
//
// if (StringUtils.isNotEmpty(email)) {
// queryWrapper.like("email", email);
// }
// 第一个参数 condition 用来做判断,指定该条件是否被添加到 sql 语句中
queryWrapper.like(StringUtils.isNotEmpty(lastName), "last_name", lastName)
.like(StringUtils.isNotEmpty(email), "email", email);
employeeMapper.selectList(queryWrapper)
.forEach(System.out::println);
}
}