mybatis传入参数类型parameterType详解
前言
Mybatis的Mapper文件中的select、insert、update、delete元素中都有一个parameterType属性,用于对应的mapper接口方法接受的参数类型。
( resultType:指定输出结果类型,mybatis将sql查询结果的一行记录数据映射为resultType指定类型的对象。如果有多条数据,则分别进行映射,并把对象放到容器List中。所以即使返回是list数组,resultType也是pojo类型 )
1. MyBatis的传入参数parameterType类型分两种
1. 1. 基本数据类型:int,string,long,Date;
1. 2. 复杂数据类型:类和Map
2. 如何获取参数中的值:
2.1 基本数据类型:#{value}或${value} 获取参数中的值
2.2 复杂数据类型:#{属性名}或${属性名} ,map中则是#{key}或${key}
注:#{}与${}的区别:
#{value}:输入参数的占位符,相当于jdbc的? 防注入 自动添加了‘ ’ 引号!
例如:select * from user where username = #{name} //输入的参数lisa,就会自动加上引号
变成:select * from user where username = ‘lisa’
注意:value可以写任意变量,没有统一规定
${value}: 不防注入,就是字符串拼接
例如:select * from user where username like '%${value}%' //默认不加引号
注意:只能写value!!!
select * FROM user WHERE username like "%"'s'"%"//是正确的,符合语法,引号的形式只能是这样,不能变!
3.案例:
3.1 基本数据类型案例
<sql id="Base_Column_List" >
id, car_dept_name, car_maker_name, icon,car_maker_py,hot_type
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
select
<include refid="Base_Column_List" />
from common_car_make
where id = #{id,jdbcType=BIGINT}
</select>
3.2 复杂类型--map类型
<select id="queryCarMakerList" resultMap="BaseResultMap" parameterType="java.util.Map">
select
<include refid="Base_Column_List" />
from common_car_make cm
where 1=1
<if test="id != null">
and cm.id = #{id,jdbcType=DECIMAL}
</if>
<if test="carDeptName != null">
and cm.car_dept_name = #{carDeptName,jdbcType=VARCHAR}
</if>
<if test="carMakerName != null">
and cm.car_maker_name = #{carMakerName,jdbcType=VARCHAR}
</if>
<if test="hotType != null" >
and cm.hot_type = #{hotType,jdbcType=BIGINT}
</if>
ORDER BY cm.id
</select>
3.3 复杂类型--类类型
<update id="updateByPrimaryKeySelective" parameterType="com.epeit.api.model.CommonCarMake" >
update common_car_make
<set>
<if test="carDeptName != null" >
car_dept_name = #{carDeptName,jdbcType=VARCHAR},
</if>
<if test="carMakerName != null" >
car_maker_name = #{carMakerName,jdbcType=VARCHAR},
</if>
<if test="icon != null" >
icon = #{icon,jdbcType=VARCHAR},
</if>
<if test="carMakerPy != null" >
car_maker_py = #{carMakerPy,jdbcType=VARCHAR},
</if>
<if test="hotType != null" >
hot_type = #{hotType,jdbcType=BIGINT},
</if>
</set>
where id = #{id,jdbcType=BIGINT}
</update>
3.4 复杂类型--map中包含数组的情况
<select id="selectProOrderByOrderId" resultType="com.epeit.api.model.ProOrder" parameterType="java.util.HashMap" >
select sum(pro_order_num) proOrderNum,product_id productId,promotion_id promotionId
from pro_order
where 1=1
<if test="orderIds != null">
and
<foreach collection="orderIds" item="item" open="order_id IN(" separator="," close=")">
#{item,jdbcType=BIGINT}
</foreach>
</if>
GROUP BY product_id,promotion_id
</select>
4. Mybatis (ParameterType) 如何传递多个不同类型的参数
4.1 方法一:不需要写parameterType参数
public List<XXXBean> getXXXBeanList(String xxId, String xxCode);
<select id="getXXXBeanList" resultType="XXBean">
select t.* from tableName where id = #{0} and name = #{1}
</select>
由于是多参数那么就不能使用parameterType, 改用#{index}是第几个就用第几个的索引,索引从0开始
4.2 方法二:基于注解(最简单)
public List<XXXBean> getXXXBeanList(@Param("id")String id, @Param("code")String code);
<select id="getXXXBeanList" resultType="XXBean">
select t.* from tableName where id = #{id} and name = #{code}
</select>
由于是多参数那么就不能使用parameterType, 这里用@Param来指定哪一个
4.3 方法三:Map封装
public List<XXXBean> getXXXBeanList(HashMap map);
<select id="getXXXBeanList" parameterType="hashmap" resultType="XXBean">
select 字段... from XXX where id=#{xxId} code = #{xxCode}
</select>
其中hashmap是mybatis自己配置好的直接使用就行。map中key的名字是那个就在#{}使用那个,map如何封装就不用了我说了吧。
4.4 方法四:List封装
public List<XXXBean> getXXXBeanList(List<String> list);
<select id="getXXXBeanList" resultType="XXBean">
select 字段... from XXX where id in
<foreach item="item" index="index" collection="list" open="(" separator=","close=")">
#{item}
</foreach>
</select>
---------------------
mybatis传入参数类型parameterType详解的更多相关文章
- mybatis传入参数类型parameterType和输出结果类型resultType详解
前言 Mybatis的Mapper文件中的select.insert.update.delete元素中都有一个parameterType和resultType属性,parameterType属性用于对 ...
- Mybatis传入参数类型为Map
mybatis更新sql语句: <update id="publishT00_notice" parameterType="Map"> update ...
- MyBatis传入参数与parameterType
参考:http://openwares.net/database/mybatis_parametertype.html Mybatis的Mapper文件中的select.insert.update.d ...
- MyBatis 传入参数之parameterType
在MyBatis的select,insert,update,delete这些元素中都提到了parameterType这个属性.MyBatis现在使用parameterType有基本类型和JAVA复 ...
- 《深入理解mybatis原理6》 MyBatis的一级缓存实现详解 及使用注意事项
<深入理解mybatis原理> MyBatis的一级缓存实现详解 及使用注意事项 0.写在前面 MyBatis是一个简单,小巧但功能非常强大的ORM开源框架,它的功能强大也体现在它的缓 ...
- Scala 深入浅出实战经典 第53讲:Scala中结构类型实战详解
王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-64讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...
- Quartz学习——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(转)
通过前面的学习,你可能大致了解了Quartz,本篇博文为你打开学习SSMM+Quartz的旅程!欢迎上车,开始美好的旅程! 本篇是在SSM框架基础上进行的. 参考文章: 1.Quartz学习——Qua ...
- 【转】@RequestParam @RequestBody @PathVariable 等参数绑定注解详解
@RequestParam @RequestBody @PathVariable 等参数绑定注解详解 2014-06-02 11:24 23683人阅读 评论(2) 收藏 举报 目录(?)[+] 引言 ...
- MyBatis传入参数为list、数组、map写法
1.foreach简单介绍: foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有item,index,collection,open,sep ...
随机推荐
- 基于SSH的J2EE Web系统配置文件
1. web.xml 关于Servlet Servlet是一个特殊的Java类,Servlet可以输出HTML标签作为表现层使用,但自从MVC规范出来以后,明确了Servlet的指责仅作为控制器使用, ...
- poj 1734 floyd求最小环,可得到环上的每个点
#include<stdio.h> #include<string.h> #define inf 100000000 #define N 110 #define min(a, ...
- SIM卡中UCS2编码的三种格式(80,81,82)分析
网上看到一篇比较好的说ucs2编码的文章,保存一下,原文地址: http://hi.baidu.com/youren4548/blog/item/fa08bd1bf61005058618bf1d.ht ...
- 1009在WINDOWS上面如何备份数据库
第一步 解决windows上面使用GZIP命令 参考http://www.xuebuyuan.com/1676976.html 后经研究,发现解决方法也很简单,只需下载gzip的windows版本,解 ...
- 关于double类型数字相加位数发生变化的问题
因为计算机内部存贮本身的缺陷,导致double类型的数字相加.得到的结果有非常多位,比方 774.23 750.0 2638.66 4162.889999999999 看到这个是不是非常晕 当然 ...
- 搭建单机CDH环境,并更新spark环境
搭建单机CDH环境,并更新spark环境 1,安装VMWare Player,http://dlsw.baidu.com/sw-search-sp/soft/90/13927/VMware_playe ...
- 公布IOS产品被拒后怎样再上传新公布包
问题描写叙述: 前两天提交公司产品2.0.7版本号到苹果审核,昨天提示被拒绝. 被拒原因就不描写叙述了. 我们经过改动后.又一次打包上传,但是怎么也提交不了.由于在苹果后台上已经存在2.0.7版本号的 ...
- ant 调用
博客园 首页 新随笔 联系 订阅 管理 [图文] 使用ant编译和发布java项目 开发JavaEE项目经常会碰到修改代码后,项目没有重新编译的问题.老大给指明了一个解决办法:用ant ...
- Leetcode:remove_element
一. 题目 给定一个数组和一个值.删除当中和给定值相等的元素.返回得到的新数组长度 二. 分析 刚開始我以为仅仅须要返回最后的数组长度即可了呢! 后来WA了一次才知道还得把心数组构造好 ...
- nginx源代码分析--nginx进程间通信
Linux下的IPC非常多,nginx的进程都是有亲缘关系的进程,对于他们的通信我们选择TCP socket进行通信. TCP socket 用来做进程通信的优点有, 1.socket是文件描 ...