Home> Common Problem> body text

What is mybatis dynamic sql

anonymity
Release: 2020-09-16 15:27:32
Original
12419 people have browsed it

MyBatis' dynamic SQL refers to the flexible operation of SQL statements; it is based on OGNL expressions and flexibly splices SQL statements through if, choose, when, otherwise, trim, where, set, and foreach tags , assembly, thus improving developer efficiency.

What is mybatis dynamic sql

What is dynamic SQL? What is the role of dynamic SQL?

mybatis core Perform flexible operations on SQL statements, judge through expressions, and flexibly splice and assemble SQL.

With the traditional method of using JDBC, I believe that when you combine complex SQL statements, you need to splice them together. If you don't pay attention, even missing a space will lead to errors. The dynamic SQL function of Mybatis is designed to solve this problem. It can be combined into very flexible SQL statements through if, choose, when, otherwise, trim, where, set, and foreach tags, thereby improving the efficiency of developers

MyBatis uses OGNL to use dynamic SQL.

Currently, dynamic SQL supports the following tags

What is mybatis dynamic sql

Example: if statement uses

according to username and sex to query the data. If username is empty, then it will be queried only based on sex; otherwise, it will be queried only based on username

First do not use dynamic SQL to write the above query statement

Copy after login

, We can find that if #{username} is empty, then the query result is also empty. How to solve this problem?

Use if to determine

Copy after login

Writing like this, we can see that if sex is equal to null, then the query statement isselect * from user where username=# {username}.

The above is the detailed content of What is mybatis dynamic sql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!