The previous article introduced the methods of MyBatis multi-parameter passing annotation and default parameter naming. Today we will introduce the Map method. Still previous paging Method for querying instructor information findTeacherByPage example source code: http://down.51cto.com/data/546809 ).
First, modify the findTeacherByPage method in the ER interface TeacherMapper as follows:
// Query the instructor information by PAGE
Public List <Teacher> findTeacherByPage (Map <String, Object> map );
Correspondingly, the Map interface should be introduced here: import java. util. Map.
In the execution class CollectionDemo, the code for calling the findTeacherByPage method is as follows:
Map <String, Object> params =
New HashMap <String, Object> ();
// Sort by name in ascending order,
Params. put ("sort", "name ");
Params. put ("dir", "asc ");
// The query results start from 0th and query 2 records
Params. put ("start", 0 );
Params. put ("limit", 2 );
// Query the instructors whose titles are professors or associate professors
Params. put ("title", "% professor ");
// Query the instructor information by PAGE
List <Teacher> teachers =
Mapper. findTeacherByPage (params );
We can see that we put the parameter in a Map first, so that we can use # {…} in the corresponding SQL statement #{...} These parameters are referenced. As follows:
<selectid="findTeacherByPage"resultMap="supervisorResultMap"
parameterType="java.util.Map">
select * from teacher where title like #{title}
order by ${sort} ${dir} limit #{start},#{limit}
</select>
As before, $ {…} should be used in the order by clause {...} . In fact, parameterType = "java. util. Map" here can be avoided.
The running result is as follows:
650) this. width = 650; "src =" http://img1.51cto.com/attachment/201210/170222296.png "border =" 0 "/>
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/1030804