MyBatis傳入參數的實例代碼。本站提示廣大學習愛好者:(MyBatis傳入參數的實例代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是MyBatis傳入參數的實例代碼正文
在MyBatis的select、insert、update、delete這些元素中都提到了parameterType這個屬性。MyBatis如今可使用的parameterType有根本數據類型和JAVA龐雜數據類型
根本數據類型:包括int,String,Date等。根本數據類型作為傳參,只能傳入一個。經由過程#{參數名} 便可獲得傳入的值
龐雜數據類型:包括JAVA實體類、Map。經由過程#{屬性名}或#{map的KeyName}便可獲得傳入的值
根本數據類型參數示例:
依據班級ID查詢教員列表
xml文件
<select id="selectTeacher" parameterType="int" resultType="com.myapp.domain.Teacher"> select * from Teacher where c_id=#{id} </select>
java代碼
List<Teacher> tList = teacherMapper.selectTeacher(2); for (Teacher entityTemp : tList) { System.out.println(entityTemp.toString()); }
JAVA實體類型參數示例:
<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注解
示例:
接口辦法
public List<Teacher> selectTeacher(@Param(value="id") String id,@Param(value="sex") String sex);
XML文件
<select id="selectTeacher" resultType="com.myapp.domain.Teacher"> select * from Teacher where c_id=#{id} and sex=#{sex} </select>
測試代碼
List<Teacher> tList = teacherMapper.selectTeacher("2","男"); for (Teacher entityTemp : tList) { System.out.println(entityTemp.toString());
上面再給年夜家分享MyBatis傳參
1、當傳參為list 時:
1.1 mapper接口中:
void updateContactsIsRead(List<Integer> logidList);
1.2 mapper.xml 文件中:
<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>
以上所述是小編給年夜家引見的MyBatis傳入參數的實例代碼,願望對年夜家有所贊助,假如年夜家有任何疑問請給我留言,小編會實時答復年夜家的。在此也異常感激年夜家對網站的支撐!