Mybatis @Param注解使用总结

"本文详细介绍了MyBatis中的@Param注解,包括何时使用、参数重命名、动态SQL及无需使用的情况。@Param用于指定Mapper方法参数在SQL中的名称,避免名称冲突。当参数名与SQL中的占位符不一致或多个参数时,需使用@Param。同时,文章提醒了使用#{}

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概述

平常使用@Param时会不会傻傻分不清楚,什么时候该用,什么时候不用呢?下面做一个总结,扫清相关障碍。

@Param说明

用来给mapper文件中方法的参数指定名称,能够让mybatis框架通过sql语句找到相应的参数。也就是说如果默认情况下,mybatis可以找到相关的参数,就无需使用@Param注解了。

mybaits如何查找sql参数

mybatis是通过参数名称来找到的对应的参数的,因此参数名称必须和sql中的引用名称一致。

必须使用@Param注解

重命名参数

下面列子展示了,参数personName被重命名为name,sql中通过name名称使用。

// 示例1
User selectUser(@param(“name”)String personName);
<select id=" selectUser" resultMap="BaseResultMap">  
   select  *  from user  where user_name = #{name} 
</select>
// 示例2
List<USer> select(@Param("id") Integer userId, @Param("name") String userName);
void insert(@Param("record") User user);
<select id="select" resultType="User">
  select * from users
  <where>
    <if test="id != null">and id = #{id}</if>
    <if test="name != null">and name = #{name}</if>
  </where>
</select>

<insert id="insert">
  insert into users (id, name) values
    (#{record.id}, #{record.name})
</insert>

使用${}方式引用参数

使用${},不会走sql预编译,只是字符串替换;这种方式不推荐,会引起sql注入问题。
示例

User selectUser(@param(“name”)String personName);
<select id=" selectUser" resultMap="BaseResultMap">  
   select  *  from user  where user_name = ${name} 
</select>

方法中有多个参数

Integer insert(@Param("username") String username, @Param("address") String address);
<insert id="insert">
    insert into user (username,address) values (#{username},#{address})
</insert>
int updateUser(@Param("user") User user, @Param("ages") List<Integer> ages);
<update id="updateUser">
        update t_user
            set  is_delete   = 1,
                <if test="user.name != null">
                    name = #{user.name},
                </if>
                 update_time = now()
        where is_delete = 0 and age in
        <foreach collection="ages" item="age" separator="," open="(" close=")">
            #{age}
        </foreach>
    </update>

动态sql中用作判断条件

即使一个参数,如果用作判断条件也要使用@Param注解
示例:

List<User> getUserById(@Param("id") Integer id);
<select id="getUserById" resultType="org.javaboy.helloboot.bean.User">
    select * from user
    <if test="id != null">
        where id = #{id}
    </if>
</select>

无需使用@Param注解

只有一个参数的时候才考虑不使用@Param注解。
通常我们只会在下面两种情况下不使用@Param注解,根据经验如果是Collect对象,还是要使用@Param注解的。

只有一个JavaBean参数

引用是直接用bean对象的属性即可。
如果使用@Param注解指定了名称,引用是必须加上名称才行(name.xxx)。
示例:

// 这里name是user的属性
Enchashment selectUserById(User user);
<select id=" selectUser" resultMap="BaseResultMap">  
   select  *  from user  where user_name = #{name} 
</select>

只有一个Map对象

示例:

UserInfo userInfo = new UserInfo();
Pagination page = new Pagination();
Map<String,Object> map = new HashMap<>;
map.put("userInfo",userInfo);
map.put("page",page);
userService.searchUser(map);
	List<UserInfo> searchUser(Map<String,Object> queryMap);
	<select id="searchUser" parameterType="java.util.Map" resultType="UserInfo">
		select *
		from t_userinfo user 
		where 1 =1
		<if test="userInfo.uname != null and ''!= userInfo.uname ">
			and user.uname like '%${userInfo.uname}$%'
		</if>
		<if test="page.order != null and page.order == 10" >
			order by user.id asc
		</if>
		limit #{page.pagenum * page.limitnum}, #{page.limitnum}
	</select>

参考

How to properly use the @Param annotation of Mybatis

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

融极

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值