例如:查询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. 五、mybatis集成使用

    1.添加依赖 <!-- mybatis-spring集成--> <dependency> <groupId>org.mybatis.spring.boot</ ...

  2. PHP5中Static和Const关键字

    (1) static static要害字在类中是,描述一个成员是静态的,static能够限制外部的访问,因为static后的成员是属于类的,是不属于任何对象实例,其他类是无法访问的,只对类的实例共享, ...

  3. 2018-12-25 课堂笔记&面试题

    面试题一.Java中,八大基本数据类型有哪些?答:数值型:整型(byte.short.int.long)浮点型(float.double)非数值型:布尔类型(boolean)字符型(char).注意: ...

  4. 基于bootstrap的内容折叠功能

    加入js及css支持: <link rel="stylesheet" href="css/bootstrap.min.css"/> <scri ...

  5. Linux pip 安装模块时,一直黄字错误:Could not find a version that satisfies the requirement

    参考原文:https://blog.csdn.net/u012592062/article/details/51966649 这时我们用国内的镜像源来加速 pip install 包名-i http: ...

  6. JRebel&XRebel

    介绍==>>>> JRebel&XRebel官网 https://zeroturnaround.com/HotSwap和JRebel原理 http://www.holl ...

  7. Java接口和抽象类理解(New)

    一. 抽象类和接口的特点  包含抽象方法的类称为抽象类,但并不意味着抽象类中只能有抽象方法,它和普通类一样,同样可以拥有成员变量和普通的成员方法.注意,抽象类和普通类的主要有三点区别: 1)抽象方法必 ...

  8. CSS背景相关属性

    CSS样式可以精确控制HTML元素的背景.边框的样式和外观,也可以精确控制边框的线型和形状.其中,背景相关属性可以用于控制背景色.背景图片等属性.在控制背景图片的同时还可以控制背景图片的排列方式. 常 ...

  9. C/C++ OpenCV读取视频与调用摄像头

    原文:http://blog.csdn.net/qq78442761/article/details/54173104 OpenCV通过VideoCapture类,来对视频进行读取,调用摄像头 读取视 ...

  10. How to solve problems

    练习是为了帮助你成长 0.Don't panic! 1.What are the inputs? 2.What are the outputs? 3.Work through some example ...