例如:查询id=1,name=tom的一条数据

查询接口:

User getUserByIdAndName(Integer id,String name);

//
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--
namespace:命名空间,指定为接口的全类名
selectUserById:唯一标识
resultType:返回值类型
-->
<mapper namespace="com.yunqing.mybatis.dao.UserMapper">
//这种写法是错误的,mybatis会根据多个参数值封装一个map,在map的key中根据param1....paramN取值或者根据参数索引取值arg0....arg(N-1)
<!--
   <select id="getUserByIdAndName" resultType="com.yunqing.mybatis.bean.User">
select * from t_user where id = #{id} and name = #{name}
</select>
--> //正确写法 1.根据参数索引
   <select id="getUserByIdAndName" resultType="com.yunqing.mybatis.bean.User">
select * from t_user where id = #{arg0} and name = #{arg1}
</select>
//正确写法 2.根据参数param
  
   <select id="getUserByIdAndName" resultType="com.yunqing.mybatis.bean.User">
select * from t_user where id = #{param1} and name = #{param2}
</select>
</mapper>

建议使用的方法是

<select id="getUserByIdAndName" resultType="com.yunqing.mybatis.bean.User">
select * from t_user where id = #{id} and name = #{name}
</select>
这种方法虽然是错误的,但是只要在dao层使用命名参数指定参数名,就是最正确,最明确,最建议的写法:
User getUserByIdAndName(@Param("id")Integer id, @Param("name")String name);

也可以直接传一个pojo对象,或者一个Map,或者自定义To数据传输对象

1.map参数

xml中sql

<select id="getUserByMap" resultType="com.yunqing.mybatis.bean.User">
select * from t_user where id = #{id} and name = #{name}
</select>

dao

User getUserByMap(Map<String,Object> map);

test.java

@Test
public void getUserByMap() throws IOException {
//从xml中获取sqlSessionFactory
String resource = "conf/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //获取sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
Map<String,Object> map = new HashMap<>();
map.put("id",1);
map.put("name","tom");
User user = userMapper.getUserByMap(map);
System.out.println(user);
sqlSession.close();
}

mybatis会对多参数方法进行特殊处理的更多相关文章

  1. [原创]Spring Boot + Mybatis 简易使用指南(二)多参数方法支持 与 Joda DateTime类型支持

    前言 今天在开发练习项目时遇到两个mybatis使用问题 第一个问题是mapper方法参数问题,在参数大于一个时,mybatis不会自动识别参数命名 第二个问题是Pojo中使用Joda DateTim ...

  2. Mybatis 传入多个参数查询数据 (3种方法)

    第一种方案 DAO层的函数方法 public User selectUser(String name,String area); 对应的Mapper.xml <select id="s ...

  3. MyBatis映射文件4(参数获取#{}和${}/select标签详解[返回类型为list])

    参数获取 之前我们都是采用#{}的方式进行参数传递,其实MyBatis还有另外的参数传递方式${} 使用方法相同,但是还是有很大区别的 这里做一个测试: <select id="get ...

  4. Mybatis找不到参数错误:There is no getter for property named 'categoryId' in 'class java.lang.Integer'。

    Mybatis找不到参数错误:There is no getter for property named 'categoryId' in 'class java.lang.Integer'. 错误Li ...

  5. (转)MyBatis传入多个参数的问题

    背景:记录mybaitis的使用方法,博闻强记,后面尽量记忆使用. MyBatis传入多个参数的问题 MyBatis传入多个参数的问题 详细记录mybatis在传递多个参数时候的使用方法 关于Myba ...

  6. Mybatis传多个参数的问题 及MyBatis报错 Parameter '0' not found. Available parameters are [arg1, arg0, param1 问题

    对于使用Mybatis ,传多个参数,我们可以使用对象封装外,还可以直接传递参数 对象的封装,例如查询对象条件basequery对象 <select id="getProductByP ...

  7. Action接收页面传来的参数方法

    接收页面传来的参数方法 1.第一种:在action中设置相应的变量 在相应的action中设置与将要传进来的参数名相同的变量 eg: 页面传给后台两个参数 name=chance & age ...

  8. C# 中的可变参数方法(VarArgs)

    首先需要明确一点:这里提到的可变参数方法,指的是具有 CallingConventions.VarArgs 调用约定的方法,而不是包含 params 参数的方法.可以通过MethodBase.Call ...

  9. mybatis 查询 xml list参数

    mybatis 查询 xml list参数: <select id="getByIds" resultType="string" parameterTyp ...

随机推荐

  1. javascript预编译和执行过程总结

    javascript相对于其它语言来说是一种弱类型的语言,在其它如java语言中,程序的执行需要有编译的阶段,而在javascript中也有类似的“预编译阶段”(javascript的预编译是以代码块 ...

  2. js 去除空格回车换行

    开发中遇到特殊需求,需要将空格回车换行替换成逗号 直接使用正则表达式解决: 变量.replace(/\s+/g,","); 如果想要去除则将后面逗号变成空就OK了.

  3. Toolbar和Drawerlayout的基本使用

    参考文章: http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1118/2006.html http://www.codeceo.c ...

  4. xunsearch bsd 10.1安装心酸路。。。

    cc -c -I/usr/local/xunsearch/include/ test.cpp cc test.o -L/usr/local/xunsearch/lib -lscws -lm -o te ...

  5. Django_MTV模型

    创建DJango项目过程: 1:安装Django包 方式一:pip3 install django 方式二:pycharm-->file--->settings-->找到projec ...

  6. swoole 创建tcp服务器

    server.php <?php /** * 创建tcp服务器 * Date: 2019/1/15 */ $serv = new swoole_server('127.0.0.1', 9501) ...

  7. Android学习——BroadCast(二)

    这篇博文介绍有序广播的使用 有序广播的发送 和普通广播完全相同,只需要更换发送的方式即可 ordered.setOnClickListener(new View.OnClickListener() { ...

  8. less @import and extend及mixin详解

    在less中,通过 @import (keyword) "filename"的方式引入其他的文件,这个keyword可以是以下6种: referrence referrence这个 ...

  9. 微软在Build 2016开发者大会中发布 “认知服务”,牛津计划有正式名字啦!

    2016年3月30日:微软在Build 2016开发者大会中发布“认知服务”. 在Build 2016开发者大会中,微软发布了新的智能服务:微软认知服务(Microsoft Cognitive Ser ...

  10. dctcp-2.6.26-rev1.1.0.patch

    dctcp-2.6.26-rev1.1.0.patch diff -Naur linux-/include/linux/sysctl.h linux--dctcp-rev1.1.0/include/l ...