ParameterType usage of MyBatis Mapper configuration file, mybatismapper. xml

Source: Internet
Author: User

ParameterType usage of MyBatis Mapper configuration file, mybatismapper. xml

The parameterType attribute is mentioned in the select, insert, update, and delete elements of MyBatis. The parametertypes that MyBatis can currently use include basic data types and complex JAVA data types.

  • Basic Data Type: Contains int, String, Date, and so on. The basic data type is used as a parameter. Only one parameter can be input. You can use # {parameter name} to obtain the passed value.
  • Complex data types: including JAVA object classes and Map. By # {attribute name} or
  • # {Map's KeyName} can get the input value

 

  • Example of basic data type parameters:

Query the instructor list by class ID

Xml file

[Html]View plaincopy
  1. <Select id = "selectTeacher" parameterType = "int" resultType = "com. myapp. domain. Teacher">
  2. Select * from Teacher where c_id =#{ id}
  3. </Select>


Java code

[Java]View plaincopy
  1. List <Teacher> tList = teacherMapper. selectTeacher (2 );
  2. For (Teacher entityTemp: tList ){
  3. System. out. println (entityTemp. toString ());
  4. }
  • JAVA object type parameter example:
[Html]View plaincopy
  1. <Select id = "selectTeacher" parameterType = "com. myapp. domain. Teacher" resultType = "com. myapp. domain. Teacher">
  2. Select * from Teacher where c_id =#{ id}
  3. </Select>
[Java]View plaincopy
  1. Java code
  2. Teacher queryTeacher = new Teacher ();
  3. QueryTeacher. setId (2 );
  4. List <Teacher> tList = teacherMapper. selectTeacher (queryTeacher );
  5. For (Teacher entityTemp: tList ){
  6. System. out. println (entityTemp. toString ());}
  • Map parameter example:
    [Html]View plaincopy
    1. <Select id = "selectTeacher" parameterType = "Map" resultType = "com. myapp. domain. Teacher">
    2. Select * from Teacher where c_id = # {id} and sex = # {sex}
    3. </Select>

    [Java]View plaincopy
    1. Java code
    2. Map <String, String> map = new HasMap <String, String> ();
    3. Map. put ("id", "2 ");
    4. Map. put ("sex", "male ");
    5. List <Teacher> tList = teacherMapper. selectTeacher (map );
    6. For (Teacher entityTemp: tList ){
    7. System. out. println (entityTemp. toString ());}
In addition, MyBatis also provides a method for using annotations to input multiple parameters. In this way, the @ Param annotation must be added to the interface parameters.

Example:

Interface Method

[Java]View plaincopy
  1. Public List <Teacher> selectTeacher (@ Param (value = "id") String id, @ Param (value = "sex") String sex );


XML file

[Html]View plaincopy
  1. <Select id = "selectTeacher" resultType = "com. myapp. domain. Teacher">
  2. Select * from Teacher where c_id = # {id} and sex = # {sex}
  3. </Select>


Test code

[Java]View plaincopy
  1. List <Teacher> tList = teacherMapper. selectTeacher ("2", "male ");
  2. For (Teacher entityTemp: tList ){
  3. System. out. println (entityTemp. toString ());

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.