【Mybatis学习笔记】占位符解析#{} ${}

获取参数#{} ${}

  • #{ }、${ }

  • #{ } 可以解决Sql注入问题 实质:占位符赋值

  • ${ } 不能解决Sql注入问题 实质:字符串拼接 ( 需要单引号括起来)

    • <select id="selectAll" resultType="User">
      	select * from user where userId = #{userId};
      </select>
      
      <select id="selectAll" resultType="User">
      	select * from user where userId = '${userId}';
      	//注意!!!${}需要单引号括起来,因为sql的字符串也是需要''的。
      	//为什么#{}不用?
      	因为  #{ }  实质:占位符赋值
      		 ${ }  实质:字符串拼接
      </select>
      
      

多个参数引出的问题

  • 默认占位符中的名称并不是与参数名称一致,只是 只有一个占位符时会自动赋值

    • eg:

    • java中:
      List<User> selectById(int Id){
      		xxxx...
      		mapper = sqlSessin.getMapper(..);
      		mapper.selectById(Id)
      		//这里并不是一定传入参数名称为Id的!
      		mapper.selectById(1);
      		
      		int abc = 4;
      		mapper.selectById(abc);
      		//这样都是可以的 但只针对 传参数量为一的情况
      	
      }
      
      .xml中:
      <select id="selectById" resultType="User">
       	select * from user where userId = #{userId};
      </select>
       
      
      
  • 当多个参数传参时,如果按照占位符名称传参 会依然出错

    • 按照下面这个例子,会报错:当前,没办法找到userId 和username参数,可用参数只有:{ar0,arg1… param1,param2…}

    • 真实报错信息:Mybatis Available parameters are [0, 1, param1, param2]

    • java中:
      List<User> selectById(int Id,String username){
      		xxxx...
      		mapper = sqlSessin.getMapper(..);
      		mapper.select (Id, username)
      		//报错!!!!!!
      	
      }
      
      .xml中:
      <select id="select" resultType="User">
       	select * from user where userId = #{userId} and username=#{username};
      </select>
      
    • 这是因为底层Mybatis使用匹配的数据结构的是 Map集合,默认的键为arg…param…

      • ①解决方法:

        • .java中:
          	 
           List<User> selectById(int Id,String username){
           		xxxx...
           		mapper = sqlSessin.getMapper(..);
           		mapper.select (Id, username)
           		//报错!!!!!!
           	
           }
           
           .xml中:
           <select id="select" resultType="User">
          	select * from user where userId = #{ar0} and username=#{arg1};
          </select>
          ------------------------
          //当然,下面这些都是可以的 
          <select id="select" resultType="User">
          	select * from user where userId = #{ar0} and username=#{param1};
          </select>
          
          <select id="select" resultType="User">
          	select * from user where userId = #{ar2} and username=#{param0};
          </select>
          
          
      • ②解决办法:

        • 第一种方法显得麻烦,因为参数**无法见名思意 **(**会被同事打死!**保命方法如下)

        • 传入Map集合(达到真正的参数名称与占位符名称必须一致!)

          • .java中:
            List<User> selectById(int Id,String username){
            		xxxx...
            		mapper = sqlSessin.getMapper(..);
            		Map<String,Object> map = new HashMap<>();
            		map.put("Id",Id);
            		map.put("username",username);
            		mapper.select (map)
            	//解决参数名称与占位符名称不一致问题!又可以和同事愉快的玩耍啦!
            }
            
            .xml中:
            <select id="select" resultType="User">
             	select * from user where userId = #{Id} and username=#{username};
            
            
            
      • ③解决办法

        • 依然麻烦,还要创建一个Map集合,有没有更简单的方法!(有!)

        • 使用类对象进行传参 ,Mybatis很智能!会自动与类对象的所需名称绑定

        • 重点!Mybatis并不是与成员变量进行绑定!而是与类对象的Get、Set方法后面的参数名称首字母转换小写后的名称绑定

          • 举个例子:getUserName、setUserName 转换后绑定的名称 userName
        • // java中:
          class User{
          	private int id;
          	private String userName;
             
              getUserName(){...}
              setUserNmae(){...}
              getId(){...}
              setId(){...}
           
          }
          	 
          	 
           List<User> selectByUser(User user){
           		xxxx...
           		mapper = sqlSessin.getMapper(..); 
           		mapper.selectById (user)
           	//又解决参数名称与占位符名称不一致问题!依然可以和同事愉快的玩耍啦!
           }
           
           //.xml中:
           <select id="selectByUser" resultType="User">
          	select * from user where userId = #{id} and username=#{userName};
          
          
          
          
      • ④解决方法

        • 利用@Param注解中value属性名称与占位符名称直接绑定!

          • 小提醒:注解只写一个值,默认赋值value参数
        • 举例

          • // java中:
             List<User> select(String passWord,String userName){
             		xxxx...
             		mapper = sqlSessin.getMapper(..); 
             		mapper.selectById (
                        @Param(password) passWord,
                        @Param(username) userName
                    )
             	//又又又解决参数名称与占位符名称不一致问题!依旧可以和同事愉快的玩耍啦!
             }
             
            // .xml中:
             <select id="select" resultType="User">
            	select * from user 
                 where password = #{password} and username=#{username};
            </select>
            
            
            
  • @Param注解源码小解析

    • @Param 底层会以两种方式存储我们当前的值,一种是我们指定的名称作key,另一种是以param… 的方式
      -在这里插入图片描述

      • 但我不知道mybatis为什么要这么做…为了兼容吗?坐等大佬解析!!
  • 总结

    • 实际中常用的是第四种和第三种
    • 前两种只需要知道就行!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值