Home >Java >javaTutorial >Detailed introduction to the incoming parameters of MyBatis
The parameterType attribute is mentioned in the select, insert, update, and delete elements of MyBatis. The parameterTypes that MyBatis can now use include basic data types and Java complex data types
Basic data types: including int, String, Date, etc. Basic data types are used as parameters, and only one can be passed in. The incoming value can be obtained through #{parameter name}
Complex data types: including JAVA entity classes and Maps. You can get the incoming value through #{Attribute name} or #{KeyName of map}
Basic data type parameter example:
Query the teacher list based on class ID
xml file
<select id="selectTeacher" parameterType="int" resultType="com.myapp.domain.Teacher"> select * from Teacher where c_id=#{id} </select>
Java Code
List<Teacher> tList = teacherMapper.selectTeacher(2); for (Teacher entityTemp : tList) { System.out.println(entityTemp.toString()); }
JAVA entity type parameter example:
<select id="selectTeacher" parameterType="com.myapp.domain.Teacher" resultType="com.myapp.domain.Teacher"> select * from Teacher where c_id=#{id} </select>
java代码 Teacher queryTeacher=new Teacher(); queryTeacher.setId(2); List<Teacher> tList = teacherMapper.selectTeacher(queryTeacher); for (Teacher entityTemp : tList) { System.out.println(entityTemp.toString()); }
Map parameter example:
<select id="selectTeacher" parameterType="Map" resultType="com.myapp.domain.Teacher"> select * from Teacher where c_id=#{id} and sex=#{sex} </select>
java代码 Map<String,String> map=new HasMap<String,String>(); map.put("id","2"); map.put("sex","男"); List<Teacher> tList = teacherMapper.selectTeacher(map); for (Teacher entityTemp : tList) { System.out.println(entityTemp.toString()); }
In addition, MyBatis also provides a way to use annotations to participate in multiple parameters. This method requires adding the @Param annotation to the parameters of the interface
Example:
Interface method
public List<Teacher> selectTeacher(@Param(value="id") String id,@Param(value="sex") String sex);
XML file
<select id="selectTeacher" resultType="com.myapp.domain.Teacher"> select * from Teacher where c_id=#{id} and sex=#{sex} </select>
Test code
List<Teacher> tList = teacherMapper.selectTeacher("2","男"); for (Teacher entityTemp : tList) { System.out.println(entityTemp.toString());
The above is a detailed introduction to the incoming parameters of MyBatis, more related Please pay attention to the PHP Chinese website (www.php.cn) for content!