The parameterType attribute is mentioned in the select, insert, update, and delete elements of MyBatis. MyBatis can now use parameterTypes with basic data types and JAVA complex data types
Basic data types: including int, String, Date, etc. As a parameter, only one basic data type can be passed in. The passed value can be obtained through #{parameter name}
Complex data types: including JAVA entity class, Map. You can get the incoming value by #{attribute name} or #{map's KeyName}
Example of basic data type parameters:
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()); }Example of JAVA entity type parameter:
<select id="selectTeacher" parameterType="com.myapp.domain.Teacher" resultType="com.myapp.domain.Teacher"> select * from Teacher where c_id=#{id} </select> Java code
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 code
Map<String,String> map=new HasMap<String,String>(); map.put("id","2"); map.put("sex","male"); 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 register multiple parameters. This method requires adding @Param annotation to the interface parameters
Example:
Interface method
public List<Teacher> selectTeacher(@Param(value="id") String id,@Param(value="sex") String sex);
XML files
<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","Male"); for (Teacher entityTemp: tList) { System.out.println(entityTemp.toString());Next, I will share with you MyBatis transcribing
1. When the parameter is passed as a list:
1.1 mapper interface:
void updateContactsIsRead(List<Integer> logidList);
1.2 in the mapper.xml file:
<update id="updateContactsIsRead"> update emaillog2 set isRead = 1 where isRead = 0 and logid in <foreach collection="list" item="logid" index="index" open="(" close=")" separator=","> #{logid,jdbcType=INTEGER} </foreach> </update>The above is the example code of MyBatis parameters introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!