FAQ(16):Parameter 'userRole' not found. Available parameters are [UserRole, param1]

本文介绍了一个关于MyBatis中ResultMap的association使用时出现的参数找不到的问题,并给出了具体的错误日志及解决方法,主要原因是参数名的大小写不一致导致。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

场景:Mybatis-ResultMap的association的使用BUG,

看Log:

===============test add!
org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: org.apache.ibatis.binding.BindingException: Parameter 'userRole' not found. Available parameters are [UserRole, param1]
### Cause: org.apache.ibatis.binding.BindingException: Parameter 'userRole' not found. Available parameters are [UserRole, param1]
	at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:150)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
	at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:137)
	at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:75)
	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:53)
	at com.sun.proxy.$Proxy4.getUserListByRoleId(Unknown Source)
	at com.smbms.entities.UserTest.test11(UserTest.java:281)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.apache.ibatis.binding.BindingException: Parameter 'userRole' not found. Available parameters are [UserRole, param1]
	at org.apache.ibatis.binding.MapperMethod$ParamMap.get(MapperMethod.java:195)
	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.executor.BaseExecutor.createCacheKey(BaseExecutor.java:219)
	at org.apache.ibatis.executor.CachingExecutor.createCacheKey(CachingExecutor.java:146)
	at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:82)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148)
	... 29 more

解决:

      Parameter 'userRole' not found. 什么意思呢?不是数据库找不到这个参数!而是从test.java传递参数进mapper.xml时没有找到该参数!!原来是字母大小写没区分!!

原:

	public List<User> getUserListByRoleId (@Param("UserRole") Integer RoleId);


	----------------------------------------------------------------------------
	<select id="getUserListByRoleId"
		parameterType="Integer" resultMap="UserRoleResult">
		select u.* ,r.id as r_id,r.roleCode,r.roleName 
		   from smbms_user u,smbms_role r
		   where u.userRole=#{userRole} and u.userRole=r.id
	</select>

改:

	public List<User> getUserListByRoleId (@Param("UserRole") Integer RoleId);


	----------------------------------------------------------------------------
	<resultMap type="com.smbms.entities.User" id="UserRoleResult">
		<id property="id" column="id"/>
		<result property="userCode" column="userCode"/>
		<result property="userName" column="userName"/>
		<!-- -->
		<result property="userRole" column="userRole"/>	 
		<association property="role" javaType="com.smbms.entities.Role">
			<id property="id" column="r_id"/>
			<result property="roleCode" column="roleCode"/>
			<result property="roleName" column="roleName"/>
		</association>	
	</resultMap>
	<select id="getUserListByRoleId"
		parameterType="Integer" resultMap="UserRoleResult">
		select u.* ,r.id as r_id,r.roleCode,r.roleName 
		   from smbms_user u,smbms_role r
		   where u.userRole=#{UserRole} and u.userRole=r.id
	</select>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

后台技术汇

对你的帮助,是对我的最好鼓励。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值