parameterType传入参数类型
Mybatis的Mapper文件中的select、insert、update、delete元素中都有一个parameterType属性,用于对应的mapper接口方法接受的参数类型。
parameterType (不是必须)如果不指定,自动识别,指的就是传入的参数
MyBatis的传入参数parameterType类型分两种:
- 简单数据类型:int、string、long、Date;
- 复杂数据类型:类(JavaBean、Integer等)和Map
如何获取参数中的值:
- 简单数据类型:#{参数} 获取参数中的值
- 复杂数据类型:#{属性名} ,map中则是#{key}
传入Long型
mapper接口代码:
public User findUserById(Long id);
xml代码:
<select id="findUserById" parameterType="java.lang.Long" resultType="User">
select * from user where id = #{id};
</select>
传入List
mapper接口代码:
public List<User> findUserListByIdList(List<Long> idList);
xml代码:
<select id="findUserListByIdList" parameterType="java.util.List" 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种情况:
- 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
- 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
- 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可
传入数组
mapper接口代码:
public List<User> findUserListByIdList(int[] ids);
xml代码:
<select id="findUserListByIdList" parameterType="java.util.List" resultType="User">
select * from user user
<where>
user.id in (
<foreach collection="array" item="id" index="index" separator=",">
#{id}
</foreach>
)
</where>
</select>
传入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时不太一样。
传入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时不太一样。
注解@Param
① 注解单一属性;这个类似于将参数重命名了一次
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>
② 注解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>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!