解决注解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);

相关文章

可以认为OpenFeign是Feign的增强版,不同的是OpenFeign支持S...
为进一步规范小程序交易生态、提升用户购物体验、满足用户在...
云原生之使用Docker部署Dashdot服务器仪表盘
本文主要描述TensorFlow之回归模型的基本原理
1.漏洞描述Apache Druid 是一个集时间序列数据库、数据仓库和...
内部类(当作类中的一个普通成员变量,只不过此成员变量是cl...