<MyBatis>入门四 传入的参数处理
1.单个参数
传入单个参数时,mapper文件中 #{}里可以写任意值
/**
* 传入单个参数
*/
Employee getEmpById(Integer id);
<!--单个参数 #{} 里可以写任意值-->
<select id="getEmpById" resultType="org.maple.pojo.Employee">
SELECT id,last_name name,email,gender
FROM tbl_employee
WHERE id = #{abc}
</select>
2.多个参数
/**
* 传入多个参数
*/
Employee getEmpByIdAndName(Integer id,String name);
<select id="getEmpByIdAndName" resultType="org.maple.pojo.Employee">
SELECT id,last_name name,email,gender
FROM tbl_employee
WHERE id = #{id}
AND last_name = #{name}
</select>
此时会报错 Caused by: org.apache.ibatis.binding.BindingException:Parameter 'id' not found. Available parameters are [arg1, arg0, param1, param2]
传入多个参数时,mybatis会做特殊处理,
将多个参数封装成一个map,key是param1...paramN#{},从map中获取指定的key值
解决办法
1、使用param1..paramN,
<select id="getEmpByIdAndName" resultType="org.maple.pojo.Employee">
SELECT id,last_name name,email,gender
FROM tbl_employee
WHERE id = #{param1}
AND last_name = #{param2}
</select>
2.使用方法1,并不能见名知意,所以推荐第二种方法
命名参数,明确指定封装参数的map,
key:使用@Param注解指定的值
这样就可以通过#{}来取值
/**
* 传入多个参数
*/
Employee getEmpByIdAndGender(@Param("id") Integer id, @Param("gender") Character gender);
<select id="getEmpByIdAndGender" resultType="org.maple.pojo.Employee">
SELECT id,last_name name,email,gender
FROM tbl_employee
WHERE id = #{id}
AND gender = ${gender}
</select>
3 .传入pojo
如果多个参数正好是我们业务逻辑的数据模型,直接传入POJO,#{属性名}:取出POJO属性值
/**
* 传入pojo
*/
Employee getEmpByIdAndNamePojo(Employee employee);
<!--传入pojo对象-->
<select id="getEmpByIdAndNamePojo" resultType="org.maple.pojo.Employee">
SELECT id,last_name name,email,gender
FROM tbl_employee
WHERE id = #{id}
AND last_name = #{name}
</select>
4.传入Map
如果多个参数不是业务模型中的数据,没有对应的POJO,不经常使用,为了方便,我们传入map
/**
* 传入map
*/
Employee getEmpByMap(Map<String,Object> map);
<!--传入map-->
<select id="getEmpByMap" resultType="org.maple.pojo.Employee">
SELECT id,last_name name,email,gender
FROM tbl_employee
WHERE id = #{id}
AND last_name = #{name}
</select>
Map<String, Object> map = new HashMap<>();
map.put("id",1);
map.put("name","tom");
Employee emp = mapper.getEmpByMap(map);
5.传入TO(transfer Object)
多个参数不是业务中的模型,但是要经常使用,推荐写一个TO,数据传输对象。
6.思考

7.${},#{}区别
#{}:是以预编译的形式,将参数设置到sql语句中,PreparedStatement;防止sql注入
${}:取出的值直接拼装在sql语句中;会有安全问题
大多情况下,取参数使用#{}
比如分表,select * from ${year}_salary where xxx;
原生不支持占位符的:select * from tbl_employee order by ${name} ${order}
8.#{}的用法

<MyBatis>入门四 传入的参数处理的更多相关文章
- springMVC使用map接收入参 + mybatis使用map 传入查询参数
测试例子: controllel层 ,使用map接收请求参数,通过Debug可以看到,请求中的参数的值都是字符串形式,如果将这个接收参数的map直接传入service,mybatis接收参数时会报错, ...
- Mybatis框架四:输入参数、输出参数
输入参数可以有三种:简单类型,POJO,包装类 关于前两种: http://www.cnblogs.com/xuyiqing/p/8600888.html 这里写一下传递包装类参数: 一个POJO:U ...
- mybatis入门四 解决字段名与实体类属性名不相同的冲突
一.创建测试需要使用的表和数据 CREATE TABLE orders( order_id INT PRIMARY KEY AUTO_INCREMENT, order_no VARCHAR(20), ...
- MyBatis(四)多参数处理问题
这里总结了关于多参数传递时,MyBatis接收的三种方式. (1)接口中编写方法 public Emp getEmpByParams(Integer id,String lastNmae); publ ...
- Mybatis入门(四)配置别名(二)
这一章我们练习一下Mybatis的别名,这大大的提高了我们的开发效率 类型别名(typeAliases) 类型别名是为 Java 类型设置一个短的名字. 它只和 XML 配置有关,作用在于用来减少类完 ...
- Mybatis入门(四)配置优化(一)
这一章主要实验Mybatis的引入外部配置文件,属性(properties)这个属性都是可外部配置且可动态替换的,既可以在典型的 Java 属性文件中配置,亦可通过 properties 元素的子元素 ...
- 【转载】 mybatis入门系列四之动态SQL
mybatis 详解(五)------动态SQL 目录 1.动态SQL:if 语句 2.动态SQL:if+where 语句 3.动态SQL:if+set 语句 4.动态SQL:choose(when, ...
- mybatis入门系列二之输入与输出参数
mybatis入门系列二之详解输入与输出参数 基础知识 mybatis规定mapp.xml中每一个SQL语句形式上只能有一个@parameterType和一个@resultType 1. 返回 ...
- mybatis入门(四)
mybatis入门 需求:根据id查询用户的信息 mysql数据库: CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `us ...
随机推荐
- HDU 5976 Detachment 【贪心】 (2016ACM/ICPC亚洲区大连站)
Detachment Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- bzoj3550: [ONTAK2010]Vacation&&bzoj3112: [Zjoi2013]防守战线
学了下单纯形法解线性规划 看起来好像并不是特别难,第二个code有注释.我还有...*=-....这个不是特别懂 第一个是正常的,第二个是解对偶问题的 #include<cstdio> # ...
- ZOJ 1871:Steps
Steps Time Limit: 2 Seconds Memory Limit: 65536 KB One steps through integer points of the stra ...
- leetcode 784. Letter Case Permutation——所有BFS和DFS的题目本质上都可以抽象为tree,这样方便你写代码
Given a string S, we can transform every letter individually to be lowercase or uppercase to create ...
- 15_传智播客iOS视频教程_OC语言完全兼容C语言
OC支持C语言所有的运算符并且效果是一样的.C语言中所有的运算符OC都支持.这些所有的运算符OC当中全部都支持. 包括C语言的结构体.枚举全部都可以写在OC当中,没有任何问题,并且效果是一样的. 比如 ...
- 07-11 Linux命令操作
1. 查看当做操作目录位置 > pwd 2. 查看(当前)目录里边的文件内容 > ls //list > ls -l 或ll ...
- AtCoder Regular Contest 099 C~E
C - Minimization 枚举就可以了 因为最后一定会变成1,所以第一次操作的区间就包含1会比较优,然后枚举1在第一次操作区间里排第几个取min即可 #include<iostream& ...
- LoadRunner监控Linux配置教程
LoadRunner监控Linux资源时弹出如下错误: Monitor name :UNIX Resources. Cannot initialize the monitoring on 192.16 ...
- SpringBoot2.x版本整合SpringSecurity、Oauth2进行password认证
很多人在进行项目开发时都会用到Oauth2.0结合SpringSecurity或者Shiro进行权限拦截以及用户验证,网上也有很多的案例,前几天项目里边需要用到,顺便整合了进来,特此写篇博客,记录下过 ...
- sublime text 3 使用技巧
一.下载 官网下载合适的版本(http://www.sublimetext.com/3) 二.破解 执行 Help->Enter license 粘贴你的License代码 ----- BEGI ...