Mybatis @Param注解使用总结

导读:本篇文章讲解 Mybatis @Param注解使用总结,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

概述

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/100117.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!