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語句纔開始執行的,所以在子查詢不能使用#{}取值,而必須使用${}取值

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章