MyBatis multi-parameter transfer-default naming example-MyBatis study note 12

Source: Internet
Author: User

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 ):
 
 
  1. Package com. abc. mapper;

  2. Import com. abc. domain. Teacher;

  3. Import org. springframework. stereotype. Component;

  4. Import java. util. List;

  5. // @ Component specify the er name as myTeacherMapper

  6. // For more information, see the author's blog:

  7. // Http://legend2011.blog.51cto.com/3018495/980150

  8. @ Component ("myTeacherMapper ")

  9. Publicinterface TeacherMapper {

  10. Public Teacher getById (int id );

  11. // Query the instructor information by PAGE

  12. Public List <Teacher> findTeacherByPage (

  13. String sort, // sorting Field

  14. String dir, // sorting direction

  15. Int start, // start record

  16. Int limit // number of records

  17. );

  18. }

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:

 
 
  1. <? Xmlversion = "1.0" encoding = "utf8"?>

  2. <! DOCTYPE mapper PUBLIC "-// mybatis.org//DTD Mapper 3.0 // EN"

  3. Http://mybatis.org/dtd/mybatis-3-mapper.dtd>

  4. <! -- As before, the namespace value is the full name of the corresponding ER interface -->

  5. <Mappernamespace = "com. abc. mapper. TeacherMapper">

  6. <! -- Instructor entity ing -->

  7. <ResultMapid = "supervisorResultMap" type = "Teacher">

  8. <Idproperty = "id"/>

  9. <Resultproperty = "name"/>

  10. <Resultproperty = "gender"/>

  11. <Resultproperty = "researchArea" column = "research_area"/>

  12. <Resultproperty = "title"/>

  13. <! -- The collection element maps the attributes of the instructor's guidance student set. Here we use

  14. The namespace name. select statement id is used to reference

  15. Select statement getStudents. This collection element uses nested

  16. For details about the select statement, refer to the author's blog:

  17. Http://legend2011.blog.51cto.com/3018495/985907

  18. -->

  19. <Collectionproperty = "supStudents" column = "id" ofType = "Student"

  20. Select = "com. abc. mapper. StudentMapper. getStudents"/>

  21. </ResultMap>

  22. <! -- Param1, param2, and so on are the default names of the mappers method parameters of MyBatis -->

  23. <Selectid = "findTeacherByPage" resultMap = "supervisorResultMap">

  24. Select * from teacher

  25. Order by ${param1 }$ {param2} limit # {param3}, # {param4}

  26. </Select>

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

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.