MyBatis multi-parameter transfer-a hybrid method -- 15 of MyBatis Study Notes

Source: Internet
Author: User

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 ):

 
 
  1. // Query the instructor information by PAGE

  2. Public List <Teacher> findTeacherByPage (

  3. Map params, // query Conditions

  4. Int start, // start record

  5. Int limit // number of records

  6. );

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:

 
 
  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. // Query the instructors whose titles are professors or associate professors

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

  8. // Query the instructor's paging information

  9. List <Teacher> teachers =

  10. // The name field is sorted in ascending order and queried from 0th records.

  11. // Query 2 records

  12. Mapper. findTeacherByPage (params, 0, 2 );

Code 2)

The corresponding acing configuration TeacherMapper. xml) File Fragment is as follows:

 
 
  1. <selectid="findTeacherByPage"resultMap="supervisorResultMap">

  2. select * from teacher where teacher.title like  

  3. #{param1.title} order by ${param1.sort} ${param1.dir} limit #{param2},#{param3}  

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

Related Article

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.