Mybatis的多参数传递问题

来源:互联网 发布:董秘助手软件 编辑:程序博客网 时间:2024/05/18 00:56

申明:以下代码并不是从IDE工具中拷贝,是直接通过TXT编辑,有可能有worry,但不会影响主体,只需稍微纠正。

1、传递参数以Map的形式传递参数

在Service层建立map并传递:
private UserMapping UM;
public User UserMappingselectByIdAndName(int id,String name){
Map userMap=new HashMap();
userMap.put("id",id);
userMap.put("name",name);
return this.UM.selectByIdAndName(userMap);
}

DAO层的方法如下:
User selectByIdAndName(Map userMap);//(User)查询的结果可以是对象,也可以是集合(List<User>)

Mode层对应的mapping的部分内容如下:
<select id="selectByIdAndName" ResultMap="BaseResultMap">
select * from User where id=#{id} and name=#{name}
</select>

2、传递参数以0、1....等方式传递
DAO层的多参数方法如下:
User selectByIdAndName(int id,String name);

Mode层对应的mapping的部分内容如下:
<select id="selectByIdAndName" ResultMap="BaseResultMap">
select * from User where id=#{0} and name=#{1}
</select>

3、传递参数以@param的方式传递
Dao层的多参数方法如下:
User selectByIdAndName(@Param(int id),@Param(String name));//注意Param的大小写,引入的包是org.apache.ibatis.annotations.Param;

Mode层对应的mapping的部分内容如下:
<select id="selectByIdAndName" ResultMap="BaseResultMap">
select * from User where id=#{id} and name=#{name}
</select>

1 0
原创粉丝点击