Example of Map mode for MyBatis multi-parameter transfer -- thirteen MyBatis Study Notes

Source: Internet
Author: User

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:
 
 
  1. // Query the instructor information by PAGE

  2. 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:
 
 
  1. Map <String, Object> params =

  2. New HashMap <String, Object> ();

  3. // Sort by name in ascending order,

  4. Params. put ("sort", "name ");

  5. Params. put ("dir", "asc ");

  6. // The query results start from 0th and query 2 records

  7. Params. put ("start", 0 );

  8. Params. put ("limit", 2 );

  9. // Query the instructors whose titles are professors or associate professors

  10. Params. put ("title", "% professor ");

  11. // Query the instructor information by PAGE

  12. List <Teacher> teachers =

  13. 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:
 
 
  1. <selectid="findTeacherByPage"resultMap="supervisorResultMap"

  2. parameterType="java.util.Map">

  3.               select * from teacher where title like #{title}           

  4.               order by ${sort} ${dir} limit #{start},#{limit}  

  5. </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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.