MyBatis的parameterType传入参数类型
在mybatis映射接口的配置中,有select,insert,update,delete等元素都提到了parameterType的用法,parameterType为输入参数,在配置的时候,配置相应的输入参数类型即可。parameterType有基本数据类型和复杂的数据类型配置。
1. MyBatis的传入参数parameterType类型分两种
1. 1. 基本数据类型:int、string、long、Date;
1. 2. 复杂数据类型:类(JavaBean、Integer等)和Map
2. 如何获取参数中的值:
2.1 基本数据类型:#{参数} 获取参数中的值
2.2 复杂数据类型:#{属性名} ,map中则是#{key}
3.案例:
3.1 传入Long型
mapper接口代码:
public User findUserById(Long id);
xml代码:
<select id="findUserById" parameterType="java.lang.Long" resultType="User">
select * from user where id = #{id};
</select>
3.2 传入List
mapper接口代码:
public List<User> findUserListByIdList(List<Long> idList);
xml代码:
<select id="findUserListByIdList" parameterType="java.util.ArrayList" resultType="User">
select * from user user
<where>
user.ID in (
<foreach collection="list" item="id" index="index" separator=",">
#{id}
</foreach>
)
</where>
</select>
在使用foreach的时候最关键的也是最容易出错的就是collection属性。
该属性是必须指定的,但是在不同情况 下,该属性的值是不一样的,主要有一下3种情况:
1. 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
2. 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
3. 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可
3.3 传入数组:
mapper接口代码:
public List<User> findUserListByIdList(int[] ids);
xml代码:
<select id="findUserListByIdList" parameterType="java.util.HashList" resultType="User">
select * from user user
<where>
user.ID in (
<foreach collection="array" item="id" index="index" separator=",">
#{id}
</foreach>
)
</where>
</select>
3.4 传入map
mapper接口代码:
public boolean exists(Map<String, Object> map);
xml代码:
<select id="exists" parameterType="java.util.HashMap" resultType="java.lang.Integer">
SELECT COUNT(*) FROM USER user
<where>
<if test="code != null">
and user.CODE = #{code}
</if>
<if test="id != null">
and user.ID = #{id}
</if>
<if test="idList !=null ">
and user.ID in (
<foreach collection="idList" item="id" index="index" separator=",">
#{id}
</foreach>
)
</if>
</where>
</select>
MAP中有list或array时,foreach中的collection必须是具体list或array的变量名。
比如这里MAP含有一个名为idList的list,所以MAP中用idList取值,这点和单独传list或array时不太一样。
3.5传入JAVA对象
mapper接口代码:
public int findUserList(User user);
xml代码:
<select id="findUserList" parameterType="User" resultType="java.lang.Integer">
SELECT COUNT(*) FROM USER user
<where>
<if test="code != null">
and user.CODE = #{code}
</if>
<if test="id != null">
and user.ID = #{id}
</if>
<if test="idList !=null ">
and user.ID in (
<foreach collection="idList" item="id" index="index" separator=",">
#{id}
</foreach>
)
</if>
</where>
</select>
JAVA对象中有list或array时,foreach中的collection必须是具体list或array的变量名。
比如这里User含有一个名为idList的list,所以User中用idList取值,这点和单独传list或array时不太一样。
3.6注解@Param
例子1:
注解单一属性;这个类似于将参数重命名了一次
mapper接口代码:
List<User> selectUserByTime(@Param(value="startdate")String startDate);
xml代码:
<select id="selectUserByTime" resultType="User" parameterType="java.lang.String">
select * from t_user
where create_time >= to_date(#{startdate,jdbcType=VARCHAR},'YYYY-MM-DD')
</select>
例子2:
注解javaBean
mapper接口代码:
List<User> selectUserByTime(@Param(value="dateVO")DateVO dateVO);
xml代码:
<select id="selectUserByTime" resultType="User" parameterType="DateVO">
select *
from t_user
where create_time >= to_date(#{dateVO.startDate,jdbcType=VARCHAR},'YYYY-MM-DD')
and create_time < to_date(#{dateVO.endDate,jdbcType=VARCHAR},'YYYY-MM-DD')
</select>
---------------------
作者:Remember_Ray 来源:
CSDN 原文:https://blog.csdn.net/q343509740/article/details/80578832
MyBatis的parameterType传入参数类型的更多相关文章
- mybatis传入参数类型parameterType和输出结果类型resultType详解
前言 Mybatis的Mapper文件中的select.insert.update.delete元素中都有一个parameterType和resultType属性,parameterType属性用于对 ...
- mybatis传入参数类型parameterType详解
前言 Mybatis的Mapper文件中的select.insert.update.delete元素中都有一个parameterType属性,用于对应的mapper接口方法接受的参数类型. ( res ...
- Mybatis传入参数类型为Map
mybatis更新sql语句: <update id="publishT00_notice" parameterType="Map"> update ...
- Mybatis的parameterType传入多个参数
如果查询的条件有多个的时候,mybatis有三种传入方式: 1.通过注解传入 例如: public interface Mapper(){ public User login(@Param(" ...
- mybatis 框架动态传入参数${}和#{}之间的区别
动态SQL是mybatis的强大特性之一,mybatis在对sql语句进行预编译之前,会对sql进行动态解析,解析为一个BoundSql对象,也是在此处对动态sql进行处理.下面让我们先来熟悉下myb ...
- mybatis sql in 查询(mybatis sql语句传入参数是list)mybatis中使用in查询时in怎么接收值
1.in查询条件是list时 <select id="getMultiMomentsCommentsCounts" resultType="int"> ...
- MyBatis中foreach传入参数为数组
一.当只有一个参数,并且这个参数是数组时 接口方法的参数不需要添加@Param注释,collection="array" 示例 接口的方法 void deleteMulti(Str ...
- mybatis 之 parameterType="HashMap"参数包含list
/** * 获得人气组合商品详情 * @param paramMap * @return */ public List<Goods> getCheckGoodsCombination(Ma ...
- 关于Mybatis中mapper.xml的传入参数简单技巧
由于在做项目的时候,我看见同事使用的传入参数类型各式各样,感觉没规律可言,闲暇的时候我就自己搭建了项目做了一些传入参数的测试(当然其实更好的方式是看源码,但是博主能力有限,毕竟入行没多久,看起来很吃力 ...
随机推荐
- [USACO10FEB]给巧克力Chocolate Giving
题意简叙: FarmerFarmerFarmer JohnJohnJohn有B头奶牛(1<=B<=25000)(1<=B<=25000)(1<=B<=25000), ...
- 洛谷P2265 路边的水沟
题目 题目背景 LYQ市有一个巨大的水沟网络,可以近似看成一个n*m的矩形网格,网格的每个格点都安装了闸门,我们将从水沟网络右下角的闸门到左上角的闸门的一条路径称为水流. 题目描述 现给定水沟网的长和 ...
- SSM 框架集成
1.SSM是什么? SSM是指目前最主流的项目架构的三大框架: SpringMVC : spring的 Web层框架,是spring的一个模块 Spring :容器框架 MyBatis :持久层框架 ...
- 在安装Openstack的keystone认证服务时,出现The request you have made requires authentication. (HTTP 401) (Request-ID: req-f94bebba-f0c5-4a92-85问题的处理
创建openstack的keystone认证服务器报错: The request you have made requires authentication. (HTTP 401) (Reques ...
- 【git】Git的使用
一.安装git 1.windows下安装一个Git 2.lInux下yum(apt-get) install git 二.使用git连接github 使用git连接github时,需要将linux下产 ...
- 《VR入门系列教程》之7---DK2和Crescent Bay
The DK2 于2014年春,Oculus发布了第二代开发版头显设备,代号为DK2.与DK1相比,Oculus Rift DK2的外观有很大改进,并且轻了许多,体积仍然比较大,可以罩住大部分 ...
- 深挖Openstack Nova - Scheduler调度策略
深挖Openstack Nova - Scheduler调度策略 一. Scheduler的作用就是在创建实例(instance)时,为实例选择出合适的主机(host).这个过程分两步:过滤(F ...
- Centos7 安装Homestead环境2
历史命令, -- :: cd /etc/yum.repos.d/ -- :: wget http://download.virtualbox.org/virtualbox/rpm/rhel/virtu ...
- Python秒算24点,行还是不行?
周末闲来无事,看到隔壁家的老王在和隔壁家的媳妇玩24点,就进屋看了看.发现老王是真不行啊,那不行,这也不行. 就连个24点都玩不过他媳妇,给他媳妇气的,啥都不能满足,这不能,那也不能. 我坐下来和他媳 ...
- 第二章 javaScript操作BOM
什么是BOM BOM(Browser Object Model)即浏览器对象模型. BOM提供了独立于内容 而与浏览器窗口进行交互的对象: 由于BOM主要用于管理窗口与窗 ...