mybatis中如何使用mapper中传递多个参数呢?
下文笔者讲述mybaits中mapper传递多个参数的方法分享,如下所示
实现思路:
方式1:
Mapper(DAO层)中定义多个参数
然而参数时,使用0,1代表
第一个参数,第二个参数
方式2:
使用@param注解参数定义参数名称
然后xml文件中使用此名称即可
方式3:
将多个参数封装到map中
然后获取即可
例:
方式1:
//DAO层的函数
Public UserselectUser(String name,String userno);
//对应的xml
//#{0}代表接收的是dao层中的第一个参数
//#{1}代表dao层中第二参数,更多参数一致往后加即可。
<select id="selectUser"resultMap="BaseResultMap">
select * from users
where user_name = #{0}
and userno=#{1}
</select>
方式2:使用 @param 注解:
public interface usermapper {
user selectuser(@param(“username”) string username,
@param(“userno”) string userno);
}
然后,就可以在xml像下面这样使用(推荐封装为一个map,作为单个参数传递给mapper):
<select id=”selectuser” resulttype=”user”>
select * from users
where user_name = #{username}
and userno = #{userno}
</select>
方式3:多个参数封装成map
try{
//映射文件的命名空间.SQL片段的ID,就可以调用对应的映射文件中的SQL
//由于我们的参数超过了两个,而方法中只有一个Object参数收集,因此我们使用Map集合来装载我们的参数
Map<String, Object> map = new HashMap();
map.put("username", username);
map.put("userno", userno);
return sqlSession.selectlist("userid.selectuser", map);
}catch(Exception e){
e.printStackTrace();
sqlSession.rollback();
throw e; }
finally{
MybatisUtil.closeSqlSession();
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


