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
- < Select id="selectteacher" parametertype="int" resulttype = "Com.myapp.domain.Teacher" >
- SELECT * from Teacher where c_id=#{id}
- </ Select >
Java code
[Java]View Plaincopy
- list<teacher> tList = Teachermapper.selectteacher (2);
- for (Teacher entitytemp:tlist) {
- System.out.println (Entitytemp.tostring ());
- }
- Examples of Java entity type Parameters :
[HTML]View Plaincopy
- < Select id="selectteacher" parametertype=" Com.myapp.domain.Teacher " resulttype=" Com.myapp.domain.Teacher " >
- SELECT * from Teacher where c_id=#{id}
- </ Select >
[Java]View Plaincopy
- Java code
- Teacher queryteacher=New Teacher ();
- Queryteacher.setid (2);
- list<teacher> tList = Teachermapper.selectteacher (Queryteacher);
- for (Teacher entitytemp:tlist) {
- System.out.println (Entitytemp.tostring ()); }
- Example Map parameters:
[HTML]View Plaincopy
- < Select id="selectteacher" parametertype="Map" resulttype = "Com.myapp.domain.Teacher" >
- SELECT * from Teacher where c_id=#{id} and sex=#{sex}
- </ Select >
[Java]View Plaincopy
- java code
-
- map<string,string> map=new HasMap<String,String> ();
- map.put ( , );
- map.put ( "sex" , "male" );
- list<teacher> tlist = teachermapper.selectteacher (map);
- for (teacher entitytemp : tlist) {
- 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
- Public list<teacher> selectteacher (@Param(value="id") String ID, @Param (value="Sex") String sex);
XML file
[HTML]View Plaincopy
- < Select id="selectteacher" resulttype=" Com.myapp.domain.Teacher ">
- SELECT * from Teacher where c_id=#{id} and sex=#{sex}
- </ Select >
Test code
[Java]View Plaincopy
- list<teacher> tList = teachermapper.selectteacher ("2","male");
- for (Teacher entitytemp:tlist) {
- System.out.println (Entitytemp.tostring ());
ParameterType usage of the mapper configuration file for MyBatis