As mentioned above, the annotation Method for MyBatis multi-parameter transmission is described. Today, we will look at another method, that is, the default parameter naming method. For example, findTeacherByPage is used to query the instructor information by page.
1. Specific steps
For the method in the Mapper, MyBatis names the parameters of the method from left to right as param1, param2 ..., And so on. We can directly use these default names in SQL statements without using annotations.
First remove the @ Param annotation of TeacherMapper. java complete source code: http://down.51cto.com/data/539217 ):
Package com. abc. mapper;
Import com. abc. domain. Teacher;
Import org. springframework. stereotype. Component;
Import java. util. List;
// @ Component specify the er name as myTeacherMapper
// For more information, see the author's blog:
// Http://legend2011.blog.51cto.com/3018495/980150
@ Component ("myTeacherMapper ")
Publicinterface TeacherMapper {
Public Teacher getById (int id );
// Query the instructor information by PAGE
Public List <Teacher> findTeacherByPage (
String sort, // sorting Field
String dir, // sorting direction
Int start, // start record
Int limit // number of records
);
}
According to the above default naming method, the default names of MyBatis parameters for the findTeacherByPage method are as follows: sort is param1, dir is param2, start is param3, and limit is param4. Then, you can use these names in the SQL statement corresponding to this method in the teing file TeacherMapper. xml. The following 27 rows are shown:
<? Xmlversion = "1.0" encoding = "utf8"?>
<! DOCTYPE mapper PUBLIC "-// mybatis.org//DTD Mapper 3.0 // EN"
Http://mybatis.org/dtd/mybatis-3-mapper.dtd>
<! -- As before, the namespace value is the full name of the corresponding ER interface -->
<Mappernamespace = "com. abc. mapper. TeacherMapper">
<! -- Instructor entity ing -->
<ResultMapid = "supervisorResultMap" type = "Teacher">
<Idproperty = "id"/>
<Resultproperty = "name"/>
<Resultproperty = "gender"/>
<Resultproperty = "researchArea" column = "research_area"/>
<Resultproperty = "title"/>
<! -- The collection element maps the attributes of the instructor's guidance student set. Here we use
The namespace name. select statement id is used to reference
Select statement getStudents. This collection element uses nested
For details about the select statement, refer to the author's blog:
Http://legend2011.blog.51cto.com/3018495/985907
-->
<Collectionproperty = "supStudents" column = "id" ofType = "Student"
Select = "com. abc. mapper. StudentMapper. getStudents"/>
</ResultMap>
<! -- Param1, param2, and so on are the default names of the mappers method parameters of MyBatis -->
<Selectid = "findTeacherByPage" resultMap = "supervisorResultMap">
Select * from teacher
Order by ${param1 }$ {param2} limit # {param3}, # {param4}
</Select>
</Mapper>
Similar to the annotation method, the # {parameter name} method in the order by clause is invalid. you can experiment with this method on your own. Therefore, $ {parameter name} is still used here.
In addition to the above changes, the rest of the program is consistent with the previous blog. The running result is as follows:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1211251136-0.png "border =" 0 "/>
In the red box, the SQL statements generated by MyBatis and the values of start and limit are 0 and 2, respectively ). It can be seen that the parameter value name, asc referenced by the order by clause has been directly placed in the SQL statement, and the value of the start and limit parameters should be placed, but two "?" Placeholder.
There is also a numbering method, that is, numbering the method parameters from left to right from 0, and then referencing them in the SQL statement # {number. Similarly, in the order by clause, the related values cannot be referenced by $ {number. In this example, you can use the methods # {2} and # {3} to reference the start and limit values of the findTeacherByPage method. You can perform your own experiments.
Ii ,#{...} And $ {...} Differences
MyBatis official document http://code.google.com/p/mybatis/wiki/faq) the description of the two is :#{...} Is a parameter tag, and $ {...} It is just a simple string replacement. In general, to avoid SQL injection attacks, #{...} In this way, MyBatis will handle the issue of special character escaping. However, in some SQL statements, # {…} cannot be used #{...} Method. The example given in the above document is that the table name cannot be specified in this way, but in our experience, this method cannot be used in the order by clause. We can sum up the names of the tables or their fields, for example, table names, field names, and sorting fields after the order by clause, and SQL keywords such as the asc keyword after the order by clause) are not usable #{...} Only $ {…} with string replacement {...} Method.
MyBatis Study Notes series I: download and install ant
Prepare for MyBatis Study Notes Series II: ant getting started
MyBatis learning notes: MyBatis getting started
MyBatis Study Notes Series II: Example of adding, deleting, and modifying MyBatis
MyBatis Study Notes Series 3: association examples of MyBatis
MyBatis Study Notes Series 4: two forms of MyBatis association
MyBatis Study Notes Series 5: examples of integration between MyBatis and Spring
MyBatis Study Notes Series 6: examples of integration between MyBatis and Spring
MyBatis study notes 7: MyBatis one-to-multiple bidirectional Association
MyBatis Study Notes: MyBatis MapperScannerConfigurer Configuration
MyBatis Study Notes: two forms of MyBatis collection
MyBatis Study Notes Series 10: Log4j example of MyBatis logs
MyBatis Study Notes: example of how to annotate MyBatis with multiple parameters
MyBatis learning notes: Example of the default naming method for MyBatis multi-parameter transfer
MyBatis Study Notes: Example of Map mode for MyBatis multi-parameter transfer
MyBatis Study Notes: N + 1 in MyBatis
MyBatis Study Notes: a hybrid transfer of multiple parameters in MyBatis
MyBatis Study Notes:Spring declarative Transaction Management example
MyBatis Study Notes: Example of MyBatis multiple-to-multiple storage
This article is from the "Xiao fan's column" blog, please be sure to keep this source http://legend2011.blog.51cto.com/3018495/1024869