Example of how to annotate MyBatis with multiple parameters -- 11 of MyBatis Study Notes

Source: Internet
Author: User

If the method in the er has only one parameter, You can reference this parameter in the corresponding SQL statement # {parameter name}. The previous examples mostly belong to this type. But this method does not apply to the need to pass multiple parameters, today to introduce how to use annotations to pass Multiple Parameter example source code: http://down.51cto.com/data/537051 ).

1. To use annotations to implement multi-parameter transfer, we should first introduce "org. apache. ibatis. annotations. param ", which we introduced in the interface TeacherMapper and added the Statement of the findTeacherByPage Method for instructor paging query. As follows:
 
 
  1. Package com. abc. mapper;

  2. Import com. abc. domain. Teacher;

  3. Import org. springframework. stereotype. Component;

  4. Import java. util. List;

  5. // To use the @ Param annotation, you must first introduce Param

  6. Import org. apache. ibatis. annotations. Param;

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

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

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

  10. @ Component ("myTeacherMapper ")

  11. Publicinterface TeacherMapper {

  12. Public Teacher getById (int id );

  13. // Query the instructor information by PAGE

  14. Public List <Teacher> findTeacherByPage (

  15. // Use the @ Param ("sort") annotation to go to the SQL statement

  16. // Reference the sort parameter value of this method in the form of "# {sort.

  17. // Of course, you can also use other names in @ Param,

  18. // For example, @ Param ("mysort ")

  19. @ Param ("sort") String sort, // sorting Field

  20. // The following three annotations are the same

  21. @ Param ("dir") String dir, // sort direction

  22. @ Param ("start") int start, // start record

  23. @ Param ("limit") int limit // number of records

  24. );

  25. }

The content of the corresponding ing file TeacherMapper. xml is as follows:
 
 
  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. <Selectid = "findTeacherByPage" resultMap = "supervisorResultMap">

  23. Select * from teacher

  24. Order by $ {sort }$ {dir} limit # {start}, # {limit}

  25. </Select>

  26. </Mapper>

Run the main program as follows:
 
 
  1. Package com. demo;

  2. Import org. springframework. context. ApplicationContext;

  3. Import com. abc. mapper. StudentMapper;

  4. Import com. abc. mapper. TeacherMapper;

  5. Import com. abc. domain. Teacher;

  6. Import com. abc. domain. Student;

  7. Import org. springframework. context. support. ClassPathXmlApplicationContext;

  8. Import java. util. List;

  9. Publicclass CollectionDemo

  10. {

  11. Privatestatic ApplicationContext ctx;

  12. Static

  13. {

  14. // Search for the resources/beans. xml file in the class path

  15. Ctx = new ClassPathXmlApplicationContext ("resources/beans. xml ");

  16. }

  17. Publicstaticvoid main (String [] args)

  18. {

  19. // Request the er from the Spring container

  20. TeacherMapper mapper =

  21. (TeacherMapper) ctx. getBean ("myTeacherMapper ");

  22. Teacher teacher = null;

  23. // Query the instructor's paging information

  24. List <Teacher> teachers =

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

  26. // Query 2 records

  27. Mapper. findTeacherByPage ("name", "asc", 0, 2 );

  28. If (teachers = null)

  29. {

  30. System. out. println ("related instructor information not found. ");

  31. }

  32. Else

  33. {

  34. Object [] t = teachers. toArray ();

  35. System. out. println ("************************************* *********");

  36. For (int I = 0; I <t. length; I ++)

  37. {

  38. Teacher = (Teacher) t [I];

  39. System. out. println ("instructor name:" + "" + teacher. getName ());

  40. System. out. println ("instructor title:" + "" + teacher. getTitle ());

  41. System. out. println ("Guiding student information :");

  42. // Traverse the guided students

  43. For (Student s: teacher. getSupStudents ())

  44. {

  45. System. out. println (s. getName () + "" + s. getGender ()

  46. + "" + S. getGrade ()

  47. + "" + S. getMajor ());

  48. }

  49. System. out. println ("************************************* *********");

  50. }

  51. }

  52. }

  53. }

The running result is as follows:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/12132Q117-0.png "border =" 0 "/>

Ii. Possible Errors 1. About order by, in general, we will reference parameters in the method in the form of # {parameter name, however, this method is invalid for the order by clause or returns an error. For example, when TeacherMapper. when the order by clause in the select statement findTeacherByPage of xml references the value of the sort parameter in the method in the form of # {sort}, the reader can verify it by himself ); when the dir parameter value in the method is referenced in the form of # {dir}, MySQLSyntaxErrorException is reported, as shown in:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/12132UE4-1.png "border =" 0 "/>

Therefore, the corresponding parameter value is referenced in the form of $ {parameter name.

2. invalid XML character error. This is a strange error. This error is reported when the Chinese character "error" is followed by the Chinese character ending in the comment in the ing file, as shown in:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/12132Q614-2.png "border =" 0 "/>

The same is true in the Spring configuration file beans. xml. Similarly, this error is also reported when the Chinese character "error" is followed by a Chinese comma. If a Chinese character is added after the word "error", no error is reported. However, an error is still reported when the word "ah" is added. You can try it on your own. Maybe you can find other errors.

All the exceptions reported are org. xml. sax. SAXParseException, as shown on the left side of the red box in the error image above. This may mean that they all use the same xml parsing component. Is this component a bug?

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/1015003

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.