---
title: "[转]MyBatis之传入参数"
author: "badwing"
date: "2012-06-14T10:52:59Z"
tags: []
categories: ["开源软件"]
slug: "mybatis"
draft: false
lastmod: "2012-06-14T10:52:59Z"
id: 1560707
url: "https://badwing.iteye.com/blog/1560707"
---

**在MyBatis的select、insert、update、delete这些元素中都提到了parameterType这个属性。MyBatis现在可以使用的parameterType有基本数据类型和JAVA复杂数据类型**

- 基本数据类型：包含int,String,Date等。基本数据类型作为传参，只能传入一个。通过#{参数名} 即可获取传入的值
- 复杂数据类型：包含JAVA实体类、Map。通过#{属性名}或#{map的KeyName}即可获取传入的值

 

- 基本数据类型参数示例：

> 根据班级ID查询教师列表
> 
> xml文件
> 
> **\[html]** [view plain](http://blog.csdn.net/liaoxiaohua1981/article/details/6862764 "view plain")[copy](http://blog.csdn.net/liaoxiaohua1981/article/details/6862764 "copy")
> 
> 1. &lt;select id="selectTeacher" parameterType="int" resultType="com.myapp.domain.Teacher"&gt;
> 2. select * from Teacher where c\_id=#{id}
> 3. &lt;/select&gt;
> 
> java代码
> 
> **\[java]** [view plain](http://blog.csdn.net/liaoxiaohua1981/article/details/6862764 "view plain")[copy](http://blog.csdn.net/liaoxiaohua1981/article/details/6862764 "copy")
> 
> 1. List&lt;Teacher&gt; tList = teacherMapper.selectTeacher(2);
> 2. for (Teacher entityTemp : tList) {
> 3. System.out.println(entityTemp.toString());
> 4. }
> 
> <!--THE END-->

- JAVA实体类型参数示例：

> **\[html]** [view plain](http://blog.csdn.net/liaoxiaohua1981/article/details/6862764 "view plain")[copy](http://blog.csdn.net/liaoxiaohua1981/article/details/6862764 "copy")
> 
> 1. &lt;select id="selectTeacher" parameterType="com.myapp.domain.Teacher" resultType="com.myapp.domain.Teacher"&gt;
> 2. select * from Teacher where c\_id=#{id}
> 3. &lt;/select&gt;
> 
> **\[java]** [view plain](http://blog.csdn.net/liaoxiaohua1981/article/details/6862764 "view plain")[copy](http://blog.csdn.net/liaoxiaohua1981/article/details/6862764 "copy")
> 
> 1. java代码
> 2. Teacher queryTeacher=new Teacher();
> 3. queryTeacher.setId(2);
> 4. List&lt;Teacher&gt; tList = teacherMapper.selectTeacher(queryTeacher);
> 5. for (Teacher entityTemp : tList) {
> 6. System.out.println(entityTemp.toString()); }
> 
> <!--THE END-->

- Map参数示例：
  
  **\[html]** [view plain](http://blog.csdn.net/liaoxiaohua1981/article/details/6862764 "view plain")[copy](http://blog.csdn.net/liaoxiaohua1981/article/details/6862764 "copy")
  
  1. &lt;select id="selectTeacher" parameterType="Map" resultType="com.myapp.domain.Teacher"&gt;
  2. select * from Teacher where c\_id=#{id} and sex=#{sex}
  3. &lt;/select&gt;
  
  **\[java]** [view plain](http://blog.csdn.net/liaoxiaohua1981/article/details/6862764 "view plain")[copy](http://blog.csdn.net/liaoxiaohua1981/article/details/6862764 "copy")
  
  1. java代码
  2. Map&lt;String,String&gt; map=new HasMap&lt;String,String&gt;();
  3. map.put("id","2");
  4. map.put("sex","男");
  5. List&lt;Teacher&gt; tList = teacherMapper.selectTeacher(map);
  6. for (Teacher entityTemp : tList) {
  7. System.out.println(entityTemp.toString()); }

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

示例：

接口方法

**\[java]** [view plain](http://blog.csdn.net/liaoxiaohua1981/article/details/6862764 "view plain")[copy](http://blog.csdn.net/liaoxiaohua1981/article/details/6862764 "copy")

1. public List&lt;Teacher&gt; selectTeacher(@Param(value="id") String id,@Param(value="sex") String sex);

XML文件

**\[html]** [view plain](http://blog.csdn.net/liaoxiaohua1981/article/details/6862764 "view plain")[copy](http://blog.csdn.net/liaoxiaohua1981/article/details/6862764 "copy")

1. &lt;select id="selectTeacher"  resultType="com.myapp.domain.Teacher"&gt;
2. select * from Teacher where c\_id=#{id} and sex=#{sex}
3. &lt;/select&gt;

测试代码

**\[java]** [view plain](http://blog.csdn.net/liaoxiaohua1981/article/details/6862764 "view plain")[copy](http://blog.csdn.net/liaoxiaohua1981/article/details/6862764 "copy")

1. List&lt;Teacher&gt; tList = teacherMapper.selectTeacher("2","男");
2. for (Teacher entityTemp : tList) {
3. System.out.println(entityTemp.toString());