mybatisPlus apply using complex SQL statements such as concatenation tables
In MyBatis-Plus, the `apply()` method can be used to add any SQL fragment, including conjunction table queries. Therefore, you can use the `apply()` method to handle various types of conjunction table queries.
Benefits of using apply() method
It is possible to add native SQL fragments directly into the query conditions without being restricted by the regular query condition construction methods provided by MyBatis-Plus. This allows you to more flexibly build complex conjunction table query statements to meet specific query needs.
But it is important to note:
- Use of the `apply()` method requires careful handling.
- This can lead to security issues such as SQL injection because the `apply()` method allows native SQL snippets to be added.
- To avoid potential security risks, you need to make sure that the SQL fragments you add are trustworthy and are properly parameterized.
Additionally, using the `apply()` method can cause query performance to degrade, especially when processing large amounts of data. If possible, it is recommended to prioritize the use of the conventional query condition building method provided by MyBatis-Plus to take advantage of the framework's optimization capabilities.
In summary:
- Although the `apply()` method can be used to handle various conjunction table queries
- However, safety and performance issues need to be carefully considered when using
For example, I want to write three tables of left-leaf couplets, and finally sort them in reverse order of time.
Give an example
Suppose you have three tables: `user`, `order` and `product`, and their relationship is that the `user` table and `order` table are associated with `user_id` and the `order` table and `product` table are associated with `product_id`.
You want to do three tables of left-hand couplets and sort them in reverse order of time.
You can use the `apply()` method to add native SQL snippets to build this query.
QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper .select("user.*, order.*, product.*") // Select the required column.apply("LEFT JOIN `order` ON `user`.id = `order`.user_id") .apply("LEFT JOIN `product` ON `order`.product_id = `product`.id") .orderByDesc("`order`.create_time"); List<User> userList = (queryWrapper);
In the above example:
We created a QueryWrapper object and added two SQL fragments of left-bound operations using the apply() method.
- The select() method is used to select the columns to be returned. Here we select user.*, order.* and product.*, which returns all columns of the three tables.
- The orderByDesc() method is used to sort in reverse order by the `create_time` field of the `order` table.
Finally, we use the selectList() method to execute the query and store the result in the userList.
Please note:
- In actual use, you need to make corresponding adjustments based on the actual structure of the table and the field name.
- Also, make sure that table and field names are wrapped in backticks ` in SQL statements to avoid conflicts with MySQL keywords.
Hopefully this example helps you understand how to use the `apply()` method to perform left-link queries and sort them in reverse order in time.
If you want to perform GROUP BY operations in a conjunction table query and filter using the HAVING clause, you can use the `groupBy()` and `having()` methods of MyBatis-Plus to do so.
Here is an example:
Suppose you want to query the `user` table and the `order` table, group it according to `user_id`, and filter out users with a total order amount greater than 1,000:
QueryWrapper<Order> queryWrapper = new QueryWrapper<>(); queryWrapper .select(", , SUM() AS total_amount") .eq("", "completed") .groupBy("") .having("total_amount > 1000"); List<Map<String, Object>> resultList = (queryWrapper);
In the above example:
We created a QueryWrapper object and used the `select()` method to select the columns to be returned. Here we selected ``, `` and `SUM() AS total_amount`, which returns the user's ID, name and total order amount. The `eq()` method is used to add query conditions. Here we filter out orders with the status "completed" in the `order` table. The `groupBy()` method is used to specify grouping by ``. The `having()` method is used to add a HAVING clause, where we filter out users with a total order amount greater than 1000.
Finally, we execute the query using the selectMaps() method and store the result in `resultList`. The `selectMaps()` method returns a List<Map<String, Object>>, each Map corresponds to a record, where the key is the column name and the value is the corresponding value.
Please note that in actual use, you need to make corresponding adjustments based on the actual structure of the table and the field name.
Hopefully this example helps you understand how to perform GROUP BY operations in conjunction table queries and filter using HAVING clauses.
In MyBatis-Plus, `apply()` and `last()` are methods of the QueryWrapper class, but their functions and usage scenarios are different.
- The apply() method is used to add a custom SQL snippet to the query criteria. You can use the apply() method to add native SQL fragments to meet specific query needs, such as concatenated table queries, custom conditional expressions, etc.
- The last() method is used to add native SQL snippets at the end of the query statement. You can use the last() method to add native SQL snippets to add custom SQL statements at the end of the query statement, such as sorting, paging, etc.
Here is an example:
Shows the use of apply() and last() methods:
QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper .apply("LEFT JOIN `order` ON `user`.id = `order`.user_id") .last("ORDER BY `order`.create_time DESC"); List<User> userList = (queryWrapper);
In the above example:
- We used the apply() method to add a SQL fragment of the concatenated table operation, and left-join the user table and the order table.
- Then, using the last() method, a native SQL fragment was added at the end of the query statement, specifying that the order table's create_time field is sorted in descending order.
What should be noted is:
- Using the last() method requires caution, as it splices native SQL fragments directly to the end of the query statement, which can lead to security issues such as SQL injection.
- To avoid potential security risks, it is necessary to ensure that the added SQL fragments are trustworthy and process appropriately appropriately.
In summary:
- The apply() method is used to add custom SQL snippets to the query condition, while the last() method is used to add native SQL snippets at the end of the query statement.
- They are used for different scenarios and purposes respectively.
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.