In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to use MyBatis to easily achieve recursive query and stored procedure calls, I believe that many inexperienced people do not know what to do. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Recursive call
Because the level of the department is not controllable, if I want to get the complete json of all departments, I have to use recursive calls and use Java code to deal with recursive low. It just so happens that the collection in the ResultMap of MyBatis can easily solve this problem. The core code is as follows:
Select d1.*from department D1 where d1.`parentId` = # {pid} AND d1.enabled=true
Each Department has a children property, and the return result of the getDepByPid method is a collection in BaseResultMap,BaseResultMap and the getDepByPid method will be called, so we can quickly implement a recursive call. You only need to define the following methods in Mapper:
List getDepByPid (Long pid)
The query results are as follows (part):
[{"id": 1, "name": "shareholders' meeting", "parentId":-1, "enabled": true, "children": [{"id": 4, "name": "Chairman", "parentId": 1, "enabled": true "children": [{"id": 5, "name": "General Manager", "parentId": 4, "enabled": true "children": [{"id": 8, "name": "Finance Department", "parentId": 5, "enabled": true "children": [], "parent": false}], "parent": true}], "parent": true}] "parent": true}] stored procedure call
It is relatively simple to call a stored procedure. Take adding a department as an example, as follows:
Add the following methods to 1.Mapper:
Void addDep (@ Param ("dep") Department department)
It is written as follows in 2.xml:
Call addDep (# {dep.name,mode=IN,jdbcType=VARCHAR}, # {dep.parentId,mode=IN,jdbcType=INTEGER}, # {dep.enabled,mode=IN,jdbcType=BOOLEAN}, # {dep.result,mode=OUT,jdbcType=INTEGER}, # {dep.id,mode=OUT,jdbcType=BIGINT})
Note that the statementType call indicates that this is a stored procedure, mode=IN indicates that this is the input parameter, and mode=OUT indicates that this is the output parameter. After a successful call, get the id and result fields of department in service, and you can get the corresponding call result.
After reading the above, have you mastered how to easily implement recursive queries and stored procedure calls using MyBatis? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.