1. 程式人生 > >Parameter 'xx' not found. Available parameters are [0, 1, 2, 3, param3, param4, param1, param2]

Parameter 'xx' not found. Available parameters are [0, 1, 2, 3, param3, param4, param1, param2]

報錯描述:

2017-03-30 16:37:11,177 ERROR [500.jsp] - nested exception is org.apache.ibatis.binding.BindingException: Parameter 'userId' not found. Available parameters are [0, 1, 2, 3, param3, param4, param1, param2]
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'userId' not found. Available parameters are [0, 1, 2, 3, param3, param4, param1, param2]
	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:76)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:399)
	at com.sun.proxy.$Proxy39.selectOne(Unknown Source)
	at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:165)
	at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:68)
	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:52)
	at com.sun.proxy.$Proxy117.getSingleRecord(Unknown Source)
	at com.lczyfz.istep.modules.teach.service.TeachDistributeService.duplicateCourse(TeachDistributeService.java:227)
	at com.lczyfz.istep.modules.teach.service.TeachDistributeService.saveDistribute(TeachDistributeService.java:139)
	at com.lczyfz.istep.modules.teach.service.TeachDistributeService$$FastClassBySpringCGLIB$$3587a48.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:718)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
	at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
	at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
	at com.lczyfz.istep.modules.teach.service.TeachDistributeService$$EnhancerBySpringCGLIB$$5c07bb4f.saveDistribute(<generated>)
	at com.lczyfz.istep.modules.teach.service.TeachDistributeService$$FastClassBySpringCGLIB$$3587a48.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:718)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
	at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
	at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)


        問題原因:ibatis.binding.BindingException,dao裡面的引數傳到mapping無法一一繫結。

我的dao是這樣寫的:

/**
 * 課程計數表DAO介面
 * @author cqh
 * @version 2017-03-28
 */
@MyBatisDao
public interface TeachCourseCountDao extends CrudDao<TeachCourseCount> {

    //根據distributeId、userId、CourseSystemId、CourseId獲取單條記錄
    TeachCourseCount getSingleRecord(String userId, String coursesystemId, String CourseId, String DEL_FLAG_NORMAL);
}
mapping是這樣寫的:
<!--根據distributeId、userId、CourseSystemId、CourseId獲取單條記錄-->
	<select id="getSingleRecord" resultType="TeachCourseCount">
		SELECT
		<include refid="teachCourseCountColumns"/>
		FROM teach_courseCount a
		WHERE
		a.user_id = #{userId}
		AND a.coursesystem_id = #{coursesystemId}
		AND a.course_id = #{CourseId}
		AND a.del_flag = #{DEL_FLAG_NORMAL}
	</select>

解決辦法:

方法一:修改mapping。將引數以0、1、2...的順序從dao傳到mapping,在mapping以{0}、{1}...的方式 接收引數。結果如下:

<!--根據distributeId、userId、CourseSystemId、CourseId獲取單條記錄-->
	<select id="getSingleRecord" resultType="TeachCourseCount">
		SELECT
		<include refid="teachCourseCountColumns"/>
		FROM teach_courseCount a
		WHERE
		a.user_id = #{0}
		AND a.coursesystem_id = #{1}
		AND a.course_id = #{2}
		AND a.del_flag = #{3}
	</select>

方法二:修改dao。引入註解@Param。結果如下:
import com.lczyfz.istep.common.persistence.CrudDao;
import com.lczyfz.istep.common.persistence.annotation.MyBatisDao;
import com.lczyfz.istep.modules.teach.entity.TeachCourseCount;
import org.apache.ibatis.annotations.Param;

/**
 * 課程計數表DAO介面
 * @author cqh
 * @version 2017-03-28
 */
@MyBatisDao
public interface TeachCourseCountDao extends CrudDao<TeachCourseCount> {

    //根據distributeId、userId、CourseSystemId、CourseId獲取單條記錄
    TeachCourseCount getSingleRecord(@Param("userId")String userId, @Param("coursesystemId")String coursesystemId,
                                     @Param("CourseId")String CourseId, @Param("DEL_FLAG_NORMAL")String DEL_FLAG_NORMAL);

}

以上兩種方式均可解決,任選其一即可。