在MyBatis的select、insert、update、delete這些元素中都提到了parameterType這個屬性。MyBatis如今可使用的parameterType有基本數據類型和JAVA複雜數據類型html
- 基本數據類型:包含int,String,Date等。基本數據類型做爲傳參,只能傳入一個。經過#{參數名} 便可獲取傳入的值
- 複雜數據類型:包含JAVA實體類、Map。經過#{屬性名}或#{map的KeyName}便可獲取傳入的值
根據班級ID查詢教師列表java
xml文件app
- <select id="selectTeacher" parameterType="int" resultType="com.myapp.domain.Teacher">
- select * from Teacher where c_id=#{id}
- </select>
java代碼dom
- List<Teacher> tList = teacherMapper.selectTeacher(2);
- for (Teacher entityTemp : tList) {
- System.out.println(entityTemp.toString());
- }
- <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參數示例:
- <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()); }
另外MyBatis還提供了一個使用註解來參入多個參數的方式。這種方式須要在接口的參數上添加@Param註解
示例:測試
接口方法spa
- public List<Teacher> selectTeacher(@Param(value="id") String id,@Param(value="sex") String sex);
XML文件.net
- <select id="selectTeacher" resultType="com.myapp.domain.Teacher">
- select * from Teacher where c_id=#{id} and sex=#{sex}
- </select>
測試代碼xml
- List<Teacher> tList = teacherMapper.selectTeacher("2","男");
- for (Teacher entityTemp : tList) {
- System.out.println(entityTemp.toString());