关于mybatis的Parameter ‘userid‘ not found. Available parameters are [arg1, arg0, param1, param2]的解决方案

一、异常的内容

调用的映射方法:

    @Insert("insert into user_role(userId,roleId) values(#{userId},#{id})")
    void saveUserWithRole( String userId,  String id) throws MyException;

出现的异常:

Caused by: org.apache.ibatis.binding.BindingException: Parameter 'userId' not found. Available parameters are [arg1, arg0, param1, param2]
	at org.apache.ibatis.binding.MapperMethod$ParamMap.get(MapperMethod.java:202)
	at org.apache.ibatis.reflection.wrapper.MapWrapper.get(MapWrapper.java:45)
	at org.apache.ibatis.reflection.MetaObject.getValue(MetaObject.java:122)
	at org.apache.ibatis.scripting.defaults.DefaultParameterHandler.setParameters(DefaultParameterHandler.java:79)
	at org.apache.ibatis.executor.statement.PreparedStatementHandler.parameterize(PreparedStatementHandler.java:93)
	at org.apache.ibatis.executor.statement.RoutingStatementHandler.parameterize(RoutingStatementHandler.java:64)
	at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:86)
	at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:49)
	at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117)
	at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:63)
	at com.sun.proxy.$Proxy121.update(Unknown Source)

二、解决方案

这是由于出入多参,sql语句中的取参结果成了这样 userId.userId,userId.roleId,所以需要定义一下sql语句中引入的参数对应方法中的哪个参数,修改如下:

	//用mybatis提供的@Param注解,定义sql语句中的对应参数
    @Insert("insert into user_role(userId,roleId) values(#{userId},#{otherroleId})")
    void saveUserWithRole( @Param("userId")String userId,  @Param("otherroleId")String id) throws MyException;

猜你喜欢

转载自blog.csdn.net/weixin_42717117/article/details/119672081
今日推荐