首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

[转]MyBatis之传到参数

2012-07-05 
[转]MyBatis之传入参数在MyBatis的select、insert、update、delete这些元素中都提到了[html]?view plaincopy

[转]MyBatis之传入参数

在MyBatis的select、insert、update、delete这些元素中都提到了[html]?view plaincopy

  1. <select?id="selectTeacher"?parameterType="int"?resultType="com.myapp.domain.Teacher">??
  2. ????select?*?from?Teacher?where?c_id=#{id}??
  3. </select>??


java代码

[java]?view plaincopy
  1. List<Teacher>?tList?=?teacherMapper.selectTeacher(2);????
  2. for?(Teacher?entityTemp?:?tList)?{????
  3. ????System.out.println(entityTemp.toString());????
  4. }????
    JAVA实体类型参数示例:
    [html]?view plaincopy
    1. <select?id="selectTeacher"?parameterType="com.myapp.domain.Teacher"?resultType="com.myapp.domain.Teacher">??
    2. ????select?*?from?Teacher?where?c_id=#{id}??
    3. </select>??
    [java]?view plaincopy
    1. java代码??
    2. ??
    3. Teacher?queryTeacher=new?Teacher();??
    4. queryTeacher.setId(2);??
    5. List<Teacher>?tList?=?teacherMapper.selectTeacher(queryTeacher);????
    6. for?(Teacher?entityTemp?:?tList)?{????
    7. ????System.out.println(entityTemp.toString());?}??
      Map参数示例:
      [html]?view plaincopy
      1. <select?id="selectTeacher"?parameterType="Map"?resultType="com.myapp.domain.Teacher">??
      2. ????select?*?from?Teacher?where?c_id=#{id}?and?sex=#{sex}??
      3. </select>??

      [java]?view plaincopy
      1. java代码??
      2. ??
      3. Map<String,String>?map=new?HasMap<String,String>();??
      4. map.put("id","2");??
      5. map.put("sex","男");??
      6. List<Teacher>?tList?=?teacherMapper.selectTeacher(map);????
      7. for?(Teacher?entityTemp?:?tList)?{????
      8. ????System.out.println(entityTemp.toString());?}??

      ?另外MyBatis还提供了一个使用注解来参入多个参数的方式。这种方式需要在接口的参数上添加@Param注解

      示例:

      接口方法

      [java]?view plaincopy
      1. public?List<Teacher>?selectTeacher(@Param(value="id")?String?id,@Param(value="sex")?String?sex);??


      XML文件

      [html]?view plaincopy
      1. <select?id="selectTeacher"??resultType="com.myapp.domain.Teacher">??
      2. ????select?*?from?Teacher?where?c_id=#{id}?and?sex=#{sex}??
      3. </select>??


      测试代码

      [java]?view plaincopy
      1. List<Teacher>?tList?=?teacherMapper.selectTeacher("2","男");????
      2. for?(Teacher?entityTemp?:?tList)?{????
      3. ????System.out.println(entityTemp.toString()); ??