Escenario del proyecto:
提示:黑马新版web项目,
Consulta condicional para agregar, eliminar, modificar y verificar
Descripción del problema:
La conexión a la base de datos falló, al principio pensé que era mybatis, pero luego descubrí que era un problema de sintaxis SQL de configuración XML.
Palabra clave del problema: parámetro 'xxxxx' no encontrado
mensaje de error
Caused by: org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.apache.ibatis.binding.BindingException: Parameter 'brandName' not found. Available parameters are [size, param3, begin, brand, param1, param2]
### Cause: org.apache.ibatis.binding.BindingException: Parameter 'brandName' not found. Available parameters are [size, param3, begin, brand, param1, param2
Análisis de causa:
Código de problema:
<select id="selectByPageAndCondition" resultMap="brandResultMap" >
select *
from tb_brand
<where>
<if test="brand.brandName != null and brandName != '' ">
and brand_name like #{brand.brandName}
</if>
<if test="brand.companyName != null and companyName!='' ">
and company_name like #{brand.companyName}
</if>
<if test="brand.status != null">
and status = #{brand.status}
</if>
</where>
limit #{begin} , #{size}
</select>
Análisis del problema: varias situaciones de este problema
1. No hay anotación @Param(xxx) en la interfaz del mapeador, pero se usa el atributo xxx o se pasan varias variables
2. Agregar anotaciones no usa el atributo xxx para usar directamente el nombre del atributo (mi error)
solución:
Agregue el objeto delante del atributo y cámbielo a objeto.atributo
<select id="selectByPageAndCondition" resultMap="brandResultMap" >
select *
from tb_brand
<where>
<if test="brand.brandName != null and brand.brandName != '' ">
and brand_name like #{brand.brandName}
</if>
<if test="brand.companyName != null and brand.companyName!='' ">
and company_name like #{brand.companyName}
</if>
<if test="brand.status != null">
and status = #{brand.status}
</if>
</where>
limit #{begin} , #{size}
</select>