微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

解决注解Mybatis注入多个参数时报错

解决注解Mybatis注入多个参数时报错

问题:当使用注解Mybatis注入多个参数时,报错

@Insert("insert into user_role values(#{uid},#{rid})")
void saveUserRole(int uid, int rid);

报错信息:

org.apache.ibatis.binding.BindingException: Parameter 'uid' not found. Available parameters are [arg1, arg0, param1, param2]
		at org.apache.ibatis.binding.MapperMethod$ParamMap.get(MapperMethod.java:212)
		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:94)
		at org.apache.ibatis.executor.statement.RoutingStatementHandler.parameterize(RoutingStatementHandler.java:64)
		at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:88)
		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 org.apache.ibatis.session.defaults.DefaultsqlSession.update(DefaultsqlSession.java:197)
		at org.apache.ibatis.session.defaults.DefaultsqlSession.insert(DefaultsqlSession.java:184)
		at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
		at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
		at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
		at java.base/java.lang.reflect.Method.invoke(Method.java:567)
		at org.mybatis.spring.sqlSessionTemplate$sqlSessionInterceptor.invoke(sqlSessionTemplate.java:427)
		at jdk.proxy3/jdk.proxy3.$Proxy30.insert(UnkNown Source)
		at org.mybatis.spring.sqlSessionTemplate.insert(sqlSessionTemplate.java:272)
		at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:62)
		at org.apache.ibatis.binding.MapperProxy$PlainMethodInvoker.invoke(MapperProxy.java:152)
		at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:85)
		at jdk.proxy3/jdk.proxy3.$Proxy37.saveUserRole(UnkNown Source)
		at com.one.service.UserServiceImpl.save(UserServiceImpl.java:31)
		at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
		at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
		at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
		at java.base/java.lang.reflect.Method.invoke(Method.java:567)

原因:Mybatis注解认只能注入一个基本类型或对象,多个参数注入时无法将参数值和参数名匹配

解决方法:使用@parm("参数名")注入数据

@Insert("insert into user_role values(#{uid},#{rid})")
void saveUserRole(@Param("uid")int uid, @Param("rid")int rid);
userMapper.saveUserRole(user.getId(),rid);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐