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 ...
随机推荐
- Go.网络篇-2
package main import ( "io/ioutil" "os" "io" "log" "net/ ...
- 【Hadoop系列】linux下 root用户免密码登录远程主机 ssh
SSH原理:[Hadoop系列]linux SSH原理解析 操作环境: CentOS 6.5 操作对象: 用户A主机和远程主机B 正文部分:斜体加粗代表linux指令. linux下 非root用户免 ...
- django通用分页封装
__author__ = 'Administrator'from django.utils.safestring import mark_safe class Page: def __init_ ...
- springboot遇到的那些坑
一.在springboot整合jsp时,程序中的所有配置都是正确的,但是在启动springboot后,访问无法找到jsp页面,报错404, 解决办法 二.在springboot整合jpa实现crud时 ...
- What is the relation of theme and it's derived theme.
You know, a theme can derive from other theme in two ways: xx.xxx implicit way and parent="xxx& ...
- Python之异常设计(一)
一 定义 异常分为两类:一类是自动触发异常如除零错误:另一类是通过raise触发. 二 为什么要使用异常 当程序运行时,如果检测到程序错误,Python就会引发异常,我们可以在程序中使用try语句捕获 ...
- 第八章.Java集合
Java集合类是一种特别有用的工具类,可用于存储数量不等的对象.Java集合大致可分为Set.List.Queue和Map四种体系 Set代表无序.不可重复的集合 List代表有序.重复的集合 Map ...
- python对excel文件的读写操作
import xlrd,xlwt data = xlrd.open_workbook('a.xlsx') #读 table = data.sheets()[0] data_list = [] data ...
- mysql case when & concat & SUBSTRING_INDEX & not & having 使用的小case
1. 代码 SELECT a.id, a.activity_name, ( CASE WHEN a.activity_end_time > now() THEN '参与中' ELSE ( CAS ...
- URL传递中文:Server.UrlEncode与Server.UrlDecode
1.设置web.config文件. <system.web> ...... <globalization requestEncoding="gb2312" r ...