Mybatis @Param注解使用总结

概述

平常使用@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

  • 9
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MyBatis是一款优秀的持久层框架,它对JDBC的操作数据库的过程进行了封装,使得开发者只需要关注SQL语句本身,而不需要花费精力去处理例如注册驱动、创建连接、创建Statement、手动设置参数、结果集检索等JDBC繁杂的过程代码。在MyBatis中,@Param注解用于给SQL语句中的参数命名,可以在SQL语句中直接引用该参数名,而不是使用默认的参数名。使用@Param注解可以避免因为参数名不同而导致的错误,同时也可以使得SQL语句更加清晰易懂。 举个例子,假设我们有一个查询用户信息的方法: ``` public User getUserById(int id, String name); ``` 如果我们使用MyBatis进行SQL映射,可以这样写: ``` <select id="getUserById" resultType="User"> select * from user where id = #{id} and name = #{name} </select> ``` 其中#{id}和#{name}就是使用@Param注解指定的参数名。在Java代码中,我们需要这样调用该方法: ``` User user = getUserById(1, "Tom"); ``` 这里的参数名与SQL语句中的参数名是一致的,因此MyBatis可以正确地将参数传递给SQL语句执行。如果我们不使用@Param注解,则需要这样写: ``` <select id="getUserById" resultType="User"> select * from user where id = #{arg0} and name = #{arg1} </select> ``` 这里的#{arg0}和#{arg1}是MyBatis默认的参数名,与Java代码中的参数名不一致,因此需要手动设置参数: ``` User user = getUserById(1, "Tom"); ``` 使用@Param注解可以避免这种手动设置参数的过程,使得代码更加简洁易懂。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

融极

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

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

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

打赏作者

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

抵扣说明:

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

余额充值