MyBatis参数映射之@Param注解的用法解析
1、MyBatis的参数映射配置
MyBatis的参数映射利用的属性是:parameterType。这个属性是可选的,因为 MyBatis 可以通过类型处理器(TypeHandler)推断出具体传入语句的参数。
2、简单类型,不需要设置 parameterType
对于大多数简单的使用场景,你都不需要使用复杂的参数,比如:
<select id="selectUsers" resultType="User">
select id, username, password
from users
where id = #{id}
</select>
上面的这个示例说明了一个非常简单的命名参数映射。鉴于参数类型(parameterType)会被自动设置为 int,这个参数可以随意命名。
3、复杂类型,需要设置 parameterType
原始类型或简单数据类型(比如 Integer 和 String)因为没有其它属性,会用它们的值来作为参数,不需要设置 parameterType。 然而,如果传入一个复杂的对象,行为就会有点不一样了。比如:
<insert id="insertUser" parameterType="User">
insert into users (id, username, password)
values (#{id}, #{username}, #{password})
</insert>
此时需要 User 类型的参数对象传递到了语句中,会查找 id、username 和 password 属性,然后将它们的值传入预处理语句的参数中。
另外,还可以使用 map 传递,这是最原始的用法。定义接口使用map传递多个参数进行查询,如下所示:
public List<Product> getByMap(Map<String, Object> paramMap);
sql语句
<!--通过map传递参数 -->
<select id="selectUsers" resultType="User" parameterType="map">
select id, username, password
from users
where id = #{id} and age = #{age}
</select>
需要注意的有:
1、parameterType参数类型为map(此处使用别名)
2、参数名称是map中的key
4.1、复杂类型,不需要设置 parameterType ,使用
@Param注解
创建接口,使用注解传递多个参数进行查询
public List<Users> getUsers(@Param("name") String name, @Param("age") int age);
<select id="selectUsers" resultType="User">
select id, username, password
from users
where id = #{id} and age = #{age}
</select>
这种方式不需要设置参数类型 ,参数名称为注解定义的名称。这种方式能够大大提高可读性,但是只适合参数较少的情况,一般是少于5个用此方法
4.2、复杂类型,不需要
设置 parameterType ,使用
@Param注解
代码如下:
DAO 层 UserDao.java
package com.User.dao;
/**
* @description: 用户 DAO 接口
*/
@Mapper
public interface UserDao {
//批量新增用户
int batchInsert(@Param("records") List<UserVo> records);
}
DAO 层 UserMapper.xml
<insert id="batchInsert">
insert all
<foreach item="record" index="index" collection="records">
into t_user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="record.id != null">
id,
</if>
<if test="record.username != null">
username,
</if>
<if test="record.password != null">
password,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="record.id != null">
#{record.id},
</if>
<if test="record.username != null">
#{record.username},
</if>
<if test="record.password != null">
#{record.password},
</if>
</trim>
</foreach>
SELECT 1 FROM DUAL
</insert>
4.2、复杂类型,需要
设置 parameterType ,不使用
@Param注解
代码如下:
DAO 层 UserDao.java
package com.User.dao;
/**
* @description: 用户 DAO 接口
*/
@Mapper
public interface UserDao {
//批量新增用户 注意参数 records 到xml内不能写作 records
int batchInsert(List<UserVo> records);
}
DAO 层 UserMapper.xml
<insert id="batchInsert" parameterType="java.util.List">
insert all
<!--此处的collection="list"不能改写为collection="records"-->
<foreach item="record" index="index" collection="list">
into t_user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="record.id != null">
id,
</if>
<if test="record.username != null">
username,
</if>
<if test="record.password != null">
password,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="record.id != null">
#{record.id},
</if>
<if test="record.username != null">
#{record.username},
</if>
<if test="record.password != null">
#{record.password},
</if>
</trim>
</foreach>
SELECT 1 FROM DUAL
</insert>
注意
UserDao.java
中batchInsert
参数是 records
注意
UserMapper.xml
中batchInsert
中的遍历必须写作 collection="list
" 不能写作为 collection="records
"
5、小结
在不使用@Param注解的时候,函数的参数只能为一个,并且在查询语句取值时只能用#{}。如果想传递多个参数,parameterType参数类型为map(此处为别名)或者为JavaBean。
而使用@Param注解则可以使用多个参数,无需再设置parameterType,并且在查询语句中使用时可以使用#{}或者${}
原文链接:MyBatis中文官网
本文作者:Journey&Flower
本文链接:https://www.cnblogs.com/JourneyOfFlower/p/15160010.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步