org.apache.ibatis.binding.BindingException

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")标明。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值