ParameterType usage of the mapper configuration file for MyBatis

Source: Internet
Author: User

The ParameterType attribute is mentioned in the MyBatis Select, INSERT, UPDATE, delete elements . MyBatis can now be used for parametertype with basic data types and Java complex data types

    • Basic data type: Contains Int,string,date and so on. The base data type is passed as a parameter and can only pass in one. The passed value can be obtained by #{the parameter name}
    • Complex data types: Contains Java entity classes, maps. By #{property name} or
    • #{map KeyName} to get the value passed in

    • Examples of basic data type parameters:

Query teacher list based on 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. }
    • Examples of Java entity type Parameters :
[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 ()); }
  • Example Map parameters:
    [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.    
      3. map<string,string> map=new  HasMap<String,String> ();   
      4. map.put ( , );   
      5. map.put ( "sex" , "male" );   
      6. list<teacher> tlist  = teachermapper.selectteacher (map);     
      7. for   (teacher entitytemp : tlist)  {    
      8.     system.out.println (entitytemp.tostring ()); }   
 In addition, MyBatis provides a way to use annotations to enter multiple parameters. This approach requires adding @param annotations on the interface's 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 ());

ParameterType usage of the mapper configuration file for MyBatis

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.