• MyBatis ---- MyBatis获取参数值的两种方式(重点)


    MyBatis 获取参数值的两种方式:${}#{}

    ${}:本质就是字符串拼接
    #{}:本质就是占位符赋值
    ${} 使用字符串拼接的方式拼接 sql,若为字符串类型或日期类型的字符进行赋值时,需要手动加单引号
    #{} 使用占位符赋值的方式拼接 sql,此时为字符串类型或日期类型的字段进行赋值时,可以自动添加单引号

    1. 单个字面量类型的参数

        /**
         * 根据用户名查询用户信息
         * @param username
         * @return
         */
        User getUserByUsername(String username);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
        
        <select id="getUserByUsername" resultType="user">
            select * from t_user where username = #{username};
        select>
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

        
        <select id="getUserByUsername" resultType="user">
            select * from t_user where username = '${username}';
        select>
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    2. 多个字面量类型的参数

    若 mapper 接口中的方法参数为多个时
    此时 MyBatis 会自动将这些参数放在一个 map 集合中,以 arg0,arg1… 为键,以参数为值;以 param1,param2… 为键,以参数为值;因此只需要通过 ${}#{} 访问 map 集合的键就可以获取相对应的值,注意 ${} 需要手动加单引号

        /**
         * 检查用户是否登录成功
         * @param username
         * @param password
         * @return
         */
        User checkLogin(String username, String password);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
        
        <select id="checkLogin" resultType="user">
            select * from t_user where username = #{param1} and password = #{param2};
        select>
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

        
        <select id="checkLogin" resultType="user">
            select * from t_user where username = #{arg0} and password = #{arg1};
        select>
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    3. map集合类型的参数

    若 mapper 接口中的方法需要的参数为多个时,此时可以手动创建 map 集合,将这些数据放在 map 中只需要通过 ${}#{} 访问 map 集合的键就可以获取相对应的值,注意 ${} 要手动加单引号

        /**
         * 使用map结合作为参数检查用户是否登录成功
         * @param map
         * @return
         */
        User checkLoginByMap(Map<String, String> map);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
        
        <select id="checkLoginByMap" resultType="user">
            select * from t_user where username = #{username} and password = #{password};
        select>
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    4. 实体类类型的参数

    若 mapper 接口中的方法参数为实体类对象时
    此时可以使用 ${}#{},通过访问实体类独享中的属性名获取属性值,注意 ${} 需要手动加单引号

        /**
         * 使用实体类插入数据
         * @param user
         * @return
         */
        int insert(User user);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
        
        <insert id="insert">
            insert into t_user values(null, #{username}, #{password}, #{age}, #{sex}, #{email});
        insert>
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    5. 使用@Param标识参数

    可以通过 @Param 注解标识 mapper 接口中的方法参数
    此时,会将这些参数放在 map 集合中,以 @Param 注解的 value 属性值为键,以参数为值;以 param1,param2… 为键,以参数为值;只需要通过 ${}#{} 访问 map 集合就可以获取相应的值,注意 ${} 需要手动加单引号

        /**
         * 通过@Param注解验证用户登录
         * @param username
         * @param password
         * @return
         */
        User checkLoginByParam(@Param("username") String username, @Param("password") String password);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
        
        <select id="checkLoginByParam" resultType="user">
            select * from t_user where username = #{username} and password = #{password};
        select>
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

  • 相关阅读:
    服务器不稳定会造成什么影响
    图解LeetCode——1302. 层数最深叶子节点的和(难度:中等)
    在jar里限制指定的包名才可调用(白名单)。
    解密Socks5代理和代理IP:网络工程师的隐秘武器
    LLMs之Baichuan 2:《Baichuan 2: Open Large-scale Language Models》翻译与解读
    图像识别的进步:从单标签到多标签分类
    解读以下产生m个k位数的验证码的代码
    RTTI Internals
    游戏心理学Day26
    使用redux-thunk完成异步connect的第二个参数的对象写法。
  • 原文地址:https://blog.csdn.net/qq_52354698/article/details/127100267