mybatis会对多参数方法进行特殊处理
例如:查询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会对多参数方法进行特殊处理的更多相关文章
- [原创]Spring Boot + Mybatis 简易使用指南(二)多参数方法支持 与 Joda DateTime类型支持
前言 今天在开发练习项目时遇到两个mybatis使用问题 第一个问题是mapper方法参数问题,在参数大于一个时,mybatis不会自动识别参数命名 第二个问题是Pojo中使用Joda DateTim ...
- Mybatis 传入多个参数查询数据 (3种方法)
第一种方案 DAO层的函数方法 public User selectUser(String name,String area); 对应的Mapper.xml <select id="s ...
- MyBatis映射文件4(参数获取#{}和${}/select标签详解[返回类型为list])
参数获取 之前我们都是采用#{}的方式进行参数传递,其实MyBatis还有另外的参数传递方式${} 使用方法相同,但是还是有很大区别的 这里做一个测试: <select id="get ...
- 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 ...
- (转)MyBatis传入多个参数的问题
背景:记录mybaitis的使用方法,博闻强记,后面尽量记忆使用. MyBatis传入多个参数的问题 MyBatis传入多个参数的问题 详细记录mybatis在传递多个参数时候的使用方法 关于Myba ...
- Mybatis传多个参数的问题 及MyBatis报错 Parameter '0' not found. Available parameters are [arg1, arg0, param1 问题
对于使用Mybatis ,传多个参数,我们可以使用对象封装外,还可以直接传递参数 对象的封装,例如查询对象条件basequery对象 <select id="getProductByP ...
- Action接收页面传来的参数方法
接收页面传来的参数方法 1.第一种:在action中设置相应的变量 在相应的action中设置与将要传进来的参数名相同的变量 eg: 页面传给后台两个参数 name=chance & age ...
- C# 中的可变参数方法(VarArgs)
首先需要明确一点:这里提到的可变参数方法,指的是具有 CallingConventions.VarArgs 调用约定的方法,而不是包含 params 参数的方法.可以通过MethodBase.Call ...
- mybatis 查询 xml list参数
mybatis 查询 xml list参数: <select id="getByIds" resultType="string" parameterTyp ...
随机推荐
- 设计模式入门,观察者模式,c++代码实现
// test02.cpp : Defines the entry point for the console application.////设计模式第2章 观察者模式#include " ...
- MySQL聚合函数在计算时,不会自动匹配与之相对应的数据
学习mysql过程中遇到了一个困惑,纠结了我半天时间,刚刚又重新复习了一下,终于知道问题所在 以下是一个需求: 取得平均薪水最高的部门的部门编号 代码如下: select deptno, avg(sa ...
- Scarpy+selenium 结合使用
首先要先在spider对象实例化时,同时实例化一个浏览器对象 # -*- coding: utf-8 -*- import scrapy from selenium import webdriver ...
- Python-MRO和C3算法
一. python多继承 在前面的学习过程中,我们已经知道了python中类与类之间可以有继承关系,当出现x是一种y的时候就可以使用继承关系.即'is-a'关系,在继承关系中子类自动拥有父类中除了私有 ...
- Python基础-面向过程编程实现Linux下cat -rl ‘dir’ |grep ‘keywords’ 功能
函数是Python内建支持的一种封装,我们通过把大段代码拆成函数,通过一层一层的函数调用,就可以把复杂任务分解成简单的任务,这种分解可以称之为面向过程的程序设计.函数就是面向过程的程序设计的基本单元. ...
- 讲解JavaScript两个圆括号、自调用和闭包函数
一.JavaSript圆括号的使用 先来看一组通过函数声明来定义的函数: 先附代码: 运行结果如下: 这里我们可以看出: Ø 若没有加圆括号,则返回的是这个函数的内容 Ø 若加上圆括号,则返回的是 ...
- Java入门到精通——调错篇之Astah Community打开报需要jre1.7运行环境
1.问题概述 Astah Community安装完以后点击运行Astah Community的时候报此应用需要jdk1.7如下图 但是我的电脑在D盘装了jdk1.8了为什么这个软件为什 ...
- Java—IO流 字符流
java的文本(char)是16位无符号整数,是字符的unicode编码(双字节编码). 文件是byte byte byte ... 的数据序列. 文本文件是文本(char)序列按照某种编码方案(uf ...
- django模板报错:Requested setting TEMPLATE_DEBUG, but settings are not configured. You must either define
转自:http://blog.csdn.net/xiaowanggedege/article/details/8651236 django模板报错: Requested setting TEMPLAT ...
- Netty入门2之----手动搭建HttpServer
在上一章中我们认识了netty,他有三大优点:并发高,传输快,封装好.在这一章我们来用Netty搭建一个HttpServer,从实际开发中了解netty框架的一些特性和概念. netty.png 认识 ...