MyBatis-Plus
is a very popular MyBatis enhancement tool that provides many ways to simplify queries and operations.apply
is one of the most useful methods, which allows you to add native SQL snippets directly into the query criteria. This feature is usually used in scenarios where complex queries are required.
Apply method overview
apply
Methods are mainly used inQueryWrapper
orLambdaQueryWrapper
, custom add a native SQL fragment to the query condition and can be parameterized through placeholders.
Sample code
Below are some common usage examples, showingapply
Various application scenarios of the method:
Basic usage
Basic usage: Add directlySQL
Fragment, without parameterized placeholders.
// Assume this variable is your Mapper objectUserMapper userMapper = ...; QueryWrapper<User> queryWrapper = new QueryWrapper<>(); ("date(created_at) = '2023-10-01'"); List<User> users = (queryWrapper);
With parameterized placeholders: Pass parameters using placeholders to prevent SQL injection.
QueryWrapper<User> queryWrapper = new QueryWrapper<>(); String dateParam = "2023-10-01"; ("date(created_at) = {0}", dateParam); List<User> users = (queryWrapper);
Combined with other conditions: Can be used in combination with other query conditions.
QueryWrapper<User> queryWrapper = new QueryWrapper<>(); String startDate = "2023-10-01"; String endDate = "2023-10-31"; queryWrapper .ge("age", 18) // Age is greater than or equal to 18 .apply("date(created_at) BETWEEN {0} AND {1}", startDate, endDate); List<User> users = (queryWrapper);
LambdaQueryWrapper Usage
Lambda expressions:useLambdaQueryWrapper
can provide a more type-safe way to pass fields.
LambdaQueryWrapper<User> lambdaQuery = new LambdaQueryWrapper<>(); ("date(created_at) = {0}", dateParam); List<User> users = (lambdaQuery);
Entity class and Mapper interface in the example
Suppose there is an entity classUser
and the corresponding Mapper interfaceUserMapper
:
User Entity Class
public class User { private Long id; private String name; private Integer age; private Date createdAt; // Getters and Setters }
UserMapper Interface
import ; public interface UserMapper extends BaseMapper<User> { }
Complex query example
In practical applications,apply
Methods can be used to build more complex query conditions, such as multi-table joint query, custom complex filter conditions, etc. The following example shows how to use it in multi-table joint searchapply
:
Multi-table joint search example
Suppose we have another tableOrder
, you need to query the user and his latest order information:
public class Order { private Long id; private Long userId; private Date orderDate; private BigDecimal amount; // Getters and Setters }
import ; public interface OrderMapper extends BaseMapper<Order> { }
Complex query example
import ; import ; import ; public class UserService { private final UserMapper userMapper; private final OrderMapper orderMapper; public UserService(UserMapper userMapper, OrderMapper orderMapper) { = userMapper; = orderMapper; } public List<User> getUsersWithRecentOrders() { QueryWrapper<User> queryWrapper = (); ("EXISTS (SELECT 1 FROM orders o WHERE o.user_id = AND o.order_date = " + "(SELECT MAX(order_date) FROM orders WHERE user_id = o.user_id))"); return (queryWrapper); } }
Summarize
From the above example, we can see thatapply
The method isMyBatis-Plus
Very powerful, providing the ability to directly insert native SQL fragments, which makes developers more flexible when building complex query conditions. At the same time, using parameterized queries can effectively prevent SQL injection problems and ensure the security of queries. In actual development, according to specific needs, it can be flexibly used in combination with other conditional construction methods.apply
Methods, construct various complex queries.
This is the end of this article about the application usage of MyBatis-Plus. For more related content on MyBatis-Plus, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!