search
Homephp教程PHP开发Summary of the difference between #{} and ${} in passing parameters in Mybatis and the difference between # and $

I have been using mybatis recently, and I have used ibatis before. Generally speaking, it is similar, but I still encountered a lot of problems. I will record them again.

For example, use #{} and ${} to pass parameters. The difference,

use # to pass in parameters, the sql statement parsing will add "", for example, select * from table where name = #{name}, the name passed in is Xiao Li, then it will be printed out at the end What is

select * from table where name = 'Xiao Li' is that it will be parsed as a string. The advantage compared to $ is more obvious, right? #{} passing parameters can prevent sql Injection, if the parameters you pass in are single quotes', then if you use ${}, this method will report an error.

Another scenario is if you want to do dynamic sorting, such as order by column, be sure to use ${} at this time, because if you use #{}, what will be printed will be

select * from table order by 'name', which is useless.

Currently, if you can use #, don’t use $.

The difference between # and $ in mybatis

1. #Treat the incoming data as one character string, a double quotation mark will be added to the automatically passed in data. For example: order by #user_id#, if the value passed in is 111, then the value when parsed into sql is order by "111". If the value passed in is id, the parsed sql is order by "id".

2. $ Display the incoming data directly and generate it in sql. For example: order by $user_id$, if the value passed in is 111, then the value when parsed into sql is order by user_id. If the value passed in is id, the parsed sql is order by id.

3. #Method can prevent sql injection to a great extent.

4.$ method cannot prevent Sql injection.

5. The $ method is generally used to pass in database objects, such as table names.

6. If you can generally use #, don’t use $.

MyBatis needs to pay attention when using order by dynamic parameters when sorting. Use $ instead of

#string replacement

By default, using the #{} format syntax will cause MyBatis to create preprocessing statement attribute and set a safe value (such as ?) against it. This is safe, fast and preferred, sometimes you just want to insert an unchanged string directly into the SQL statement. For example, like ORDER BY, you can use it like this:

ORDER BY ${columnName}

Here MyBatis will not modify or escape the string.

Important: It is not safe to accept output from the user and provide it with an immutable string in the statement. This leads to potential SQL injection attacks, so you should not allow users to enter these fields, or generally escape and check them yourself.

A brief summary of the difference between $ and # in Mybatis

Not long ago, someone came to our company for an interview, and our manager asked this question. I also had only a partial understanding of it, so I went to Baidu to do some research. .

In fact, the difference is very simple. You will understand it with an example. Write a SQL sentence - for example: select * from user_role where user_code = "100";

For this sentence, you need to write select * from ${tableName} where user_code = #{userCode}

So, the $ symbol is directly spelled into sql, and the # symbol will be spliced ​​with sql in the form of a string.


For more related articles on the difference between #{} and ${} passing parameters in Mybatis and the summary of the difference between # and $, please pay attention to the PHP Chinese website!


Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.