mybatispuls条件查询
时间: 2024-01-29 19:03:26 浏览: 126
MyBatis Plus provides various methods to perform conditional queries. Here's an example of how to perform a conditional query using MyBatis Plus:
```java
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.QueryChainWrapper;
public class MyBatisPlusExample {
public static void main(String[] args) {
// Select query with conditions
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("age", 25)
.like("name", "John")
.orderByDesc("salary")
.last("LIMIT 10");
// Select query using lambda expressions
LambdaQueryWrapper<User> lambdaQueryWrapper = Wrappers.lambdaQuery();
lambdaQueryWrapper.eq(User::getAge, 25)
.like(User::getName, "John")
.orderByDesc(User::getSalary)
.last("LIMIT 10");
// Select query chain
QueryChainWrapper<User> queryChainWrapper = new QueryChainWrapper<>(mybatisPlusMapper);
queryChainWrapper.eq("age", 25)
.like("name", "John")
.orderByDesc("salary")
.last("LIMIT 10");
// Execute the query
List<User> userList = mybatisPlusMapper.selectList(queryWrapper);
}
}
```
In the above example, we demonstrated three ways to perform a conditional query using MyBatis Plus. The `queryWrapper` allows you to build the query conditions using chained methods. The `lambdaQueryWrapper` uses lambda expressions to define the conditions. The `queryChainWrapper` provides a query chain style method to build the conditions.
You can customize the query conditions based on your requirements by using different methods provided by MyBatis Plus, such as `eq`, `ne`, `like`, `gt`, `lt`, `orderBy`, etc.
阅读全文