mybatis模糊查詢報錯
阿新 • • 發佈:2018-12-29
模糊查詢報錯
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='name', mode=IN, javaType=class java.lang.String, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
mabaits模糊查詢引數型別為pojo類時需使用#{屬性名} 設定佔位符並設值
<select id="queryItemsByPo" parameterType="item" resultType="item">
select * from items name like '%#{name}%'
</select>
使用以上模糊查詢報錯,使用mysql中concat()函式拼接字串,可避免錯誤
<select id="queryItemsByPo" parameterType="item" resultType="item"> select * from items <where> <if test="name != null and name !=''"> name like CONCAT('%',#{name},'%') </if> </where> </select>
mybaits模糊查詢引數型別為常用型別呢String可以使用以下方式
<select id="findUserByName" parameterType="java.lang.String" resultType="user">
<!-- ${value}使用不安全value值內引起sql注入 -->
select * from user where username like '%${value}%' <!-- ; 可帶可不帶 -->
</select>
這樣使用不會報錯