Get the App
SLTechnology News&Howtos  ›  Development  › 

How to generate sql statements automatically by Mybatis

Shulou Source: shulou.com Published: 2022-06-02 06:36:58 09月24日 Update

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.

Tags: Statements users queries tags files generation information interfaces methods automatic generation dynamics conditions content that is problems analysis testing differences difficulties specialties Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Technology vpn Docker MySQL