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 ...
随机推荐
- 使用requirejs模块化开发多页面一个入口js的使用方式
描述 知道requirejs的都知道,每一个页面需要进行模块化开发都得有一个入口js文件进行模块配置.但是现在就有一个很尴尬的问题,如果页面很多的话,那么这个data-main对应的入口文件就会很多. ...
- Feign 负载均衡
一.是什么 Feign 是一个声明式 WebService 客户端.使用 Feign 能让编写 Web Service 客户端更加简单,他的使用方法是定义一个接口,然后在上面添加注解.同时也支持 JA ...
- Summary of Memory Management Methods
Summary of Memory Management Methods Table 18-1 summarizes the various memory management methods. If ...
- 数位dp无前导零
题目链接:http:// www.lydsy.com/JudgeOnline/problem.php?id=1026 #include <iostream> #include < ...
- Redis容量及利用计划
在利用Redis过程当中,咱们发明了很多Redis分歧于Memcached,也差别于MySQL的特点.(本文首要会商Redis未启用VM撑持环境) 1. Schema MySQL: 需事先设计Memc ...
- 洛谷 P2121 拆地毯
P2121 拆地毯 题目背景 还记得 NOIP 2011 提高组 Day1 中的铺地毯吗?时光飞逝,光阴荏苒,三年过去了.组织者精心准备的颁奖典礼早已结束,留下的则是被人们踩过的地毯.请你来解决类似于 ...
- android发送get请求时报错
异常信息: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.synology.synologycloud/ ...
- 18124 N皇后问题
18124 N皇后问题 时间限制:2000MS 内存限制:65535K提交次数:0 通过次数:0 题型: 编程题 语言: G++;GCC;VC Description 有N*N的国际象棋棋盘,要 ...
- tomcat下载及启动
http://tomcat.apache.org/ 打开网页,在左边选择版本,选择后网页往下面拉 拉下来,根据windows选择32还是64位的,其中zip是windows免安装版 下载后解压,然后配 ...
- KVO---视图间数据的传递:标签显示输入的内容【多个视图中】
RootViewController.m #import "ModalViewController.h" @interface RootViewController () @end ...