【已解决】关于MyBatis参数传入#{index}的问题的解决方案


一、在测试update时,传入多个参数,进行测试,发现一直报:

org.apache.ibatis.exceptions.PersistenceException: 
### Error updating database.  Cause: org.apache.ibatis.binding.BindingException: 
Parameter '0' not found. Available parameters are [arg1, arg0, param1, param2]
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: update students set name=? where stud_id=?
### Cause: org.apache.ibatis.binding.BindingException: 
Parameter '0' not found. Available parameters are [arg1, arg0, param1, param2]


二、还原代码情景

1)、接口类StudentManager.java
public interface StudentMapper {
    void updateStudentName(String name ,String id);
}
2)、映射文件StudentMapper.xml
<update id="updateStudentName">
	update students set name=#{0} where stud_id=#{1}
</update> 

PS:顺序#{0}和Mapper接口中的参数顺序一致

3)、测试代码
@Test
public void updateStudentName() {
	SqlSession sqlSession = MyBatisSqlSessionFactory.openSession();
	try {
		StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
		studentMapper.updateStudentName("大牛",2);
		sqlSession.commit();//提交,事务管理
	} finally {
		sqlSession.close();
	}
}


三、参考了多个度贴,内容基本一样,结果无法



四、解决方案

4.1 研究错误异常
org.apache.ibatis.exceptions.PersistenceException: 
### Error updating database.  Cause: org.apache.ibatis.binding.BindingException: 
Parameter '0' not found. Available parameters are [arg1, arg0, param1, param2]
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: update students set name=? where stud_id=?
### Cause: org.apache.ibatis.binding.BindingException: 
Parameter '0' not found. Available parameters are [arg1, arg0, param1, param2]
其中
Available parameters are [arg1, arg0, param1, param2]

含义是有效的参数是[arg1,arg0,param1,param2]

4.2 实质系统是通过map的方式进行自动绑定

其map对应的key就是上面的这些值[arg1,arg0,param1,param2]

代码中可以通过这些名字进行引用使用

<update id="updateStudentName">
	update students set name=#{arg0} where stud_id=#{arg1}
</update> 

<update id="updateStudentName">
	update students set name=#{param1} where stud_id=#{param2}
</update> 
弊端:魔术参数名!arg0,arg1,param1,param2


五、其他解决办法

5.1 在每个参数处设置@param("参数名")
public interface StudentMapper {
	void updateStudentName(@param("name1") String name,@param("id1") String id);
}
5.2 在mapper.xml中使用设置的参数名
<update id="updateStudentName">
	update students set name=#{name1} where stud_id=#{id1}
</update> 
5.3 此时数据正常,可以正常执行,参数名也很舒服


六、源码sql脚本

上传资源时候忘了传,补上内容。

DROP TABLE IF EXISTS `students`;
CREATE TABLE `students` (
  `stud_id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `email` varchar(50) NOT NULL,
  `dob` date DEFAULT NULL,
  PRIMARY KEY (`stud_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

ps:源码下载 https://download.csdn.net/download/u011698550/10430282

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值