标签:
鸣谢:http://blog.csdn.net/liaoxiaohua1981/article/details/6862764
--------------------------------------------------------------------
在MyBatis的select、insert、update、delete这些元素中都提到了parameterType这个属性。MyBatis现在可以使用的parameterType有基本数据类型和JAVA复杂数据类型
根据班级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()); }
xml文件 (注:parameterType="com.myapp.domain.Teacher" 可简写为 parameterType="Teacher" )
<select id="selectTeacher" parameterType="com.myapp.domain.Teacher" resultType="com.myapp.domain.Teacher"> select * from Teacher where c_id=#{id} </select>
java代码
java代码 Teacher queryTeacher=new Teacher(); queryTeacher.setId(2); List<Teacher> tList = teacherMapper.selectTeacher(queryTeacher); for (Teacher entityTemp : tList) { System.out.println(entityTemp.toString()); }
xml文件
<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()); }
示例:
接口方法
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之传入参数——parameterType(转)
标签:
原文地址:http://www.cnblogs.com/wql025/p/5001621.html