mybatis模糊查询报错

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lin1094201572/article/details/82503290

 模糊查询报错

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>

这样使用不会报错

猜你喜欢

转载自blog.csdn.net/lin1094201572/article/details/82503290