In this series of articles "Examples of Map mode for MyBatis multi-parameter transfer", mashiguang asked how to pass parameters in the following method: public List findStudents (Map conditions, int page, int pageSize ). This is a hybrid form, that is, there are both Map-type parameters and common-type parameters similar to int. After some exploration, the author also found a relatively smooth solution to this situation.
In fact, it is also very simple. In the default naming method "MyBatis multi-parameter transfer" default naming method example), this article introduces the default naming of MyBatis parameters, which is still valid in this case. What we need to do is read the parameter values in Map according to this name. This method is used for instructor paging query. First modify the ER interface TeacherMapper. java) in the instructor paging query method declaration complete source code download: http://down.51cto.com/data/742758 ):
// Query the instructor information by PAGE
Public List <Teacher> findTeacherByPage (
Map params, // query Conditions
Int start, // start record
Int limit // number of records
);
Code 1)
MyBatis names the three parameters of this method as param1, param2, and param3 in sequence. The first parameter is of the Map type, and the last two parameters are of the int type.
The query code snippets in the execution class CollectionDemo. java are as follows:
Map <String, Object> params =
New HashMap <String, Object> ();
// Sort by name in ascending order,
Params. put ("sort", "name ");
Params. put ("dir", "asc ");
// Query the instructors whose titles are professors or associate professors
Params. put ("title", "% professor ");
// Query the instructor's paging information
List <Teacher> teachers =
// The name field is sorted in ascending order and queried from 0th records.
// Query 2 records
Mapper. findTeacherByPage (params, 0, 2 );
Code 2)
The corresponding acing configuration TeacherMapper. xml) File Fragment is as follows:
<selectid="findTeacherByPage"resultMap="supervisorResultMap">
select * from teacher where teacher.title like
#{param1.title} order by ${param1.sort} ${param1.dir} limit #{param2},#{param3}
</select>
Code 3)
In the above ing file, the value of the title attribute in Map can be accessed in the form of # {param1.title. Of course, the order by clause should be in the form of $ {param1.sort}. For details, see the article "MyBatis multi-parameter transfer annotation method example" in this series of blog posts, part 2: "possible errors" The first one is about order. However, I have verified that it is also possible to use "#" in this example ). As a result, we can conclude that we can access the attribute values of the Map parameter in the ing file by using the # {default parameter name. attribute name} format.
The running result is as follows:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/11415132X-0.png "border =" 0 "/>
Obviously, if you use the annotation method and specify a name for the parameter to pass multiple parameters in the annotation method, please refer to the blog post in this series: Example of the annotation Method for MyBatis multi-parameter transmission ), you can also use the above form to access the attribute value in Map, here will not be repeated with the annotation, access the source code of the attribute in Map, please download here: http://down.51cto.com/data/742780 ).
Conclusion: One of the learning methods proposed by the author is "good at conjecture" in the article "views on whether or not to attend a JAVA training course ". The author did not find any reference materials to solve this problem, but relied on his own conjecture and programming verification. This shows that conjecture works.
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/1173787