1. 部分报错信息:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'aaa' not found. Available parameters are [page, User, param1, param2]
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:96)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:441)
at com.sun.proxy.$Proxy96.selectList(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:224)
at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.executeForIPage(MybatisMapperMethod.java:121)
at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:85)
at com.baomidou.mybatisplus.core.override.MybatisMapperProxy$PlainMethodInvoker.invoke(MybatisMapperProxy.java:148)
at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:89)
2. 报错原因:
*.xml文件中sql语句的条件值错误。
3. 部分代码:
(1) mapper接口
public interface SysUserMapper extends BaseMapper<SysUser> {
IPage<SysUser> selectByPage(IPage page, @Param("sysUser") SysUser sysUser);
}
(2) xml中的sql语句
错误写法:
<select id="selectByPage" resultMap="BaseResultMap2">
SELECT
<include refid="Base_user"/>,
<include refid="Base_dept"/>
FROM
sys_user u
LEFT JOIN sys_dept d
ON(u.dept_id = d.dept_id)
<where>
<if test="deptId!=null">
and u.dept_id IN (SELECT dept_id FROM sys_dept WHERE FIND_IN_SET(#{deptId},ancestors) OR dept_id = #{deptId})
</if>
<if test="userName!=null and userName!=''">
and u.user_name like concat('%',#{userName},'%')
</if>
<if test="phonenumber!=null and phonenumber!=''">
and u.phonenumber like concat('%',#{phonenumber},'%')
</if>
<if test="sysUser.email!=null and sysUser.email!=''">
and u.email = #{email}
</if>
<if test="status!=null and status!=''">
and u.status = #{status}
</if>
and u.del_flag = '0'
</where>
</select>
正确写法:
<select id="selectByPage" resultMap="BaseResultMap2">
SELECT
<include refid="Base_user"/>,
<include refid="Base_dept"/>
FROM
sys_user u
LEFT JOIN sys_dept d
ON(u.dept_id = d.dept_id)
<where>
<if test="sysUser.deptId!=null">
and u.dept_id IN (SELECT dept_id FROM sys_dept WHERE FIND_IN_SET(#{sysUser.deptId},ancestors) OR dept_id = #{sysUser.deptId})
</if>
<if test="sysUser.userName!=null and sysUser.userName!=''">
and u.user_name like concat('%',#{sysUser.userName},'%')
</if>
<if test="sysUser.phonenumber!=null and sysUser.phonenumber!=''">
and u.phonenumber like concat('%',#{sysUser.phonenumber},'%')
</if>
<if test="sysUser.email!=null and sysUser.email!=''">
and u.email = #{sysUser.email}
</if>
<if test="sysUser.status!=null and sysUser.status!=''">
and u.status = #{sysUser.status}
</if>
and u.del_flag = '0'
</where>
</select>
4. 修改解释:
(1)mapper接口有一个以上参数时,在xml中使用参数属性时需要具体指出该参数,否则会报BindingException异常。
(2)@Param注解是为了解决mapper接口方法有多个参数时,xxxMapper.xml文件中sql语句中绑定参数混淆问题。
例如:
xml文件中的参数值要与@Param("")中的值对应,如果接口中的多个参数属于一个JavaBean可以不使用注解@Param("xxx");反之,不是同一个JavaBean需要使用@Param("xxx")标明。