In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how Mybatis automatically generates sql sentences. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Mybatis automatically generates sql statements
Create a maven project and run the configuration file to generate sql statements
Dynamic sql statement of Mybatis
The main problem solved by the dynamic sql statement of Mybatis is the splicing of different conditional sql statements.
For example: according to the user information, query the user list, when do not know what information is based on the user, it is difficult to write the query SQL statement, and the dynamic SQL statement mainly solves this kind of problem.
The use of if tags
Define methods in the persistence layer interface
/ * * query the user list * @ param user * @ return * / List findByUser (User user) based on user information
Write the mapping file corresponding to the persistence layer interface
Select * from user where 1 = 1 and id = # {id} and username like # {username} and birthday = # {birthday} and sex = # {sex} and address = # {address}
Write test methods
/ * query user list based on user information * / @ Test public void testFindByUser () {User user = new User (); user.setUsername ("% King%"); List users = userDao.findByUser (user); for (User u: users) {System.out.println (u);}} use of where tags
To simplify the conditional splicing of where 1 / 1 above, we can use tags to simplify development, so modify the persistence layer mapping file
Select * from user and id = # {id} and username like # {username} and birthday = # {birthday} and sex = # {sex} Use of the and address = # {address} foreach tag
Froeach is a traversal of a collection, usually applied when building in conditional statements
For example: query a user based on a user id collection.
Encapsulate the id collection and add it to the List collection
Write persistent layer interface methods
/ * * query the user * @ param queryUR * @ return * / List findInIds (QueryUR queryUR) according to the id collection
Write a persistent layer interface mapping file
Select * from user # {uid}
Write test methods
/ * query users based on id collection * / @ Test public void testFindInIds () {QueryUR queryUR = new QueryUR (); List ids = new ArrayList (); ids.add (41); ids.add (43); ids.add (45); queryUR.setIds (ids); List users = userDao.findInIds (queryUR) Simplified programming of for (User user: users) {System.out.println (user);}} sql statement
In the mapping file, repeated sql statements can be extracted through sql tags and referenced by include tags, which has achieved the effect of sql reuse. As follows:
Select * from user where id= # {userID}; this is how the Mybatis shared by the editor automatically generates sql statements. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.