In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the usage and difference of mybatis placeholder # {} and ${}. The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn the usage and difference of mybatis placeholder # {} and ${}.
Placeholders # {} and ${} of mybatis. The # {} placeholder is used to set parameters. There can be three types of parameters: basic type, custom type, and Map.
Basic types:
Basic type, the parameter name is independent of the name in the placeholder.
# {} when entering a value, sql parses the parameter in quotation marks
If @ Param ("xxx") is used, mybatis automatically generates map as the input parameter, and the parameter name must be consistent with the placeholder
Select * from t_role where id = # {xxxid}
Test:
@ Test public void testSelectOne () {Role role = (Role) session.selectOne ("cn.wh.mapper.RoleMapper.findById", 1); System.out.println (role.getName ());}
Custom type
The custom type is used as a parameter, and the custom class needs to provide a get method for the attribute. If no get method is provided, the value will be obtained by reflection based on the name in the placeholder. If the name in the placeholder is inconsistent with the attribute, report ReflectionException.
Select * from t_role limit # {index}, # {size}
Test:
@ Test public void testPage1 () {PageUtil pu = new PageUtil (); pu.setIndex (3); pu.setSize (3); List list = session.selectList ("cn.wh.mapper.RoleMapper.findListBypage", pu); for (Role r:list) {System.out.println (r.getName ());}}
Map
Map as the parameter type, key is the same as the name in the placeholder. If the name is not consistent, then null will be passed to the placeholder.
Note: the # {} placeholder does not solve three types of problems:
Table names are dynamic: Select * from # {table_name}
Column names are dynamic: Select # {column_name} from t_role
Sorting is dynamic: Select * from t_role order by # {columu}
two。 The ${} placeholder is a string connector that can be used to dynamically set column names and sort names.
Enter the value of ${}. When parsed by sql, the parameter is not in quotation marks.
Therefore, the ${} parameter cannot be a basic data type, only a custom type and map.
Select * from ${tableName}
Test:
@ Test public void testSelectList () {Map map = new HashMap (); map.put ("tableName", "t_role"); List list = session.selectList ("cn.wh.mapper.RoleMapper.findAll", map); for (Role role:list) {System.out.println (role.getId () + "- -" + role.getName ());}}
Used as a connector:
Select * from t_role where name like'${name}%'
Test:
@ Test public void testLike2 () {Map map = new HashMap (); map.put ("name", "yellow"); List list = session.selectList ("cn.wh.mapper.RoleMapper.selectLike1", map); for (Role r:list) {System.out.println (r.getName ());}} three. # {} and ${}
Both $and # in mybatis are dynamic input parameters in sql.
Eg:select id,name,age from student where name=# {name} this name is dynamic and variable. When you pass in what kind of value, the sql statement will be executed according to the value you passed in.
# {}: parses to a parameter marker of a JDBC precompiled statement (prepared statement), and a # {} is resolved to a parameter placeholder.
${}: just for a pure broken string replacement, variable substitution will be done during the dynamic SQL parsing phase.
Example:
Eg 1: select id,name,age from student where name=# {name}; parsed to: select id,name,age from student where name='cy'
Eg 2: select id,name,age from student where name=$ {name}; parsed to: select id,name,age from student where name=cy
# takes the passed-in value as a string, eg:select id,name,age from student where id = # {id}. When the current end passes the id value 1 to the background, it is equivalent to select id,name,age from student where id ='1.
$is to directly display the incoming data to generate a sql statement, eg:select id,name,age from student where id = ${id}. When the current side passes the id value of 1 to the background, it is equivalent to select id,name,age from student where id = 1.
Using # can largely prevent sql injection. (splicing of sentences)
But if you use it in order by, you need to use $.
# is still used frequently in most cases, but must be used in different cases.
I think the biggest difference between # and # is that when entering a value, sql parses the parameter in quotation marks, and the biggest difference lies in: when entering a value, sql parses, the parameter is in quotation marks, while {} enters the value, and when sql parses, the parameter is without quotation marks.
Thank you for your reading, the above is the content of "the usage and difference of mybatis placeholder # {} and ${}". After the study of this article, I believe you have a deeper understanding of the usage and difference of mybatis placeholder # {} and ${}, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.