首页 > 代码库 > MyBatis之传入参数parameterType
MyBatis之传入参数parameterType
在MyBatis的select、insert、update、delete这些元素中都提到了parameterType这个属性。MyBatis现在可以使用的parameterType有基本数据类型和Java复杂数据类型
- 基本数据类型:包含int,String,Date等。基本数据类型作为传参,只能传入一个。通过#{参数名} 即可获取传入的值
- 复杂数据类型:包含JAVA实体类、Map。通过#{属性名}或#{map的KeyName}即可获取传入的值
- 基本数据类型参数示例:
根据班级ID查询教师列表
xml文件
[html] view plain copy
- <select id="selectTeacher" parameterType="int" resultType="com.myapp.domain.Teacher">
- select * from Teacher where c_id=#{id}
- </select>
java代码[java] view plain copy
- List<Teacher> tList = teacherMapper.selectTeacher(2);
- for (Teacher entityTemp : tList) {
- System.out.println(entityTemp.toString());
- }
- JAVA实体类型参数示例:
[html] view plain copy
- <select id="selectTeacher" parameterType="com.myapp.domain.Teacher" resultType="com.myapp.domain.Teacher">
- select * from Teacher where c_id=#{id}
- </select>
[java] view plain copy
- java代码
- Teacher queryTeacher=new Teacher();
- queryTeacher.setId(2);
- List<Teacher> tList = teacherMapper.selectTeacher(queryTeacher);
- for (Teacher entityTemp : tList) {
- System.out.println(entityTemp.toString()); }
- Map参数示例:[html] view plain copy
- <select id="selectTeacher" parameterType="Map" resultType="com.myapp.domain.Teacher">
- select * from Teacher where c_id=#{id} and sex=#{sex}
- </select>
[java] view plain copy- 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注解
示例:
接口方法
[java] view plain copy
- public List<Teacher> selectTeacher(@Param(value=http://www.mamicode.com/"id") String id,@Param(value=http://www.mamicode.com/"sex") String sex);
XML文件
[html] view plain copy
- <select id="selectTeacher" resultType="com.myapp.domain.Teacher">
- select * from Teacher where c_id=#{id} and sex=#{sex}
- </select>
测试代码
[java] view plain copy
- List<Teacher> tList = teacherMapper.selectTeacher("2","男");
- for (Teacher entityTemp : tList) {
- System.out.println(entityTemp.toString());
本文转自http://blog.csdn.net/baidu_31657889/article/details/52118650
MyBatis之传入参数parameterType
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。