mybatis plus or和and嵌套
时间: 2023-11-02 18:55:50 浏览: 123
MyBatis Plus provides support for both "OR" and "AND" nested conditions in query statements.
To use the "OR" nested condition, you can use the `or()` method provided by the `QueryWrapper` class. Here's an example:
```java
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("age", 20)
.or()
.eq("age", 30)
.like("name", "John");
List<User> userList = userMapper.selectList(queryWrapper);
```
In this example, the generated SQL query will have a condition for `age = 20` OR `age = 30` AND `name` LIKE '%John%'.
To use the "AND" nested condition, you don't need any special method. You can simply chain multiple conditions using the `eq()`, `like()`, etc. methods provided by the `QueryWrapper` class. Here's an example:
```java
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("age", 20)
.like("name", "John")
.ge("salary", 5000);
List<User> userList = userMapper.selectList(queryWrapper);
```
In this example, the generated SQL query will have conditions for `age = 20` AND `name` LIKE '%John%' AND `salary` >= 5000.
You can mix "OR" and "AND" nested conditions as per your requirements to construct more complex queries in MyBatis Plus.
阅读全文