In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Sql
This element can be used to define reusable SQL code snippets that can be included in other statements. It can be parameterized statically (in loading parameters). Different property values change through the included instance. For example:
${alias} .id, ${alias} .username, ${alias} .password
This SQL fragment can be included in other statements, such as:
Select
From some_table t1
Cross join some_table t2
Attribute values can be used for included refid attributes or for attribute values in contained words, such as:
${prefix} Table
From
Select
Field1, field2, field3
Parameter (Parameters)
What you see in all the previous statements are examples of simple parameters, which are actually very powerful elements of MyBatis. For simple practices, about 90% of the cases have very few parameters, such as:
Select id, username, password
From users
Where id = # {id}
The above example illustrates a very simple named parameter mapping. The parameter type is set to int so that this parameter can be set to anything. Native types or simple data types (such as integers and strings) are replaced entirely with parameter values because there are no related attributes. However, if you pass in a complex object, the behavior will be a little different. For example:
Insert into users (id, username, password)
Values (# {id}, # {username}, # {password})
If a parameter object of type User is passed to the statement, the id, username, and password properties are looked up and their values are passed into the parameters of the preprocessed statement.
This is good and simple for passing parameters to statements, but the function of parameter mapping is much more than that.
First, like other parts of MyBatis, parameters can specify a special data type.
# {property,javaType=int,jdbcType=NUMERIC}
Like the rest of the MyBatis, the javaType can usually be determined from the parameter object, as long as the object is not a HashMap. Then the javaType should be determined to ensure that the correct type of processor is used.
If null is passed as a value, JDBC Type is required for all columns that may be empty. You can study this situation yourself by reading the JavaDocs documentation of the setNull () method of the preprocessing statement.
To customize type handling later, you can also specify a special type handler class (or alias), such as:
# {age,javaType=int,jdbcType=NUMERIC,typeHandler=MyTypeHandler}
Although configuration seems to be becoming more and more cumbersome, it is actually rare to set them.
For numeric types, there is also a setting of decimal places to determine the number of digits retained after the decimal point.
# {height,javaType=double,jdbcType=NUMERIC,numericScale=2}
Finally, the mode attribute allows you to specify IN,OUT or INOUT parameters. If the parameter is OUT or INOUT, the real value of the parameter object property will be changed, as you would expect when getting the output parameter. If mode is OUT (or INOUT) and jdbcType is CURSOR (that is, REFCURSOR of Oracle), you must specify a resultMap to map the result set to the parameter type. Note that the javaType property here is optional, and if the space on the left is the CURSOR type of jdbcType, it is automatically set to the result set.
# {department, mode=OUT, jdbcType=CURSOR, javaType=ResultSet, resultMap=departmentResultMap}
MyBatis also supports many advanced data types, such as structs, but you must tell it the statement type name when registering the out parameter. For example (again, in practice, you can't break lines like this):
# {middleInitial, mode=OUT, jdbcType=STRUCT, jdbcTypeName=MY_TYPE, resultMap=departmentResultMap}
Although most of these powerful options you simply specify property names, MyBatis will infer other things on its own, and at most you need to specify jdbcType for column names that may be empty.
# {firstName}
# {middleInitial,jdbcType=VARCHAR}
# {lastName}
String substitution
By default, using syntax in the format # {} causes MyBatis to create preprocessing statement properties and safely set values (such as?). This is safer, faster, and usually preferred, but sometimes you just want to insert an immutable string directly into the SQL statement. For example, like ORDER BY, you can use it like this:
ORDER BY ${columnName}
Here MyBatis does not modify or escape strings.
It is not safe to accept the output from the user and provide the immutable string in the statement in this way, resulting in a potential SQL injection *, so either the user is not allowed to enter these fields, or they are escaped and checked.
Follow the Wechat account: IT (it_) and learn more.
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.