mybatis参数绑定问题

Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #2 with JdbcType null
如上报错。
代码如下

SELECT ubi.std_id,ubi.real_name,ubi.mobile,ubi.user_circle_role,bli.learn_id,bli.recruit_type,ubi.user_circle_role,ubi.user_id,ubi.circle_role_type,
		(SELECT COUNT(1) FROM bms.bd_social_circle WHERE user_id = ubi.user_id
		<if test="startTime != null and startTime !=''">
			AND sc_create_date &gt;= #{startTime}
		</if>
		<if test="endTime != null and endTime !=''">
			AND sc_create_date &lt;= #{endTime}
		</if>) dynamicNum,
		(SELECT COUNT(1) FROM us.us_circle_follow WHERE target_user_id = ubi.user_id
		<if test="startTime != null and startTime !=''">
			AND follow_time &gt;= #{startTime}
		</if>
		<if test="endTime != null and endTime !=''">
			AND follow_time &lt;= #{endTime}
		</if>) fansNum,
		(SELECT COUNT(1) FROM us.us_circle_follow WHERE user_id = ubi.user_id
		<if test="userId !=null and userId != ''">
			AND user_id = #{userId}
		</if>
		) followNum
		FROM (SELECT std_id,real_name,mobile,user_circle_role,user_id,circle_role_type FROM us.us_base_info WHERE 1=1
		<if test="userId !=null and userId != ''">
		   AND user_id = #{userId}
		</if>
		AND login_type != 2 AND std_id IS NOT NULL) ubi
		LEFT JOIN bms.bd_learn_info bli ON bli.std_id = ubi.std_id
		WHERE 1 = 1
		<if test="realName != '' and realName != null">
		    AND ubi.real_name = #{realName}
		</if>
		<if test="mobile != '' and mobile != null">
		    AND ubi.mobile = #{mobile}
		</if>
		<if test="idCard != '' and idCard != null">
		    AND bli.id_card = #{idCard}
		</if>
		<if test="unvsId != '' and unvsId != null">
		    AND bli.unvs_id = #{unvsId}
		</if>
		<if test="pfsnId != '' and pfsnId != null">
		    AND bli.pfsn_id = #{pfsnId}
		</if>
		<if test="pfsnLevel != '' and pfsnLevel != null">
		    AND bli.pfsn_level = #{pfsnLevel}
		</if>
		<if test="grade != '' and grade != null">
		    AND bli.grade = #{grade}
		</if>
		<if test="userCircleRole != '' and userCircleRole != null">
		    AND ubi.user_circle_role = #{userCircleRole}
		</if>
		
		GROUP BY ubi.user_id

主要原因在select语句的子查询使用#{startTime}取值,通过查看mybatis的源码,mybatis的预处理参数绑定是从from语句才开始执行的,所以在子查询不能使用#{}取值,而必须使用${}取值


版权声明:本文为weixin_41652785原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。