org.apache.ibatis.binding.BindingException: Parameter 'XXX' not found.原因及解决方法

前言:出现这个问题的原因有好几个,所以我们逐步的来解释并解决问题;

1、首先,要明确一点的是,Dao层的抽象方法中的参数一般情况下默认的是一个参数或者一个对象;
例如:

  
  
  1. public interface StudentDao {
  2. int selectById(int id);
  3. int insert(Student stu);
  4. }

这两种是正常的方式,不会出现什么问题,mappper中的对应取值都是用#{}这种方式;
例如:

  
  
  1. <select id="selectById" resultMap="BaseResultMap">
  2. select
  3. <include refid="Base_Column_List" />
  4. from student
  5. where 1=1 and id = #{id}
  6. </select>

  
  
  1. <insert id="insert" parameterType="com.yoho.crm.dal.model.student"> <!--注意参数的类型要与对象类对应,也就是这个类的路径-->
  2. insert into inbox_template (name,age)
  3. values (#{name},#{age})
  4. </insert>

 上面如果是student对象作为参数,那么mapper中不能少了parameterType,否则会找不对应的属性 
 
2、当传多个参数时,就容易出现问题了,问题重现,如果像下面那样写就会出现标题中的错误,找不到参数;
 

  
  
  1. public interface StudentDao {
  2. int selectBySelective(int id,String name);
  3. }
解决办法就是在每个参数前加上@param注解,括号中注解的名称就是mapper中映射的值,如下:

  
  
  1. <code class="language-java">import org.apache.ibatis.annotations.Param;  
  2.   
  3. public interface StudentDao {  
  4.   
  5.       
  6.     int selectBySelective(@Param("id")int id,@Param("name")String name);  
  7.       
  8. }</code>  

   
   
  1. import org.apache.ibatis.annotations.Param;
  2. public interface StudentDao {
  3. int selectBySelective(@Param("id")int id,@Param("name")String name);
  4. }
mapper中还是那样写,可以使用${}或者#{}任意一种方式:

  
  
  1. <select id="selectBySelective" resultMap="BaseResultMap">
  2. select
  3. <include refid="Base_Column_List" />
  4. from student
  5. where name = #{pname} and id = #{pid}
  6. </select>
3、既有参数又有对象时,对象也需要注解,一般参数的直接取,对象的需要对象.属性,如下:

   
   
  1. public interface StudentDao {
  2. int selectBySelective(@Param("page")int page,@Param("stu")Student stu);
  3. }
mapper:

   
   
  1. <select id="selectBySelective" resultMap="BaseResultMap">
  2. select
  3. <include refid="Base_Column_List" />
  4. from student
  5. where name = #{stu.name} and id = #{stu.id} limit 0 ,#{page}
  6. </select>
4、像这种有公共的属性,例如分页都要传开始的index和pagesize,然后还要在传对象,就是3中的案例,可以为了方便管理,自己再新建一个类,里面封装了page的属性,同时又有对象,如下:
建一个page类:

    
    
  1. public class Page {
  2. private int pageNo;
  3. private int pageSize;
  4. private Object obj;
  5. public Page(int page,int pageSize,Object obj){
  6. this.pageNo=page;
  7. this.pageSize=pageSize;
  8. this.obj=obj;
  9. }
  10. public int getPageNo() {
  11. return pageNo;
  12. }
  13. public void setPageNo(int pageNo) {
  14. this.pageNo = pageNo;
  15. }
  16. public int getPageSize() {
  17. return pageSize;
  18. }
  19. public void setPageSize(int pageSize) {
  20. this.pageSize = pageSize;
  21. }
  22. public Object getObj() {
  23. return obj;
  24. }
  25. public void setObj(Object obj) {
  26. this.obj = obj;
  27. }
  28. }

在处理时直接new page对象中自己定义好的构造函数,然后直接把page作为参数传到dao:

    
    
  1. Student stu = new Student();
  2. stu.setName( "aa");
  3. stu.setId( 1);
  4. stu.setAge( 12);
  5. Page page = new Page( 1, 10,stu);

dao:


    
    
  1. public interface StudentDao {
  2. int update(Page page);
  3. }

mapper:

    
    
  1. <update id="update" parameterType="com.yoho.crm.dal.model.student"> <!--此时obj则是对应page中的属性了-->
  2. update inbox_template
  3. set
  4. name = #{obj.name},
  5. age = #{obj.age}
  6. where id = #{obj.id}
  7. </update>









转载地址:https://blog.csdn.net/qq_33142257/article/details/53202443
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值